Unfortunately, when calling AddDocument the InteractiveForm (specifically InteractiveForm.Fields) in the parent document is not updated. I found a workaround for this using an image viewer, adding the child pages and calling SaveSync after each document is concatenated, but we're doing this as a batch process in the background and it's too slow for production. Is there something we can do to get the InteractiveForm to update after calling AddDocument.
Rough Example Code off the top of my head:
Code: Select all
private void ConcatenateDocuments(string parentDocumentPath, List<string> childDocumentPaths)
{
PdfDocument parentDocument = new PdfDocument(parentDocumentPath);
foreach (string childDocumentPath in childDocumentPaths)
{
PdfDocument childDocument = new PdfDocument(childDocumentPath);
foreach (FormField field in childDocument.InteractiveForm.Fields)
{
// The problem happens here because the parentDocument.InteractiveForm does not get updated when we
// add the second document, so the check doesn't behave when the third document and on are added.
if (parentDocument.InteractiveForm.Fields.Contains(e => e.Name == field.Name))
{
// Rename the child fields before adding them. Note that what we're doing is a little more complicated
// than this because we need to make sure the renamed value doesn't exist in the parent as well, but
// this is close enough to explain what we're trying to do. If _?2 exists, we use _?3 and so on.
field.Name = field.Name + _?2;
}
}
parentDocument.AddDocument(childDocument);
}
}
Thanks!