Rank: Newbie
Groups: Registered
Joined: 9/5/2019(UTC) Posts: 3 
|
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
|
|
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,138
Thanks: 10 times Was thanked: 133 time(s) in 130 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 Tuesday, October 22, 2019 10:01:50 PM(UTC)
| Reason: Not specified
|
|
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 9/5/2019(UTC) Posts: 3 
|
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.
|
|
|
|
|
|
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.
Important Information:
The Patagames Software Support Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close