asp.net – 从Web应用程序打印
|
如何从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 – 实现IModelBinder的最佳实践
- asp.net – 反伪造cookie令牌和表单字段令牌在MVC 4中不匹配
- asp.net-mvc-4 – MVC4捆绑GZIP和头文件
- asp.net – 在哪里可以下载DLR的Managed JScript?
- asp.net – 如何在StackedBar中值为零时隐藏数据点标签
- ASP.NET AJAX:在页面加载完成后触发UpdatePanel
- asp.net – Razor 3有什么新功能?
- asp.net – System.Linq.Dynamic不支持OrderByDescending(“
- 在asp.net mvc中如何使用usercontrols来显示“岛”数据?
- asp.net-mvc – ASP.Net MVC是否运行在ASP.NET 2.0之上?
- asp.net – HtmlGenericControl(“br”)呈现两次
- asp.net-mvc-3 – AutoMapper线程问题(缺少类型映
- asp.net-mvc-4 – MVC 4 – Web Api和JSON?
- asp.net – 检查是否在集成管道模式
- asp.net-mvc – 何时使用RedirectToAction和哪里
- 在Kendo-UI图表中刷新方法和重绘方法有什么不同?
- asp.net – ASP .NET检查会话
- asp.net – 如何在MVC 3中设置图表系列颜色?
- asp.net – 如何自动执行功能/集成测试和数据库回
- asp.net-mvc-3 – MVC3 Razor使用Html.BeginForm
