问题
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();
}
回答1:
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.
来源:https://stackoverflow.com/questions/33597009/how-to-create-and-fill-out-a-pdf-form