I was about to use iTextSharp to fill out PDF form, when I've found out, that there is new kid on the block: iText 7.0. Naturally I gave it a try and... it didn't go well, unfortunately. Here is an issue. I have editable PDF form, that's need to be filled out programmatically. I used following code to acomplished that:
string srcPdfFormPath = #"<Path to source pdf form>";
string destPdfFormPath = #"<Path to destination pdf form>";
using (PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(destPdfFormPath)))
{
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);
var fieldName = "FullName";
var pdfField = form.GetField(fieldName);
if (pdfField != null)
pdfField.SetValue("John Doe", null);
else
Debug.WriteLine($"Cannot find following field {fieldName } on pdf form.");
pdfDocument.Close();
}
Though desired field has been populated, the form not only has been flattened(which, AFAIK is not expected behavior), but also when populated pdf would be opened in Acrobat reader it would produce following message:
"This document enabled extended features in Adobe Acrobat Reader DC.
The document has been changed since it was created and use of extended
features is no longer available. Please contact the author for the
original version of this document."
Those results are not what we have been shooting for. So in order to preserve the document editability and get rid of pesky message I've decided to open PDF form in "Append Mode". Here is a code I've used:
string srcPdfFormPath = #"<Path to source pdf form>";
string destPdfFormPath = #"<Path to destination pdf form>";
using (PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(destPdfFormPath)
, new StampingProperties().UseAppendMode()))
{
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);
var fieldName = "FullName";
var pdfField = form.GetField(fieldName);
if (pdfField != null)
pdfField.SetValue("John Doe", null);
else
Debug.WriteLine($"Cannot find following field {fieldName } on pdf form.");
pdfDocument.Close();
}
And, alas, this attempt also failed on its mission. Though pesky message would no longer be seen, the form itself would not be populated, which is not acceptable. And for the life of me I could not figure out why those behaviors are persist and go together, like horse and carriage!
So question is: what am I missing here? Is there way to make it work using new version of iText?
Related
I am using itext7 to get the Stream of the file and fill in the form fields.
Here is my code:
using (MemoryStream outFile = new MemoryStream())
{
var streamFile = await App.GraphClient.Me.Drive.Items[item.Id].Content.Request().GetAsync();
using (PdfDocument pdf = new PdfDocument(new PdfReader(streamFile)))
{
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
PdfFormField toSet;
fields.TryGetValue("Full_Names", out toSet);
toSet.SetValue(Customer_Name.Text);
form.FlattenFields();
pdf.Close();
}
}
But I get an error on this line: toSet.SetValue(Customer_Name.Text); when trying to set the value, the error is:
There is no associate PdfWriter for making indirects.
My question is what am I doing wrong and how do I fix this? I am getting the file from OneDrive using Microsoft Graph API. The IDictionary of fields is getting populated and the name of field I am trying to set the value for is accurate.
The problem with your code is that you have dedicated outFile stream for the output result (the document with flattened fields) but you are not telling iText anything about that desired output destination.
PdfDocument has several constructors and the one you are using is PdfDocument(PdfReader) and it is dedicated for reading the document, not changing it (and setting a value is considered changing the document of course). So you should use PdfDocument(PdfReader, PdfWriter) constructor to provide the source document and the target destination to iText.
You should create your PdfDocument as follows:
PdfDocument pdf = new PdfDocument(new PdfReader(streamFile), new PdfWriter(outFile))
At the end of the execution, your outFile stream will contain the bytes of the resultant document.
Good Morning,
I don't know, how can i read the field name form below pdf.
I used all methods for AcroFields, but all methods returns 0 or null
http://www.finanse.mf.gov.pl/documents/766655/1481810/PIT-8C(7)_v1-0E.pdf
my code:
try {
PdfReader.unethicalreading = true;
PdfReader reader = new PdfReader(new FileInputStream("/root/TestPit8/web/notmod.pdf"));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("/root/TestPit8/web/testpdf.pdf"));
AcroFields form = stamper.getAcroFields();
form.setField("text_1", "666");
form.setField("text_2", "666");
form.setField("text_3", "666");
form.setFieldProperty("text_3", "clrfflags", TextField.PASSWORD, null);
form.setFieldProperty("text_3", "setflags", PdfAnnotation.FLAGS_PRINT, null);
form.setField("text_3", "12345678", "xxxxxxxx");
form.setFieldProperty("text_4", "textsize", new Float(12), null);
form.regenerateField("text_4");
stamper.close();
reader.close();
} catch( Exception ex) {
ex.printStackTrace();
}
Thx forhelp
The form you share is a pure XFA form. XFA stands for the XML Forms Architecture.
Please read The Best iText Questions on StackOverflow and scroll to the section entitled "Interactive forms".
These are the first two questions of this section:
How to fill out a pdf file programmatically? (AcroForm
technology)
How to fill out a pdf file programmatically? (Dynamic
XFA)
You are filling out the form as if it were based on AcroForm technology. That isn't supposed to work, is it? Your form is an XFA form!
Filling out an XFA form is explained in my book, in the XfaMovies example:
public void manipulatePdf(String src, String xml, String dest)
throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
XfaForm xfa = form.getXfa();
xfa.fillXfaForm(new FileInputStream(xml));
stamper.close();
reader.close();
}
In this case, src is a path to the original form, xml is a path to the XML data, and dest is the path of the filled out form.
If you want to read the data, you need the XfaMovie example:
This reads the full form (all the XFA):
public void readXfa(String src, String dest)
throws IOException, ParserConfigurationException, SAXException,
TransformerFactoryConfigurationError, TransformerException {
FileOutputStream os = new FileOutputStream(dest);
PdfReader reader = new PdfReader(src);
XfaForm xfa = new XfaForm(reader);
Document doc = xfa.getDomDocument();
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.transform(new DOMSource(doc), new StreamResult(os));
reader.close();
}
If you only want the data, you need to examine the datasets node:
public void readData(String src, String dest)
throws IOException, ParserConfigurationException, SAXException,
TransformerFactoryConfigurationError, TransformerException {
FileOutputStream os = new FileOutputStream(dest);
PdfReader reader = new PdfReader(src);
XfaForm xfa = new XfaForm(reader);
Node node = xfa.getDatasetsNode();
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
if("data".equals(list.item(i).getLocalName())) {
node = list.item(i);
break;
}
}
list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
if("movies".equals(list.item(i).getLocalName())) {
node = list.item(i);
break;
}
}
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.transform(new DOMSource(node), new StreamResult(os));
reader.close();
}
Note that I don't understand why you think there are fields such as text_1, text_2 in the form. XFA fields are easy to recognize because the contain plenty of [] characters.
Also: from the screenshot below (taken with iText RUPS), it is clear that there are no such fields in the form:
The tools are there on the iText web site. The documentation is there. Please use it!
Update:
So... instead of accepting my comprehensive answer, you decided to post a comment asking me to do your work in your place by asking where I can find example code? in spite of the fact that I provided links to XfaMovie and XfaMovies.
Well, here are two new examples for you:
ReadXFA takes xfa_form_poland.pdf and reads the data with data.xml as result.
FillXFA2 takes xfa_form_poland.pdf and fills it out with xfa_form_poland.xml resulting in xfa_form_poland_filled.pdf
Of course: I don't understand Polish, so I didn't always fill out the correct values, but now at least you have no longer a reason to ask where I can find example code?
Update 2:
In an extra comment, you claim that you can't find the NIP number (number 10 in the form) anywhere in the data structure.
This means either that you haven't examined data.xml, or that you don't understand XML.
Allow me to show the relevant part of the XML that contains the NIP number:
<Deklaracja xmlns="http://crd.gov.pl/wzor/2014/12/05/1880/" xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/">
....
<Podmiot2 rola="Podatnik">
<etd:OsobaFizyczna>
<etd:NIP>0123456789</etd:NIP>
<etd:ImiePierwsze>JUST TRY</etd:ImiePierwsze>
<etd:Nazwisko>DUDE</etd:Nazwisko>
<etd:DataUrodzenia>2015-02-19</etd:DataUrodzenia>
</etd:OsobaFizyczna>
</Podmiot2>
...
</Deklaracja>
In other words, the field name you're looking for is probably something like this: Deklaracja[0].Podmiot2[0].OsobaFizyczna[0].NIP[0] (whatever these words may mean, I only know one Polish word: Podpis).
I am trying to copy one page from an existing .pdf file and paste it to a new document like this:
using (var writer = new PdfWriter(OutputFile))
{
var reader = new PdfReader("Templates//PDF_Template_Empty.pdf");
PdfDocument template = new PdfDocument(reader);
var titlepage = template.GetPage(1);
using (var pdf = new PdfDocument(writer))
{
pdf.AddPage(titlepage); // exception
But on .AddPage() it throws this exception :
iText.Kernel.PdfException: 'Page iText.Kernel.Pdf.PdfPage cannot be
added to document iText.Kernel.Pdf.PdfDocument, because it belongs to
document iText.Kernel.Pdf.PdfDocument.'
How can I fix this ?
A PDF page object usually has a number of related objects. If you only add the page itself to a new document and not the related objects, the resulting page would be incomplete.
Thus, iText 7 checks in AddPage whether the page in question has been created inside the target document or not, and in the latter case throws an exception to prevent missing dependent objects.
To copy pages across documents there is the PdfDocument method CopyPagesTo with many overloads. For you e.g.
PdfDocument template = new PdfDocument(reader);
using (var pdf = new PdfDocument(writer))
{
// copy template pages 1..1 to pdf as target page 1 onwards
template.CopyPagesTo(1, 1, pdf, 1);
}
(Beware, if there are extras on the page, you might want to choose an overload of that method which accepts an additional IPdfPageExtraCopier instance, e.g. for AcroForm fields a PdfPageFormCopier.)
I'm using IText7 version 7.0.2.2, I'm new with it, I'm trying to merge several pdfs at the same time into one that I'm uploading first, that is working fine, the problem is when I try dynamically to insert some text in one of the pdfs and then merge it, I'm using PdfWriter to write some content into the pdf and then try to merge it, but I'm getting this exception: 'Cannot copy indirect object from the document that is being written.
This is some of the code I'm using:
private byte[] MergePdfForms( HttpPostedFileBase firstPdf, List<SectionAndPdfs> sectionsAndPdf)
{
var dest = new MemoryStream();
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdf);
firstSourcePdf = new PdfDocument(new PdfReader(keyValuePair.Value), new PdfWriter(dest));
Document document = new Document(firstSourcePdf);
document.Add(new Paragraph(sectionsAndPdf[i].Key).SetBackgroundColor(iText.Kernel.Colors.Color.GRAY));
merger.Merge(firstSourcePdf, 1, subPages); //I'm getting the exception here..
firstSourcePdf.Close();
}
This is a known bug in the class PdfDestination. It was fixed, and will be present in our next release. At the moment you can of course use the snapshot release, which should solve the problem.
I' m using itextsharp to create pdf from template pdf file. But new pdf file is created like dynamic pdf. I want to convert this file to static pdf file, so i tried to use xfaworker. I get "Signature was corrupted" error from itextsharp.licensekey.dll. How can i use xfaworker or another dll for flatten dynamic pdf?
public string Create(FaxPDFModel model, MemoryStream ms)
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string templatePath = Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));
PdfReader pdfTemplate = new PdfReader(Path.Combine(Path.Combine(templatePath, "Docs"), "fax_template.pdf"));
PdfStamper stamper = new PdfStamper(pdfTemplate, ms);
stamper.Writer.CloseStream = false;
BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, "ISO-8859-9", BaseFont.EMBEDDED);
var acroFields = stamper.AcroFields;
acroFields.GenerateAppearances = true;
stamper.FormFlattening = true;
acroFields.AddSubstitutionFont(bf);
acroFields.SetField("Name", "Mutabakat test");
acroFields.SetField("Title", "DANIŞMANLIK");
acroFields.SetField("Department", "test");
acroFields.SetField("Phone", "0 (212) 555 55 55");
stamper.Close();
string path = Path.Combine(Path.Combine(templatePath, "Docs"), System.Guid.NewGuid().ToString());
string pdfPath = path + ".pdf";
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));
XFAFlattener xfaf = new XFAFlattener(document, writer);
ms.Position = 0;
xfaf.Flatten(new PdfReader(ms));
document.Close();
return pdfPath;
}
If you want to solve your problem, you will have to start by fixing the following errors:
1. You are using code to fill AcroForms instead of code to fill XFA forms:
If you have a dynamic XFA form, your PDF acts as a container for XML. This form doesn't expect data in the form of key value pairs. This form expects data stored as XML.
You can not use this code:
var acroFields = stamper.AcroFields;
acroFields.AddSubstitutionFont(bf);
acroFields.SetField("Name", "Mutabakat test");
acroFields.SetField("Title", "DANIŞMANLIK");
acroFields.SetField("Department", "test");
acroFields.SetField("Phone", "0 (212) 555 55 55");
This code expects that your form is an AcroForm. You need to fill the form like this:
AcroFields form = stamper.AcroFields;
XfaForm xfa = form.Xfa;
xfa.FillXfaForm(new FileStream(xml, FileMode.Open));
In this snippet xml refers to the data stored as XML.
2. You try to flatten the form before flattening the form:
I see this line in the first part of your code:
stamper.FormFlattening = true;
With this line, you remove all interactivity from your PDF. After closing the stamper object, you no longer have a form, hence the second part of your code will never work.
3. You are trying to embed a Standard Type 1 font:
This line doesn't make sense:
BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, "ISO-8859-9", BaseFont.EMBEDDED);
Times-Roman is a Standard Type 1 font (in the old days, we called this a Base14 font); iText never embeds any of the 14 Standard Type 1 fonts defined for PDF, hence the parameter BaseFont.EMBEDDED will be ignored.
4. The real error:
XFA Worker is a closed source addon for iTextSharp. It requires a valid license key. When you get an error saying "Signature was corrupted", you are using a license key that is corrupt.
Possible causes:
You are not a customer of iText Group. You are using a key that you found somewhere and you changed some of its contents.
You are a customer of iText Group. You received a key, but somehow it got tampered with. In the past, we've had a similar problem where a customer was reading the key as if it was encoded in EBCDIC. Please contact your account manager at iText Group for more info.