首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从Web应用程序打印?

如何从Web应用程序打印?
EN

Stack Overflow用户
提问于 2018-06-25 04:47:28
回答 2查看 0关注 0票数 0

你如何从Web应用程序生成纸质印刷品?

具体而言,我正在考虑更复杂的纸质文件,如文凭,发票和合同。可变数量的页面,包括框架,表格,标识和页眉/页脚。

今天,我使用自定义表单和CSS来处理某些事情,使用iTextSharp来处理其他事务(我使用asp.net和MS-SQL),但我认为这两种方法都非常耗时且很难在不同文档间保持一致。

有没有更好的方法去解决它?

EN

回答 2

Stack Overflow用户

发布于 2018-06-25 13:21:26

固定格式打印PDF

票数 0
EN

Stack Overflow用户

发布于 2018-06-25 14:37:35

代码语言:javascript
复制
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;
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100000336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档