问题一
Document doc.Save(Response, "学员报名表.pdf", ContentDisposition.Inline, null); 执行后没有效果,因为异步的时候需要加,如果不是异步,即没有使用UpdatePanel,那么就不要加此方法Triggers。
解决
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <Triggers> <asp:PostBackTrigger ControlID="btnExportAndPrint" /> </Triggers> <ContentTemplate> <asp:Button runat="server" ID="btnExportAndPrint" class="btn btn-primary" Text="打印报名表" OnClick="btnExportAndPrint_Click"/> </ContentTemplate> </asp:UpdatePanel>
我的程序
protected void btnExportAndPrint_Click(object sender, EventArgs e) { string studentNo = txtStudentCode.Text; if (!string.IsNullOrWhiteSpace(studentNo)) { OLD_StudentUser student = OLD_StudentUserDAL.GetByStudentNo(studentNo); string templateFile = Server.MapPath("../Template/Word/学员报名表.doc"); //string photoPath = Server.MapPath("../IDCardPhoto/142201199201264557.Bmp"); //测试方法 string photoPath = Server.MapPath(student.ZP); Document doc = new Document(templateFile); doc = ExportAndPrintHelper.ExportAndPrintRegister(doc,student,studentNo,photoPath); if (doc != null) { doc.Save(Response, "学员报名表.pdf", ContentDisposition.Inline, null); } } else { Response.Write("<script>alert('学员学号为空!')</script>"); } }
public class ExportAndPrintHelper { /// <summary> /// 导出和打印学员报名表 /// </summary> /// <param name="doc"></param> /// <param name="student"></param> /// <param name="StudentNo"></param> /// <param name="photoPath"></param> /// <returns></returns> public static Document ExportAndPrintRegister(Document doc, OLD_StudentUser student, string studentNo, string photoPath) { DocumentBuilder builder = new DocumentBuilder(doc); if (!string.IsNullOrWhiteSpace(studentNo)) { #region 基础信息 if (doc.Range.Bookmarks["Code"] != null) { doc.Range.Bookmarks["Code"].Text = DateTime.Now.ToShortDateString(); } if (doc.Range.Bookmarks["Name"] != null) { doc.Range.Bookmarks["Name"].Text = student.Name; } if (doc.Range.Bookmarks["Sex"] != null) { doc.Range.Bookmarks["Sex"].Text = student.Sex; } if (doc.Range.Bookmarks["Born"] != null) { doc.Range.Bookmarks["Born"].Text = student.DirthDate.ToString("yyyy.MM.dd"); } if (doc.Range.Bookmarks["StudentNo"] != null) { doc.Range.Bookmarks["StudentNo"].Text = student.StudentsNo; } if (doc.Range.Bookmarks["Politics"] != null) { doc.Range.Bookmarks["Politics"].Text = student.Politics; } if (doc.Range.Bookmarks["IDCardNo"] != null) { doc.Range.Bookmarks["IDCardNo"].Text = student.SID; } if (doc.Range.Bookmarks["EducationLevel"] != null) { doc.Range.Bookmarks["EducationLevel"].Text = student.EducationLevel; } if (doc.Range.Bookmarks["Units"] != null) { doc.Range.Bookmarks["Units"].Text = student.Units; } if (doc.Range.Bookmarks["Job"] != null) { doc.Range.Bookmarks["Job"].Text = student.Job; } if (doc.Range.Bookmarks["Phone"] != null) { doc.Range.Bookmarks["Phone"].Text = student.Phone; } if (doc.Range.Bookmarks["RelativesPhone"] != null) { doc.Range.Bookmarks["RelativesPhone"].Text = student.RelativesPhone; } if (doc.Range.Bookmarks["ResidenceAdd"] != null) { doc.Range.Bookmarks["ResidenceAdd"].Text = student.ResidenceAdd; } if (doc.Range.Bookmarks["Address"] != null) { doc.Range.Bookmarks["Address"].Text = student.Address; } if (doc.Range.Bookmarks["NowTime"] != null) { doc.Range.Bookmarks["NowTime"].Text = DateTime.Now.ToShortDateString(); } if (doc.Range.Bookmarks["Photo"] != null) { builder.MoveToBookmark("Photo"); var img = builder.InsertImage(photoPath); img.Width = 76; img.Height = 94; } #endregion #region 所报课程 List<OLD_CourseRegistration> courses = OLD_CourseRegistrationDAL.GetByStudentNo(studentNo); for (int i = 9; i < courses.Count + 9; i++) { OLD_Class classes = OLD_ClassDAL.GetByClassName(courses[i - 9].ClassName); builder.MoveToCell(0, i, 2, 0); builder.Write(courses[i - 9].ClassName.ToString()); builder.MoveToCell(0, i, 3, 0); builder.Write(classes.TeacherName.ToString()); builder.MoveToCell(0, i, 4, 0); builder.Write(classes.SKdate.ToString()); builder.MoveToCell(0, i, 5, 0); builder.Write(classes.ClassAdd.ToString()); builder.MoveToCell(0, i, 6, 0); builder.Write(classes.FY.ToString()); } for (int cancel = 22; cancel >= courses.Count + 9; cancel--) { builder.DeleteRow(0, cancel); } #endregion return doc; } else { return null; } } }
word图片
执行保存之后的图片