Reputation: 157
I have to merge 2 pdfs forms into one. The input pdfs have fillable fields, and the output should also have the same fields. I was able to achieve this, but, when I merge PDFs with same value for fields, only the first field is coming aas a field and second one is flattened. (Lets say pdf 1 has fields 'Name' and 'Comment1'; pdf 2 has fields, 'Name' and 'Comment2'; when I merge, in the output pdf, 2nd 'Name' field is flattend.)
_stamp = new PdfStamper(_reader, pdfStream);
AcroFields fields = _stamp.AcroFields;
if (!(fields == null))
{
_stamp.FormFlattening = false;
}
_stamp.Close();
_stamp = null;
Upvotes: 0
Views: 514
Reputation: 77606
In your code, you are using PdfStamper
. That's a class to fill out forms, not to merge them. Merging forms is done using PdfCopy
:
public void createPdf(String filename, PdfReader[] readers) throws IOException, DocumentException {
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(filename));
copy.setMergeFields();
document.open();
for (PdfReader reader : readers) {
copy.addDocument(reader);
}
document.close();
for (PdfReader reader : readers) {
reader.close();
}
}
You can find the full code samples here. You'll have to adapt it to C# (the method names are slightly different, but the code is similar).
It is very important that you don't forget to tell PdfCopy
that you want to merge the fields, otherwise the form will not be copied.
You explain that you have a field named Name
in one PDF and a field named Name
in the other. If you merge both forms, this will result in a single field Name
with only one value. You can't have a field Name
on one page with one value and a field Name
on another page with another value. That's why we also provide a sample where the fields are renamed. You can find that example here. You probably don't need that example; I'm only adding it for the sake of completeness.
Upvotes: 1