你如何从Web应用程序生成纸质印刷品?
具体而言,我正在考虑更复杂的纸质文件,如文凭,发票和合同。可变数量的页面,包括框架,表格,标识和页眉/页脚。
今天,我使用自定义表单和CSS来处理某些事情,使用iTextSharp来处理其他事务(我使用asp.net和MS-SQL),但我认为这两种方法都非常耗时且很难在不同文档间保持一致。
有没有更好的方法去解决它?
发布于 2018-06-25 13:21:26
固定格式打印PDF
发布于 2018-06-25 14:37:35
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;
}
}
}
}
https://stackoverflow.com/questions/-100000336
复制相似问题