logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
claudiu  
#1 Posted : Monday, July 15, 2019 1:50:14 PM(UTC)
claudiu

Rank: Newbie

Groups: Registered
Joined: 6/6/2019(UTC)
Posts: 4
Canada

Thanks: 2 times
Hello,

I have multiple documents with signature fields and my requirement is to merge the documents into one file before it is sent to signing. My problem is that after merge, my final document does not have the signature boxes. The merging is done by creating a new document and using ImportPages to concatenate the documents:

Code:

                var mergedPdf = PdfDocument.CreateNew();
                int bookmarkIndex = 0;
                foreach (var d in docs)
                {
                    var pass = CipherHelper.Decrypt<RijndaelManaged>(d.PASSWORD, ConfigHelper.Secret, ConfigHelper.Salt);
                    var tmpDoc = PdfDocument.Load(d.CONTENT,null, pass);
                    int index = mergedPdf.Pages.Count;
                    mergedPdf.Pages.ImportPages(tmpDoc, null, index);
                    if (mergedPdf.Pages.Count > 0)
                    {
                        mergedPdf.Bookmarks.InsertAt(bookmarkIndex, d.FILENAME, mergedPdf.Pages[index]);
                        bookmarkIndex++;
                    }
                }
 


Is there any way to fix this issue or an alternative merging method?

Thank You!
Paul Rayman  
#2 Posted : Tuesday, July 16, 2019 11:11:38 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 779

Thanks: 1 times
Was thanked: 97 time(s) in 95 post(s)
This happens because the AcroForm dictionary is not imported.
You can import this dictionary yourself. For example, like this:

Usage
Code:
var mergedPdf = PdfDocument.CreateNew();
var tmpDoc = PdfDocument.Load(@"e:\11\test.pdf");
mergedPdf.Pages.ImportPages(tmpDoc, null, 0);
ImportAcroForm(tmpDoc, mergedPdf);
mergedPdf.Save(@"e:\11\signew.pdf", Patagames.Pdf.Enums.SaveFlags.NoIncremental);


and ImportAcroForm
Code:
private void ImportAcroForm(PdfDocument srcDoc, PdfDocument dstDoc)
{
    if (!srcDoc.Root.ContainsKey("AcroForm"))
        return;

    var list = PdfIndirectList.FromPdfDocument(dstDoc);
    PdfTypeDictionary acroForm = srcDoc.Root["AcroForm"].As<PdfTypeDictionary>().Clone() as PdfTypeDictionary;
    list.Add(acroForm);
    dstDoc.Root.SetIndirectAt("AcroForm", list, acroForm);

    if (acroForm.ContainsKey("Fields"))
        acroForm["Fields"].As<PdfTypeArray>().Clear();
    else
        acroForm["Fields"] = PdfTypeArray.Create();

    foreach (var page in dstDoc.Pages)
    {
        if (page.Annots == null)
            continue;
        foreach(var annot in page.Annots)
        {
            if (annot is PdfWidgetAnnotation)
                acroForm["Fields"].As<PdfTypeArray>().AddIndirect(list, annot.Dictionary.ObjectNumber);
        }
    }
}


The correct structure of the document should be like in the image below. The code forms such a structure by cloning the AcroForm dictionary and by re-forms the array of fields with actual object numbers.

111.png (144kb) downloaded 7 time(s).
thanks 1 user thanked Paul Rayman for this useful post.
claudiu on 7/17/2019(UTC)
claudiu  
#3 Posted : Wednesday, July 17, 2019 6:58:40 PM(UTC)
claudiu

Rank: Newbie

Groups: Registered
Joined: 6/6/2019(UTC)
Posts: 4
Canada

Thanks: 2 times
Thanks Paul, it works a treat!
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.