PDFsharp & MigraDoc Foundation

Is there a way to fully flatten the AcroForm in a PDF?
Page 1 of 1

Author:  RBrNx [ Tue Sep 06, 2016 10:02 am ]
Post subject:  Is there a way to fully flatten the AcroForm in a PDF?

Hi there, I am having a problem with the PDF program that I am currently writing. If I try to merge two PDFs that happen to use the same AcroField names, then the Fields in the 2nd PDF will simply become blank. I am currently using the following method to 'flatten' the PDFs:

if (Flatten) {
PdfAcroField.PdfAcroFieldCollection fields = inForm.Fields;
string[] names = fields.Names;

for (int idx = 0; idx < names.Length; ++idx)
  string fqName = names[idx];
  PdfAcroField field = fields[fqName];
  PdfTextField textField;

  if ((textField = field as PdfTextField) != null)
      textField.ReadOnly = true;

This works fine, in that it makes the Fields unable to be edited in Acrobat Reader. However it seems that the forms still exist as they can be edited in Adobe Acrobat, or by loading the PDF again with PDFSharp. I'd seen that a solution was in the works here: viewtopic.php?f=2&t=3369 but this doesn't seem to have made it over to the 1.50.4000-Beta3B release yet. I tried following the patch tutorial, but the Source Code would not build after I tried it. Is there any way to completely flatten the AcroFields at this time?

Alternatively I had another idea to solve the problem in the meantime. I could rename all of the fields present in the PDF to some randomly generated 16 character name, like "ALVDrrLAWUawTxUh" or "ZDJrQdfwGLL3DDz5". This would mean that any PDF saved by my program would be extremely unlikely to ever have the same Field names, meaning that they would not overwrite each other. Is it possible to rename an AcroField?

Thanks, RBrNx

Author:  ichbins [ Tue Apr 09, 2019 11:32 am ]
Post subject:  Re: Is there a way to fully flatten the AcroForm in a PDF?

the original post is 2,5 years old, but I have the same problem. I want to concat two or more PDF files of the same type (same form, same fields, same field names, different values in the fields). The fields in the second or later file just become blank in the merged document. The result doesn't need to be a form anymore - just the values have to be at the right place. Is there any solution? Did the trick with renaming the fields work? Any other workaround?

Thanks in advance,

Author:  storres [ Thu Oct 03, 2019 9:59 pm ]
Post subject:  Re: Is there a way to fully flatten the AcroForm in a PDF?

You can use something like this to rename the fields of each document.

using (var pdfDocument = PdfReader.Open(sourceStream, PdfDocumentOpenMode.Modify))
if (pdfDocument.AcroForm != null)
var uniqueIndex = Guid.NewGuid();
var fields = pdfDocument.AcroForm.Fields;
var fieldNames = fields.Names;

for (int idx = 0; idx < fieldNames.Length; ++idx)
var fieldName = fieldNames[idx];
var field = fields[fieldName];

field.Elements.SetName($"/{fieldName}", $"{fieldName}_{uniqueIndex}");
field.ReadOnly = true;


Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group