如何创建和填写PDF表单 [英] How to create and fill out a PDF form

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

问题描述

问题的背景:我可能会在纸/ word document.pdf文档模板中获得不同类型的模板,其中包含控件(文本框,复选框)。



现在我必须在提供的模板中分配值,并且必须将其保存为pdf文档。



所以我发现了一些对模板有用的东西,叫做Openoffice,来自在那里我可以添加控件并能够保存为pdf模板文档。



现在该模板需要分配值,为此找到一个名为iTextSharp的dll。



基于我试图添加的样本,pdf模板中字段的计数显示为0.可以任何正文帮我分配值PDF模板?
下面是我在这种情况下使用的代码。

  private void ListFieldNames()
{
string pdfTemplate = @D:\ 17.pdf;
loadPDF(pdfTemplate);


//标题形式
//this.Text + = - + pdfTemplate;

//基于PDF模板文档创建一个新的PDF阅读器
PdfReader pdfReader = new PdfReader(pdfTemplate);

//使用主题中可用的每个
//字段名称创建和填充字符串构建器PDF
StringBuilder sb = new StringBuilder();
foreach(DictionaryEntry de in pdfReader.AcroFields.Fields)
{
sb.Append(de.Key.ToString()+ Environment.NewLine);
}

//将字符串生成器的内容写入表单的文本框
// userName.Text = sb.ToString();

}


解决方案

创建一个使用OpenOffice的交互式PDF表单并使用iText(或iTextSharp)填写它是一个明智的选择。请阅读



创建真实字段并为这些字段命名是很重要的。然后,您必须确保将文档导出为PDF格式:





如您所见,您需要选中创建PDF旁边的复选框。



我有理由相信您没有这样做,因为你说:


根据我试图添加的样本,pdf模板中字段的数量是显示为0.。


如果您希望我们检查该PDF模板中字段的数量是否为0,您应该共享PDF或者如果你做错了什么。



如果你正确地做事(即:如我的书中所述),你可以填写如下表格: / p>

  PdfReader reader = new PdfReader(template); 
PdfStamper stamper = new PdfStamper(reader,
new FileStream(newFile,FileMode.Create));
AcroFields form = stamper.AcroFields;
form.SetField(key1,value1);
form.SetField(key2,value2);
form.SetField(key3,value3);
...
stamper.Close();

在此代码段 key1 中, key2 key3 ,...是您在OpenOffice中创建表单时为字段定义的值,以及 value1 value2 value3 ,...是你的值想要添加到字段中。


background of the issue: I may get different kind of templates which contains controls(Text Box, check box) some times in a paper/word document.pdf document templates.

Now i have to assign values in that provided templates, and have to save it as pdf document.

So i found some thing useful for templating called Openoffice,from there i am able to add controls and able to save as pdf template document.

Now for that template need to assign values, for that found one dll called iTextSharp.

based on the samples while i am trying to add , the count of the fields in the pdf template is showing as 0. could any body help me out to assign the values for the PDF template? Below is the code i am using for this case.

   private void ListFieldNames()
        {
            string pdfTemplate = @"D:\17.pdf";
            loadPDF(pdfTemplate);


            // title the form
            //this.Text += " - " + pdfTemplate;

            // create a new PDF reader based on the PDF template document
            PdfReader pdfReader = new PdfReader(pdfTemplate);

            // create and populate a string builder with each of the
            // field names available in the subject PDF
            StringBuilder sb = new StringBuilder();
            foreach (DictionaryEntry de in pdfReader.AcroFields.Fields)
            {
                sb.Append(de.Key.ToString() + Environment.NewLine);
            }

            // Write the string builder's content to the form's textbox
          //  userName.Text = sb.ToString();

        } 

解决方案

Creating an interactive PDF form using OpenOffice and filling it out using iText (or iTextSharp) is a no-brainer. Please read Chapter 6 of my book, more specifically section 6.3.5 "Filling out a PDF form." The first part of this section is titled "Creating a form with OpenOffice."

It is important that you create real fields and that you give these fields a name. You then have to make sure that you export the document as a PDF form:

As you can see, you need to check the check box next to Create PDF.

I have reasons to believe that you didn't do this, because you say:

based on the samples while i am trying to add , the count of the fields in the pdf template is showing as 0.

You should share your PDF if you want us to check if the count of the fields in that PDF template is 0 or if you are doing something wrong.

If you are doing things correctly (that is: as described in my book), you can fill out the form like this:

PdfReader reader = new PdfReader(template);
PdfStamper stamper = new PdfStamper(reader,
    new FileStream(newFile, FileMode.Create));
AcroFields form = stamper.AcroFields;         
form.SetField(key1, value1);      
form.SetField(key2, value2);      
form.SetField(key3, value3);
...
stamper.Close();

In this snippet key1, key2, key3,... are the values you defined for the fields when you created the form in OpenOffice, and value1, value2, value3,... are the values that you want to add to the fields.

这篇关于如何创建和填写PDF表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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