PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Nov 13, 2018 4:46 am

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Jan 03, 2018 8:10 am 

Joined: Mon Nov 06, 2017 3:53 pm
Posts: 18
Hello everybody,

I came along a strange behaviour when saving a PDF with a UserPassword or OwnerPassword.

The protection itself works fine however the pdf outlines used as bookmarks are messed up afterwards.

The PDF is generated during runtime using the code from the wiki page ( CreateDocument() ) so I don't go further into that this post.

Here is my code for saving the PDF:


            Document document = CreateDocument();
            string mdddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document);
            MigraDoc.Rendering.PdfDocumentRenderer renderer = new MigraDoc.Rendering.PdfDocumentRenderer();
            renderer.Document = document;


            var pdfSharpDoc = renderer.PdfDocument;

            for (int pageCounter = 0; pageCounter < pdfSharpDoc.PageCount; pageCounter++)
                pdfSharpDoc.Outlines.Add("Page " + (pageCounter + 1), pdfSharpDoc.Pages[pageCounter]);

            //adding protection if wanted
            if (chkProtect.Checked == true)
                pdfSharpDoc.SecuritySettings.UserPassword = txtbPassword.Text;

            //Saving the PDF document

I attached two images showing the same document once saved with protection and one saved without.

Note that if I let the program generate the pdf with protection several times the characters shown in the outlines / bookmarks change.

I already tested:
- First set the protection and then setting the outlines
- Using OwnerPassword instead of UserPassword
- Using Owner and UserPassword
- Using the wiki page protect PDF using the PdfDocument document = PdfReader.Open(filenameDest, "some text"); and saving it again
but the result was the same.

Can someone explain this behaviour and give me a hint solving it?

Thank you very much!

MessedUpOutlines.JPG [ 17.44 KiB | Viewed 958 times ]
Outlines.JPG [ 12.89 KiB | Viewed 958 times ]
Reply with quote  
PostPosted: Wed Jan 03, 2018 11:54 am 
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 413
Location: CCAA
OVS wrote:
Can someone explain this behaviour and give me a hint solving it?
If the problem persists with the latest beta version, please use the Issue Submission Template to make it reproducible.

See also:

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

Reply with quote  
PostPosted: Wed Jan 03, 2018 12:16 pm 

Joined: Mon Nov 06, 2017 3:53 pm
Posts: 18
Thank you TH-Soft.

The update from beta 4 to beta 5 solved this issue!


Sorry to open this issue up again, but I have to specify that the issue is only solved when generating new PDFs. However if I open an existing protrected PDF and add a watermark and a footer and try to save it by using the same code


                pdfSharpDoc.SecuritySettings.DocumentSecurityLevel = PdfSharp.Pdf.Security.PdfDocumentSecurityLevel.Encrypted128Bit;
                pdfSharpDoc.SecuritySettings.UserPassword = txtbPassword.Text;
                pdfSharpDoc.SecuritySettings.OwnerPassword = txtbOwnerPassword.Text;

                pdfSharpDoc.SecuritySettings.PermitAccessibilityExtractContent = false;
                pdfSharpDoc.SecuritySettings.PermitAnnotations = false;
                pdfSharpDoc.SecuritySettings.PermitAssembleDocument = false;
                pdfSharpDoc.SecuritySettings.PermitExtractContent = false;
                pdfSharpDoc.SecuritySettings.PermitFormsFill = false;
                pdfSharpDoc.SecuritySettings.PermitFullQualityPrint = false;
                pdfSharpDoc.SecuritySettings.PermitModifyDocument = false;
                pdfSharpDoc.SecuritySettings.PermitPrint = false;


the outlines again are messed up the same way.

To add a watermark I used the code found in the wiki.

When I leave out the protection of a watermarked I get:

PrepareForSave: Number of deleted unreachable objects: 1
I think this is because the protected object is now no longer needed, and there for unreachable.
However in this case the outlines are fine.

Also if I create a not protected PDF, then watermark it and protect it, the outlines are fine too.

Ok I have found a workaround.

1) Open and read the file (IO) file with OwnerPassword.
2) Save the file immeadiately (Doc2)
3) Open this new Doc2 and watermark it
4) Save this file with UserPassword and OwnerPassword.

Now the outlines are fine. However I don't like the fact that the file is unprotected for a moment. Which means it is at some point vulnerable.
I try to solve this issue.

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

All times are UTC

Who is online

Users browsing this forum: No registered users and 8 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