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

Notification

Icon
Error

Options
Go to last post Go to first unread
ryanthomas03  
#1 Posted : Tuesday, October 15, 2019 10:36:01 AM(UTC)
ryanthomas03

Rank: Newbie

Groups: Registered
Joined: 9/5/2019(UTC)
Posts: 3
United States

We've been testing Pdfium.Net.SDK has a replacement library for our pdf manipulation needs. One of the blocking problem's we've encountered is that we are unable to use the library to flatten a document.
The below code snippet highlights the problem.

We've tested using version 4.18.2704 as well as version 4.19.2704 which was just released.

Code:

public static byte[] FlattenPdf(byte[] pdfBytes)
{
    byte[] flattenedPdfBytes = null;
    PdfForms pdfForms = new PdfForms();
    using (var pdfDocument = PdfDocument.Load(pdfDocumentBytes, pdfForms))
    {
        var hasFields = pdfForms.InterForm?.Fields?.Count > 0; // true


        foreach (var nextPage in pdfDocument.Pages)
        {
            nextPage.FlattenPage(FlattenFlags.NormalDisplay);
        }

        using (var flattenedPdfMemoryStream = new MemoryStream())
        {
            pdfDocument.Save(flattenedPdfMemoryStream, SaveFlags.NoIncremental);
            flattenedpdfBytes = flattenedPdfMemoryStream.ToArray();
        }
    }

    PdfForms pdfForms2 = new PdfForms();
    using (var flattenedPdfDocument = PdfDocument.Load(flattenedPdfBytes, pdfForms2))
    {
        var stillHasFields = pdfForms2.InterForm?.Fields?.Count > 0; // still true, when we expect it to be false
    }

    return flattenedPdfBytes;
}


We've tried a variety of additional calls and options:
Code:
PdfForms.ForceToKillFocus(), SaveFlags.RemoveUnusedObjects, etc


But we're unable to truly flatten a document. This is an important feature for us, once we populate a document's fields we need to ensure it is flattened before we store the populated pdf.

Thanks,
Ryan
Paul Rayman  
#2 Posted : Wednesday, October 23, 2019 12:41:52 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 2 times
Was thanked: 103 time(s) in 101 post(s)
You may call
Code:
pdfDocument.Root.Remove("AcroForm");

after you flatten the entire document
Code:
foreach (var nextPage in pdfDocument.Pages)
{
    nextPage.FlattenPage(FlattenFlags.NormalDisplay);
}
pdfDocument.Root.Remove("AcroForm");


My understanding of the situation is this:
When you flatten a separate page, the processed field on this page cannot be deleted from the "AcroForms" dictionary because other pages may still reffers to this field. Iterate all the pages in the search for references to each specific flattened field would be too not optimal. That's why when a page is flattened, the fields are removed only from the "Annots" array, which belongs only to a specific page.

However, if you flatten all pages of a document, then you can delete the global field's dictionary too.

Edited by user Wednesday, October 23, 2019 1:01:50 AM(UTC)  | Reason: Not specified

ryanthomas03  
#3 Posted : Monday, October 28, 2019 9:46:03 AM(UTC)
ryanthomas03

Rank: Newbie

Groups: Registered
Joined: 9/5/2019(UTC)
Posts: 3
United States

Hi Paul,

We tried removing the acro form dictionary from the catalog, after flattening all pages. The result we got in the output pdf was that all of the page annotations that should have been populated with values from the fields and then flattened, no longer displayed a value if we removed the acrobat form.
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.