PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Help with removing all AcroForm from a pdf
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4415
Page 1 of 1

Author:  zoorinz [ Wed Feb 08, 2023 1:42 pm ]
Post subject:  Help with removing all AcroForm from a pdf

Hey, General I am new to working with pdf, and specially on this metadata level, so bear with me.
We uses PdfSharpCore 1.3.43 (I know this is a port of the PdfSharp lib, but if it is doable is your lib, I can probably transfer it to this one).

Because of another 3rd party library, we need to purge certains elements from pdf documents, that is not part of a specific whitelisted set. For now these are:
- /Pages/DefaultValue
- /AcroForm/ -> this one always contains a single "/Fields" that is an empty array

Example of the structure of one of the documents
Image

The "/Pages/DefaultValue" was easy to remove. But I dont know how to completely remove the "AcroForm" completely from the pdf?
- My initial thought was to remove alle the attributes under the "/AcroForm/", which only contains an empty array "/Fields", but that didnt work.

Any ideas of how to achieve this in PDFsharp?

Code I am working with so far:
Code:
PdfDocument pdfSharpDoc = PdfReader.Open(memoryStream, PdfDocumentOpenMode.Modify,
PdfSharpCore.Pdf.IO.enums.PdfReadAccuracy.Moderate);

pdfSharpDoc.Pages.Elements.Remove("/DefaultValue");
pdfSharpDoc.AcroForm.Elements.Remove("/Fields"); // Doesnt work, removes the element but doesnt remove the AcroForm from the the file.
PdfCustomValues.ClearAllCustomValues(pdfSharpDoc);


There seems to be a fork of the PDFsharp nucketpackage that has implemented this in a Flattern method, but we cannot use the .NetFramwork lib.
https://github.com/packdat/PDFsharp/blo ... nt.cs#L905

Thanks in advance.

Author:  TH-Soft [ Wed Feb 08, 2023 2:35 pm ]
Post subject:  Re: Help with removing all AcroForm from a pdf

zoorinz wrote:
There seems to be a fork of the PDFsharp nucketpackage that has implemented this in a Flattern method, but we cannot use the .NetFramwork lib.
Thanks for the link.
PDFsharp always uses the .NET Framework, so I do not understand what you mean with "we cannot use the .NetFramwork lib".
It seems you did not try their "Flatten()" implementation, right?

Author:  zoorinz [ Thu Feb 09, 2023 9:08 am ]
Post subject:  Re: Help with removing all AcroForm from a pdf

Sorry I wasn't fully clear in my post.
We are running a .net Core project on linux services, and we only have limited .NetFramework support (through a web api to a windows server), hence unfortunately cannot use this library directly. Instead we are using a .net Core port PdfSharpCore v. 1.3.43. This port seems pretty close in most cases, and have been able to do the small requirement we have had for it so far.

So I was hoping that you guys could help with, how this would be achieved in PDFsharp, by removing the AcroFrom attributes from a pdf (if it is possible with this library). Then I could hopefully do something similar in the PdfSharpCore library.

Author:  (void) [ Sat Feb 11, 2023 2:51 pm ]
Post subject:  Re: Help with removing all AcroForm from a pdf

Untested, but this should do it:
Code:
pdfSharpDoc.Internals.Catalog.Elements.Remove("/AcroForm");

Author:  zoorinz [ Tue Feb 14, 2023 9:59 am ]
Post subject:  Re: Help with removing all AcroForm from a pdf

Thanks a lot void, that was the missing info I needed :)

Initial directly removal of the "/AcroForm" didn't work, but it does after I have removed all its elements (in my case there is only the "/Fields").

Code:
pdfSharpDoc.AcroForm.Elements.Remove("/Fields"); // Required for the removal of the "/AcroForm" to work
pdfSharpDoc.Internals.Catalog.Elements.Remove("/AcroForm");

Author:  laureljohnson [ Thu May 18, 2023 7:31 pm ]
Post subject:  Re: Help with removing all AcroForm from a pdf

(void) wrote:
Untested, but this should do it:
Code:
pdfSharpDoc.Internals.Catalog.Elements.Remove("/AcroForm");


Hello,
I was having the same problem and your code worked, thank you.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/