Skip to content

Conversation

@leppie
Copy link

@leppie leppie commented Feb 18, 2022

Hex literal strings werent getting encrypted so it would break images with indexed colorspace.

Sample program:

using System;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

class Program
{
    static void Main()
    {
        var ts = (DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds;

        using (var targetDoc = new PdfDocument())
        {
            using (var src = PdfReader.Open("test.pdf", PdfDocumentOpenMode.Import))
            {
                for (var i = 0; i < src.PageCount; i++)
                {
                    targetDoc.AddPage(src.Pages[i]);
                }
            }

            targetDoc.Save($"out1-{ts:F0}.pdf");

            targetDoc.SecuritySettings.UserPassword = "1234";

            targetDoc.Save($"out2-{ts:F0}.pdf");
        }

        // round trip test
        using (var targetDoc = new PdfDocument())
        {
            using (var src = PdfReader.Open($"out2-{ts:F0}.pdf", "1234", PdfDocumentOpenMode.Import))
            {
                for (var i = 0; i < src.PageCount; i++)
                {
                    targetDoc.AddPage(src.Pages[i]);
                }
            }

            targetDoc.Save($"out5-{ts:F0}.pdf");
        }
    }
}

PDF used for testing test.pdf

Output would become this without this PR: out2-1645190757122.pdf (password 1234)

@leppie
Copy link
Author

leppie commented Feb 18, 2022

Argghhh, it added more commit, that was unintended. Will see if I can fix.

@leppie
Copy link
Author

leppie commented Feb 18, 2022

Created one from a non master branch empira/PDFsharp#162

@leppie leppie closed this Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant