使用Itextsharp填写PDF表格 [英] Fill PDF Form with Itextsharp

查看:706
本文介绍了使用Itextsharp填写PDF表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用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:

  1. 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.

  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆