-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
99 lines (83 loc) · 2.92 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using RawPrint;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace PdfUnlocker
{
class Program
{
static void Main(string[] args)
{
//Microsoft Print to PDF
//foreach (string printerName in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
//{
// Console.WriteLine(printerName);
//}
//Console.ReadLine();
//return;
Console.WriteLine("Path to files:");
string originalFolder = Console.ReadLine();
string[] filePaths = Directory.GetFiles(originalFolder, "*.pdf", SearchOption.AllDirectories);
Parallel.ForEach(filePaths, (f) =>
{
string path = Path.GetDirectoryName(f);
string filename = Path.GetFileName(f);
ProcessFile(path, filename);
});
Console.WriteLine("Done! Key to exit.");
Console.ReadLine();
return;
}
private static void ProcessFile(string path, string filename)
{
PrintDoc(path, filename);
string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string sourceFileName = Path.Combine(
documents,
string.Join(".", new[] { GetUnlockedFileName(filename), "pdf" }));
string destFileName = Path.Combine(
path,
string.Join(".", new[] { GetUnlockedFileName(filename), "pdf" }));
int ok = 0;
while (ok < 360)
{
while (!File.Exists(sourceFileName) && ok < 360)
{
Thread.Sleep(1000);
ok++;
}
try
{
File.Move(sourceFileName, destFileName);
ok = 1000;
}
catch (Exception e)
{
ok++;
Thread.Sleep(1000);
}
}
if(ok != 1000)
{
Console.WriteLine("ERROR:" + destFileName);
}
else
{
Console.WriteLine("OK: " + destFileName);
}
}
private static void PrintDoc(string path, string filename)
{
// Absolute path to your PDF to print (with filename)
string Filepath = Path.Combine(path, filename);
string PrinterName = "PDFCreator";
IPrinter printer = new Printer();
printer.PrintRawFile(PrinterName, Filepath, GetUnlockedFileName(filename));
}
private static string GetUnlockedFileName(string filename)
{
return Path.GetFileNameWithoutExtension(filename) + "_unlocked";
}
}
}