添加密码

时间:2018-02-11 02:00:59

标签: c# pdf pdfsharp

我正在尝试获取已经制作的pdf文件,并使用密码保护它。代码成功添加了密码保护,但生成的pdf中的所有页面都是空白的。问题不是特定于pdf文件,我使用的任何pdf文件都有相同的结果。

我正在使用PDFSharp 1.50.4740-beta5(我已经尝试了几个以前的版本,但它们也有同样的问题。我不能在1.5之前使用任何东西,因为之前的版本不是支持Adobe 6。

奇怪的是,在我不得不升级之前,我可以毫无问题地在PDFSharp 1.32中执行此操作。任何人都可以协助,或指出我正确的方向吗?

我尝试过的示例PDF:http://www.africau.edu/images/default/sample.pdf

代码来自此处提供的示例:http://www.pdfsharp.net/wiki/ProtectDocument-sample.ashx?HL=password

// Open an existing document. Providing an unrequired password is ignored.
        PdfDocument document = PdfReader.Open(@"filelocation1", "sometext");


        PdfSecuritySettings securitySettings = document.SecuritySettings;

        securitySettings.UserPassword = "test";
        securitySettings.OwnerPassword = "test";

        securitySettings.PermitAccessibilityExtractContent = false;
        securitySettings.PermitAnnotations = false;
        securitySettings.PermitAssembleDocument = false;
        securitySettings.PermitExtractContent = false;
        securitySettings.PermitFormsFill = true;
        securitySettings.PermitFullQualityPrint = false;
        securitySettings.PermitModifyDocument = true;
        securitySettings.PermitPrint = false;


        // Setting one of the passwords automatically sets the security level to 
        // PdfDocumentSecurityLevel.Encrypted128Bit.
        //securitySettings.UserPassword = "test";

        // Don't use 40 bit encryption unless needed for compatibility reasons
        //securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit;


        // Save the document...
        document.Save(@"filelocation2");

        document.Close();

1 个答案:

答案 0 :(得分:0)

@Viveladéraison解决了我的问题。我不得不将几个版本回滚到.4589以解决问题。