PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Nov 08, 2024 11:36 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Feb 08, 2023 1:42 pm 
Offline

Joined: Wed Feb 08, 2023 12:43 pm
Posts: 3
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 08, 2023 2:35 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1007
Location: CCAA
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?

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 09, 2023 9:08 am 
Offline

Joined: Wed Feb 08, 2023 12:43 pm
Posts: 3
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.


Top
 Profile  
Reply with quote  
PostPosted: Sat Feb 11, 2023 2:51 pm 
Offline

Joined: Tue Sep 30, 2014 12:29 pm
Posts: 36
Untested, but this should do it:
Code:
pdfSharpDoc.Internals.Catalog.Elements.Remove("/AcroForm");


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 14, 2023 9:59 am 
Offline

Joined: Wed Feb 08, 2023 12:43 pm
Posts: 3
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");


Top
 Profile  
Reply with quote  
PostPosted: Thu May 18, 2023 7:31 pm 
Offline

Joined: Thu May 18, 2023 7:09 pm
Posts: 1
(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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group