如何以编程方式填充现有的PDF文档

本文关键字:PDF 文档 填充 编程 方式 | 更新日期: 2023-09-27 18:05:40

我有一个PDF文档,它不是使用Adobe LifeCycle Designer创建的。我要做的是预先填写文档中的公共字段。

我已经研究了使用iTextSharp和PDFSharp的许多可用选项,但不确定如何正确使用它。

我最近看到了这篇文章:FillPDF,它有一个很好的文档,但与我正在创建的内容不匹配。

我也在阅读关于iTextSharp可以在VS中导入和使用,但我不确定从哪里开始。我看过很多教程,但是没有一个描述如何开始。

请帮…

如何以编程方式填充现有的PDF文档

我最近用itextsharp做了一个大项目

http://www.mikesdotnetting.com/Article/88/iTextSharp-Drawing-shapes-and-Graphicshttp://www.mikesdotnetting.com/Article/81/iTextSharp-Working-with-Fonts

这里有一些让你开始的东西

,但只要从PDF中读取然后输出回来你需要一些正则表达式来帮助你。

这里是我的一个示例代码(它在每个新页面事件上创建一个页眉或页脚)

using CMS;
using CMS.Tags;
using CMS.Pages;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Globalization;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.draw;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.util;
public class pdfPage : iTextSharp.text.pdf.PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document doc)
    {
        PdfContentByte cb = writer.DirectContent;
        cb.SetLineWidth(1f);
        cb.SetCMYKColorStroke(66, 59, 57, 38);
        cb.MoveTo(30, 55);     
        cb.LineTo(doc.PageSize.Width - 30 , 55);     
        cb.Stroke();
        cb.MoveTo(185, 80);
        cb.LineTo(185, 25);
        cb.Stroke();
        ColumnText ct = new ColumnText(cb);
        BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
        Font times = new Font(bfTimes, 10);
        times.SetColor(90, 90, 90);
        ct.SetSimpleColumn(new Phrase("text text", times), 60, 60, 175, 78, 15, Element.ALIGN_MIDDLE);
        ct.Go();
        times.SetColor(1, 73, 144);
        ct.SetSimpleColumn(new Phrase("text textn", times), 60, 38, 175, 55, 15, Element.ALIGN_MIDDLE);
        ct.Go();
        times.SetColor(90, 90, 90);
        ct.SetSimpleColumn(new Phrase("text here", times), 190, 60, doc.PageSize.Width - 32 , 78, 15, Element.ALIGN_RIGHT);
        ct.Go();
        times.SetColor(90, 90, 90);
        ct.SetSimpleColumn(new Phrase("text here", times), 190, 38, doc.PageSize.Width - 32 , 55, 15, Element.ALIGN_RIGHT);
        ct.Go();
    }
}

这是pdf

开头部分的代码
using (var ms = new MemoryStream())
        {
            using (var doc = new Document(PageSize.LETTER, 220f, 30f, 115f, 100f)){
                try
                {
                  pdfPage page = new pdfPage();
                  PdfWriter writer = PdfWriter.GetInstance(doc, ms);
                  writer.PageEvent = page;
                  doc.Open();
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(RESOURCE);
                img.ScalePercent(49f);
                //img.Width = doc.PageSize.Width;
                //img.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
                img.SetAbsolutePosition(-8, 
                    doc.PageSize.Height - 180.6f);
                doc.Add(img);

和这是我的输出代码(作为下载PDF直接创建从服务器*不保存在服务器上)

}
                catch (Exception ex)
                {
                  //Log error;
                }
                finally
                {
                  doc.Close();
                }
            }
            Response.Clear();
              //Response.ContentType = "application/pdf";
              Response.ContentType = "application/octet-stream";
              Response.AddHeader("content-disposition", "attachment;filename= Company " + namefile + ".pdf");
              Response.Buffer = true; 
              Response.Clear();
              var bytes = ms.ToArray();
              Response.OutputStream.Write(bytes, 0, bytes.Length);
              Response.OutputStream.Flush();
        }