PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Protecting PDF messes up PDF outlines.
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3706
Page 1 of 1

Author:  OVS [ Wed Jan 03, 2018 8:10 am ]
Post subject:  Protecting PDF messes up PDF outlines.

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:

Code:

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

            renderer.RenderDocument();

            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
            pdfSharpDoc.Save(filename);


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!

Attachments:
MessedUpOutlines.JPG
MessedUpOutlines.JPG [ 17.44 KiB | Viewed 3760 times ]
Outlines.JPG
Outlines.JPG [ 12.89 KiB | Viewed 3760 times ]

Author:  TH-Soft [ Wed Jan 03, 2018 11:54 am ]
Post subject:  Re: Protecting PDF messes up PDF outlines.

Hi!
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:
viewtopic.php?f=2&t=832

Author:  OVS [ Wed Jan 03, 2018 12:16 pm ]
Post subject:  Re: Protecting PDF messes up PDF outlines.

Thank you TH-Soft.

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


Edit:

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

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;


                pdfSharpDoc.Save(filename);


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:

Quote:
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.

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