-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
60 lines (51 loc) · 1.98 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using iText.Kernel.Pdf;
using System.IO;
namespace PdfPasswordRemover
{
class Program
{
static void Main(string[] args)
{
// Change the values to the right ones.
// The path where the original PDFs are located.
string mainPath = @"c:\pathToPDFsWithPassword";
// The path where do you want to save the decrypted PDFs. I use a subfolder in the same path.
string decryptedFilesPath = Path.Combine(mainPath, "NowDecrypted");
// The password of the PDFs.
var password = "MySuperSecretPassword";
// Start the process.
var files = Directory.GetFiles(mainPath, "*.pdf");
var pdfManipulator = new PdfManipulation();
foreach (var file in files)
{
var dest = Path.Combine(decryptedFilesPath, Path.GetFileName(file));
pdfManipulator.ManipulatePdf(file, dest, password);
}
}
public class PdfManipulation
{
public void ManipulatePdf(string src, string dest, string password)
{
var passwordBytes = new System.Text.ASCIIEncoding().GetBytes(password);
var reader = new MyReader(src, passwordBytes);
reader.SetUnethicalReading(true);
reader.DecryptOnPurpose();
if (!Directory.Exists(Path.GetDirectoryName(dest)))
{
Directory.CreateDirectory(Path.GetDirectoryName(dest));
}
var pdfDoc = new PdfDocument(reader, new PdfWriter(dest));
pdfDoc.Close();
reader.Close();
}
}
class MyReader : PdfReader
{
public MyReader(string filename, byte[] passwordBytes) : base(filename, new ReaderProperties().SetPassword(passwordBytes)) { }
public void DecryptOnPurpose()
{
encrypted = false;
}
}
}
}