使用Itextsharp填写PDF表格 [英] Fill PDF Form with Itextsharp
问题描述
我正在尝试用ITextsharp填写表单,并尝试使用以下代码来获取pdf中的所有字段:
string pdfTemplate = @c:\Temp \questionnaire.pdf;
PdfReader pdfReader = new PdfReader(pdfTemplate);
StringBuilder sb = new StringBuilder();
foreach(var de in pdfReader.AcroFields.Fields)
{
sb.Append(de.Key.ToString()+ Environment.NewLine);
}
但是foreach循环始终为空计数。我是否需要做一些事情来提交自己,因为我已经尝试过的第8章以及可用的示例此处为Java 和此处为.Net 。
这些示例主要是将表单字段添加到新生成的PDF文档中。但是,您可以使用相同的代码将表单字段添加到PdfStamper中,该PdfStamper使用Java中的 stamper.getWriter()
和 C#中的stamper.Writer
。而不是 writer.addAnnotation(field)
,你必须使用 stamper.addAnnotation(field,page)
。 / p>
I am trying to fill up a form with ITextsharp, and trying out the following code to get all the fields in the pdf:
string pdfTemplate = @"c:\Temp\questionnaire.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
StringBuilder sb = new StringBuilder();
foreach (var de in pdfReader.AcroFields.Fields)
{
sb.Append(de.Key.ToString() + Environment.NewLine);
}
But the foreach loop is always null count. Do I need to do something to file itself as I have tried the example from here and it works fine... this is an example of pdf I am trying to fill
any ideas?
Edit ::
As it turned out, the PDF "form" to fill in actually wasn't a form (in PDF terms) at all. Thus, your have two choices:
You add the text to the page contents directly using hardcoded or configured "field" positions and dimensions as described by @tschmit007 in comments to his answer.
You add actual PDF form fields to your PDF to generate a true PDF form which you take as template to fill in later.
You can add actual form fields either using some graphical tool allowing that, e.g. Adobe Acrobat, or you can use iText(Sharp). Have a look at chapter 8 of iText in Action — 2nd Edition and the samples available here for Java and here for .Net.
Those samples mostly add form fields to newly generated PDF documents. You can virtually use the same code, though, for adding form fields to a PdfStamper which exposes its inner PdfWriter using stamper.getWriter()
in Java and the stamper.Writer
in C#. Instead of writer.addAnnotation(field)
you have to use stamper.addAnnotation(field, page)
, though.
这篇关于使用Itextsharp填写PDF表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!