Password protecting a PDF file

后端 未结 1 516
执笔经年
执笔经年 2020-12-29 11:00

I have the following:

  • routine X that creates a PDF file on a daily basis.
  • routine Y that attaches this file to an Outlook e-mail and sends it to recip
1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 11:24

    PDFSharp should be able to protect a PDF file with a password:

    // Open an existing document. Providing an unrequired password is ignored.
    PdfDocument document = PdfReader.Open(filename, "some text");
    
    PdfSecuritySettings securitySettings = document.SecuritySettings;
    
    // Setting one of the passwords automatically sets the security level to 
    // PdfDocumentSecurityLevel.Encrypted128Bit.
    securitySettings.UserPassword  = "user";
    securitySettings.OwnerPassword = "owner";
    
    // Don't use 40 bit encryption unless needed for compatibility reasons
    //securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit;
    
    // Restrict some rights.
    securitySettings.PermitAccessibilityExtractContent = false;
    securitySettings.PermitAnnotations = false;
    securitySettings.PermitAssembleDocument = false;
    securitySettings.PermitExtractContent = false;
    securitySettings.PermitFormsFill = true;
    securitySettings.PermitFullQualityPrint = false;
    securitySettings.PermitModifyDocument = true;
    securitySettings.PermitPrint = false;
    
    // Save the document...
    document.Save(filename);
    

    Reference:
    http://www.pdfsharp.net/wiki/ProtectDocument-sample.ashx

    0 讨论(0)
提交回复
热议问题