C# 使用 iText (iTextSharp) 在 PDF 中填充 XFA 表单字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1041652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using iText (iTextSharp) to populate XFA form fields in PDF?
提问by Jay Stevens
I need to populate XFA form fields in a PDF (created with Adobe LiveCycle Designer). We're attempting to use iText (actually iTextSharp with C#) to parse the PDF, populate the XFA fields and then save the modified PDF back out.
我需要在 PDF(使用 Adobe LiveCycle Designer 创建)中填充 XFA 表单字段。我们正在尝试使用 iText(实际上是带有 C# 的 iTextSharp)来解析 PDF,填充 XFA 字段,然后将修改后的 PDF 保存回来。
All the examples I can find with iText (very few iTextSharp examples) talk about modifying AcroForm fields. This PDF does NOT have AcroForm fields and uses XFA only.
我可以在 iText 中找到的所有示例(很少有 iTextSharp 示例)都在讨论修改 AcroForm 字段。此 PDF 没有 AcroForm 字段,仅使用 XFA。
Pointers to any non-standard resources would be helpful (I've already done the requisite Googling on the topic and haven't found anything useful).
指向任何非标准资源的指针都会有所帮助(我已经对该主题进行了必要的谷歌搜索,但没有发现任何有用的东西)。
Code examples here would be awesome from anyone who has actually done what I'm trying to do.
任何真正做过我想做的事情的人,这里的代码示例都会很棒。
采纳答案by stevex
If you can get a data packet into the PDF, the XFA runtime in Acrobat would populate those fields with the data in the data packet.
如果您可以将数据包放入 PDF,Acrobat 中的 XFA 运行时将使用数据包中的数据填充这些字段。
If you want to see what one of these looks like, create a form in LiveCycle Designer (comes with Acrobat Pro), add some fields to it, and save it as a dynamic PDF. Open the form in Acrobat and type some values into the fields and save it.
如果您想查看其中一个的外观,请在 LiveCycle Designer(随 Acrobat Pro 一起提供)中创建一个表单,向其中添加一些字段,然后将其另存为动态 PDF。在 Acrobat 中打开表单并在字段中键入一些值并保存。
Open the PDF with a tool that lets you peer at the PDF data and you'll find /Catalog/AcroForm/XFA a stream that has an <xfa:datasets> packet with the values you typed. That's what you'll need to create yourself and insert into the PDF.
使用可让您查看 PDF 数据的工具打开 PDF,您会发现 /Catalog/AcroForm/XFA 是一个流,其中包含一个带有您键入的值的 <xfa:datasets> 数据包。这就是您需要自己创建并插入到 PDF 中的内容。
The XDP spec includes a description of the data packet and the merge algorithm. You can find it here:
XDP 规范包括对数据包和合并算法的描述。你可以在这里找到它:
http://partners.adobe.com/public/developer/xml/index_arch.html
http://partners.adobe.com/public/developer/xml/index_arch.html
Alternately, you buy the LiveCycle server from Adobe which lets you do all this programmatically in a number of ways including through web service calls.
或者,您可以从 Adobe 购买 LiveCycle 服务器,它可以让您以多种方式(包括通过 Web 服务调用)以编程方式完成所有这些工作。
回答by Jay Stevens
It says that in the book because itext does not do this. Can you convert your PDF?
它在书中说,因为 itext 没有这样做。你能转换你的PDF吗?
回答by Dmitry
iTextSharp can work with XFA. To remove all doubts, please take a look at sample on iText website:
iTextSharp 可以与 XFA 一起使用。为了消除所有疑问,请查看 iText 网站上的示例:
回答by simaglei
using (FileStream existingPdf = new FileStream("existing.pdf", FileMode.Open))
using (FileStream sourceXml = new FileStream("source.xml", FileMode.Open))
using (FileStream newPdf = new FileStream("new.pdf", FileMode.Create))
{
// Open existing PDF
PdfReader pdfReader = new PdfReader(existingPdf);
// PdfStamper, which will create
PdfStamper stamper = new PdfStamper(pdfReader, newPdf);
stamper.AcroFields.Xfa.FillXfaForm(sourceXml);
stamper.Close();
pdfReader.Close();
}
回答by Micah 'Powershell Ninja'
I have the same issue and I think I found the solution. I am using Powershell to inspect the pdf object.
我有同样的问题,我想我找到了解决方案。我正在使用 Powershell 检查 pdf 对象。
Load the iTextSharp DLL.
加载 iTextSharp DLL。
Add-Type -Path "C:\Users\micah\Desktop\itextsharp.dll"
Load the PDF into a variable:
将 PDF 加载到变量中:
$PDF = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList "C:\Users\micah\Desktop\test.pdf"
Inspect this object:
检查此对象:
$PDF.AcroFields.XFA.DomDocument.XDP.DataSets.Data.TopMostSubForm | Get-Member
What you SHOULD see, is that all your fields on your PDF are in this object as a property. You can get a quick view of all your fields like this:
您应该看到的是,您的 PDF 上的所有字段都作为属性位于此对象中。您可以像这样快速查看所有字段:
$PDF.AcroFields.XFA.DomDocument.XDP.DataSets.Data.TopMostSubForm | Select-Object -Property "*"
That should be the magic ticket. This location is both fields from the original form AND the XFA portion of the form.
那应该是魔票。此位置既是原始表单中的字段,又是表单的 XFA 部分。