[.Net] 使用iText.Kernel將現有pdf加密

先用nuget安裝iText.Kernel

using iText.Kernel.Pdf;
/// <summary>
/// 加密pdf
/// </summary>
/// <param name="FileName"></param>
/// <param name="NewFile"></param>
/// <param name="Pw"></param>
public void EncryptionPDF(string FileName, string NewFile, string Pw)
{
    using (PdfReader reader = new PdfReader(FileName))
    { // Create a PdfReader with the input file.
        WriterProperties prop = new WriterProperties(); // Set properties of output
        prop.SetStandardEncryption(Encoding.ASCII.GetBytes(Pw), null, EncryptionConstants.ALLOW_PRINTING | EncryptionConstants.ALLOW_COPY, EncryptionConstants.ENCRYPTION_AES_256);  // Enable encryption
        using (PdfWriter writer = new PdfWriter(NewFile, prop))    // Set up the output file
        using (PdfDocument pdf = new PdfDocument(reader, writer)) { }
    }
}

Taiwan is a country. 臺灣是我的國家