如何通过使用c#提供文件的密码作为参数来解密pdf文件? [英] How to decrypt a pdf file by supplying password of the file as argument using c#?
本文介绍了如何通过使用c#提供文件的密码作为参数来解密pdf文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码生成了一个带密码保护的pdf文件:
I have generated a pdf file with password protection by using the following code:
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文件的密码通过代码使用基于我的某些条件的上述代码。
I want to remove the password for the PDF file generated using the above code based on my certain condtions through code.
推荐答案
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);
}
}
这篇关于如何通过使用c#提供文件的密码作为参数来解密pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文