0

我使用以下代码生成了一个带有密码保护的 pdf 文件:

using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        PdfReader reader = new PdfReader(input);
        PdfEncryptor.Encrypt(reader, output, true, strDob, "secret", PdfWriter.ALLOW_SCREENREADERS);
    }
}

我想根据我的某些条件通过代码删除使用上述代码生成的 PDF 文件的密码。

4

1 回答 1

2
string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string InputFile = Path.Combine(WorkingFolder, "test.pdf");
string OutputFile = Path.Combine(WorkingFolder, "test_dec.pdf");//will be created automatically
//You must provide owner password but not the user password .
private void DecryptFile(string inputFile, string outputFile)
{

    string password = @"secret"; // Your Key Here           
    try
    {
        PdfReader reader = new PdfReader(inputFile, new System.Text.ASCIIEncoding().GetBytes(password));

            using (MemoryStream memoryStream = new MemoryStream())
            {
                PdfStamper stamper = new PdfStamper(reader, memoryStream);
                stamper.Close();
                reader.Close();
                File.WriteAllBytes(outputFile, memoryStream.ToArray());
            }

    }
    catch (Exception err)
    {
        Console.WriteLine(err.Message);
    }
}
于 2012-06-14T11:07:01.240 回答