我有一个 PDF 文档,该文档不是使用 Adobe LifeCycle Designer 创建的。我想要做的是预先填写文档中的公共(public)字段。
我已经研究了使用 iTextSharp 和 PDFSharp 时可以使用的许多选项,但不确定如何正确使用它。
我最近看到这篇文章:FillPDF它有一个很好的文档,但与我正在创建的内容不匹配。
我还阅读了有关 iTextSharp 的信息,它可以在 VS 中导入并使用,但我不知道从哪里开始。我看了很多教程,但没有一个描述如何开始。
请帮忙...
最佳答案
我最近使用 itextsharp 参与了一个大型项目
http://www.mikesdotnetting.com/Article/88/iTextSharp-Drawing-shapes-and-Graphics http://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();
}
关于c# - 如何以编程方式填充现有 PDF 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23934877/