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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants