asp.net – 从Web应用程序打印
发布时间:2020-12-16 04:30:09 所属栏目:asp.Net 来源:网络整理
导读:如何从Web应用程序生成纸质打印? 具体来说,我正在考虑更复杂的纸质文件,如文凭,发票和合同.包含框架,表格,徽标和页眉/页脚的可变页数. 今天我使用自定义表单和CSS用于某些事情,而iTextSharp用于其他人(我使用asp.net和MS-SQL),但我认为这两种方法都非常耗时
如何从Web应用程序生成纸质打印?
具体来说,我正在考虑更复杂的纸质文件,如文凭,发票和合同.包含框架,表格,徽标和页眉/页脚的可变页数. 今天我使用自定义表单和CSS用于某些事情,而iTextSharp用于其他人(我使用asp.net和MS-SQL),但我认为这两种方法都非常耗时且难以在不同文档中保持一致. 还有更好的方法吗? 解决方法
使用iTextSharp从头开始创建文档可能非常耗时.作为替代方案,您可以通过向其添加表单字段来创建(或重用)PDF“模板”文档(如果需要).最简单的方法是使用完整版的Adobe Acrobat,但您也可以使用iTextSharp添加可填写的表单字段.
例如,对于奖励或文凭,您可以查找,创建或修改包含文档的所有文本,图形,花式边框和字体的PDF,然后为收件人的名称添加表单字段.您可以为日期,签名行,奖励类型等添加其他字段. 然后,您可以非常轻松地使用Web应用程序中的iTextSharp填写表单,将其展平并将其流回用户. 在这篇文章的最后是一个完整的ASHX处理程序代码示例. 还要记住,iTextSharp(或只是iText)对于组合来自不同文档的PDF文档或页面也很有用.因此,对于具有封面或说明页面的固定设计但是动态生成内容的年度报告,您可以打开封面,打开报告的模板页面,在模板的空白区域生成动态内容,打开后页“样板”,然后将它们全部合并到一个文档中以返回给用户. using System; using System.Data; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.Text; using iTextSharp; using iTextSharp.text; using iTextSharp.text.pdf; namespace iTextFormFillerDemo { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class DemoForm : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/pdf"; //This line will force the user to either open or save the //file instead of it appearing on its own page - if you remove it,//the page will appear in the browser in the same window. context.Response.AddHeader("Content-Disposition","attachment; filename=DemoForm_Filled.pdf"); FillForm(context.Response.OutputStream); context.Response.End(); } // Fills the form and pushes it out the output stream. private void FillForm(System.IO.Stream outputStream) { //Need to get the proper directory (dynamic path) for this file. //This is a filesystem reference,not a web/URL reference... //The PDF reader reads in the fillable form PdfReader reader = new PdfReader("C:/DemoForm_Fillable.pdf"); //The PDF stamper creates an editable working copy of the form from the reader //and associates it with the response output stream PdfStamper stamper = new PdfStamper(reader,outputStream); //The PDF has a single "form" consisting of AcroFields //Note that this is shorthand for writing out //stamper.AcroFields.SetField(...) for each set AcroFields form = stamper.AcroFields; //Set each of the text fields this way: SetField(name,value) form.SetField("txtFieldName","Field Value"); form.SetField("txtAnotherFieldName","AnotherField Value"); //Set the radio button fields using the names and string values: form.SetField("rbRadioButtons","Yes"); //or "No" //Form flattening makes the form non-editable and saveable with the //form data filled in stamper.FormFlattening = true; //Closing the stamper flushes it out the output stream stamper.Close(); //We're done reading the file reader.Close(); } public bool IsReusable { get { return false; } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 重定向到操作,需要传递数据
- asp.net-mvc-4 – 使用WebApi和外部登录的ASP.NET身份
- asp.net – .NET – 如何调试DLL?
- asp.net – 表达式标记中的问题,用于绑定字符串变量
- asp.net-mvc-5 – 如何在服务器上保存Rotativa PDF
- asp.net-mvc – 在哪里?Json.Encode或@Json.Decode方法在M
- entity-framework – 如何找到UIHInt属性的target属性?
- asp.net-mvc – 如何在版本信息中包含静态内容
- 如何将数组从Asp.net服务器端传递到客户端的Javascript函数
- vbscript – 在赋值中使用Set时“需要对象”
推荐文章
站长推荐
- asp.net-mvc-3 – “update-database”的实体框架
- asp.net-mvc – 从MVC 3升级到4后,Razor帮助器方
- Asp.Net MVC @Html.TextBox 只允许输入数字问题
- 如何在ASP.Net GridView中使用AutoGenerateEditB
- asp.net-mvc – redirectToAction导致null模型
- 获取当前ASP.NET Web Api 2操作的URL
- .Net 5分钟搞定网页实时监控
- asp.net – Lucene.Net是否管理访问相同索引的多
- asp.net-mvc – ASP.NET MVC:具有POST Action参
- ASP.NET MVC ViewEngine ViewLocationCache.GetV
热点阅读