PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 10:06 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Sep 06, 2016 10:02 am 
Offline

Joined: Mon Sep 05, 2016 7:54 am
Posts: 22
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:

Code:
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 09, 2019 11:32 am 
Offline

Joined: Tue Apr 09, 2019 11:17 am
Posts: 1
Hi,
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,
Andreas


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 03, 2019 9:59 pm 
Offline

Joined: Thu Oct 03, 2019 9:56 pm
Posts: 1
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;
}

pdfDocument.Save(memoryStream);
}
}


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 145 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group