-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathCli.cs
More file actions
813 lines (757 loc) · 38.6 KB
/
Copy pathCli.cs
File metadata and controls
813 lines (757 loc) · 38.6 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Printing;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Docnet.Core;
using Docnet.Core.Models;
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;
using KillerPDF.Services;
namespace KillerPDF
{
// ============================================================
// Command-line interface
// ============================================================
//
// Dispatcher for every headless CLI command. Invoked from App.OnStartup
// BEFORE the single-instance mutex, so CLI runs work while a GUI instance
// is open, never forward to it, and never show a window. A launch with no
// recognized command flag falls through to the normal GUI (including the
// classic "KillerPDF.exe file.pdf" file-association open).
//
// Each command reuses the same pipeline its GUI equivalent runs - the
// merge named-destination rewrite, the pre-save scrubs, the PDFium
// decrypt, the rotation-safe rasterizer, the OCR text-layer builder - so
// CLI output is byte-for-byte the kind of file the GUI would produce.
//
// Exit codes: 0 = success, 1 = operation failed, 2 = bad usage.
//
// Console output rides on AttachConsole (GUI-subsystem exe, see
// BatchMode.cs); lines can interleave with the shell prompt. Exit codes
// are the scripting contract.
public partial class MainWindow
{
// Options that consume the next argument as their value.
private static readonly string[] CliValueOptions =
[
"--log", "--dpi", "--format", "--pages", "--printer", "--lang", "--password", "--copies",
];
/// <summary>
/// Entry point for all CLI commands. Returns false when args carry no
/// recognized command (normal GUI launch); otherwise runs the command
/// and returns true with the process exit code set.
/// </summary>
internal static bool TryRunCli(string[] args, out int exitCode)
{
exitCode = 0;
if (args is null || args.Length == 0) return false;
// The validation resave keeps its dedicated runner in BatchMode.cs.
if (args.Any(a => Eq(a, "--batch-resave")))
return TryRunBatch(args, out exitCode);
string? command = args.FirstOrDefault(a =>
Eq(a, "--help") || Eq(a, "-h") || Eq(a, "/?") ||
Eq(a, "--version") || Eq(a, "-v") ||
Eq(a, "--merge") || Eq(a, "--extract-pages") || Eq(a, "--split") ||
Eq(a, "--decrypt") || Eq(a, "--to-image") || Eq(a, "--flatten") ||
Eq(a, "--print") || Eq(a, "--ocr"));
if (command is null) return false;
var con = OpenBatchConsole();
var (positionals, options) = ParseCliArgs(args, command);
try
{
switch (command.ToLowerInvariant())
{
case "--help":
case "-h":
case "/?":
con.WriteLine(CliHelpText());
break;
case "--version":
case "-v":
con.WriteLine(Assembly.GetExecutingAssembly().GetName().Version?.ToString(3) ?? "unknown");
break;
case "--merge":
exitCode = CliMerge(positionals, con);
break;
case "--extract-pages":
exitCode = CliExtractPages(positionals, con);
break;
case "--split":
exitCode = CliSplit(positionals, con);
break;
case "--decrypt":
exitCode = CliDecrypt(positionals, options, con);
break;
case "--to-image":
exitCode = CliToImage(positionals, options, con);
break;
case "--flatten":
exitCode = CliFlatten(positionals, options, con);
break;
case "--print":
exitCode = CliPrint(positionals, options, con);
break;
case "--ocr":
exitCode = CliOcr(positionals, options, con);
break;
}
}
catch (Exception ex)
{
con.WriteLine("Error: " + FlattenBatchDetail(ex.Message));
exitCode = 1;
}
finally
{
App.CleanupSessionTemps(); // drop any decrypt/rotation temps the run created
}
return true;
}
private static bool Eq(string a, string b) =>
string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
private static string CliHelpText() => string.Join(Environment.NewLine,
[
"KillerPDF " + (Assembly.GetExecutingAssembly().GetName().Version?.ToString(3) ?? "") + " - command line usage",
"",
" KillerPDF.exe <file.pdf> open in the app",
" KillerPDF.exe --version | -v print version",
" KillerPDF.exe --help | -h | /? this text",
"",
" --merge <out.pdf> <in1> <in2> ... merge PDFs (and images) into one PDF",
" --extract-pages <in.pdf> <pages> <out.pdf>",
" pull pages into a new PDF (pages like 1-3,5,9-12)",
" --split <in.pdf> <outDir> write one PDF per page",
" --decrypt <in.pdf> <out.pdf> [--password <p>]",
" remove encryption (lossless when possible)",
" --to-image <in.pdf> <outDir> [--dpi <n>] [--format png|jpg] [--pages <range>] [--transparent]",
" render pages to images (default 150 dpi, png;",
" background composites to white unless --transparent, png only)",
" --flatten <in.pdf> <out.pdf> [--dpi <n>] rasterize into an uneditable PDF (default 150 dpi)",
" --print <in.pdf> [--printer <name>] [--pages <range>] [--copies <n>]",
" print silently (default printer if none named)",
" --ocr <in.pdf> <out.pdf> [--lang <code>] add an invisible searchable text layer (default eng;",
" other languages download on first use)",
" --batch-resave <in> <out> [--log <f.csv>] [--quiet]",
" resave a file or tree through the standard",
" open/save pipeline (validation harness)",
"",
"Exit codes: 0 success, 1 operation failed, 2 bad usage.",
"Runs headless and works while the KillerPDF window is open.",
]);
/// <summary>
/// Splits args into positionals (everything after the command flag that
/// is not an option) and an option dictionary (case-insensitive keys).
/// </summary>
private static (List<string> Positionals, Dictionary<string, string> Options)
ParseCliArgs(string[] args, string command)
{
var positionals = new List<string>();
var options = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
int start = Array.FindIndex(args, a => Eq(a, command)) + 1;
for (int i = start; i < args.Length; i++)
{
var a = args[i];
if (a.StartsWith("--", StringComparison.Ordinal))
{
if (CliValueOptions.Any(o => Eq(o, a)) && i + 1 < args.Length)
options[a] = args[++i];
else
options[a] = string.Empty;
}
else
{
positionals.Add(a);
}
}
return (positionals, options);
}
/// <summary>
/// Parses a 1-based page range spec like "1-3,5,9-12" into sorted,
/// distinct 0-based indices. Returns null with a message in error when
/// the spec is malformed or out of range.
/// </summary>
private static List<int>? CliParsePageRange(string spec, int pageCount, out string error)
{
error = string.Empty;
var pages = new SortedSet<int>();
foreach (var rawPart in spec.Split(','))
{
var part = rawPart.Trim();
if (part.Length == 0) continue;
int a, b;
int dash = part.IndexOf('-');
if (dash > 0)
{
if (!int.TryParse(part[..dash].Trim(), out a) ||
!int.TryParse(part[(dash + 1)..].Trim(), out b))
{ error = $"Bad page range: \"{part}\""; return null; }
}
else
{
if (!int.TryParse(part, out a)) { error = $"Bad page number: \"{part}\""; return null; }
b = a;
}
if (a > b) (a, b) = (b, a);
if (a < 1 || b > pageCount)
{ error = $"Pages {part} out of range - the document has {pageCount} pages"; return null; }
for (int p = a; p <= b; p++) pages.Add(p - 1);
}
if (pages.Count == 0) { error = "Empty page range"; return null; }
return [.. pages];
}
// ============================================================
// --merge <out.pdf> <in1> <in2> ...
// ============================================================
// Mirrors the GUI merge (FileOperations.cs Merge_Click): per source PDF,
// harvest named destinations from a ReadOnly open, copy pages from an
// Import open, then rewrite named-destination links against the page
// offset. Image inputs go through the same importer the GUI drop
// pipeline uses (ImportAndZip.cs).
private static int CliMerge(List<string> pos, TextWriter con)
{
if (pos.Count < 3)
{
con.WriteLine("Usage: KillerPDF.exe --merge <out.pdf> <in1.pdf> <in2.pdf> ...");
return 2;
}
string outPath = Path.GetFullPath(pos[0]);
var inputs = pos.Skip(1).Select(Path.GetFullPath).ToList();
foreach (var f in inputs)
{
if (!File.Exists(f)) { con.WriteLine($"Input not found: {f}"); return 2; }
if (string.Equals(f, outPath, StringComparison.OrdinalIgnoreCase))
{ con.WriteLine("Output file cannot also be an input."); return 2; }
}
using var outPdf = new PdfDocument();
foreach (var f in inputs)
{
if (IsPdfPath(f))
{
int pageOffset = outPdf.PageCount;
Dictionary<string, int> namedDestMap;
using (var srcRead = PdfReader.Open(f, PdfDocumentOpenMode.ReadOnly))
namedDestMap = BuildNamedDestMap(srcRead);
using var src = PdfReader.Open(f, PdfDocumentOpenMode.Import);
for (int i = 0; i < src.PageCount; i++)
outPdf.AddPage(src.Pages[i]);
if (namedDestMap.Count > 0)
RewriteNamedDestLinks(outPdf, pageOffset, namedDestMap);
}
else
{
AddImagePagesFromFile(outPdf, f);
}
}
ScrubEmptyOutlines(outPdf);
ScrubDegenerateCropBoxes(outPdf);
CliEnsureParentDir(outPath);
outPdf.Save(outPath);
con.WriteLine($"Merged {inputs.Count} files ({outPdf.PageCount} pages) -> {outPath}");
return 0;
}
// ============================================================
// --extract-pages <in.pdf> <range> <out.pdf>
// ============================================================
// Same primitive as the GUI extract (PageOperations.cs Split_Click):
// Import-mode open, AddPage per selected index, save a fresh document.
private static int CliExtractPages(List<string> pos, TextWriter con)
{
if (pos.Count != 3)
{
con.WriteLine("Usage: KillerPDF.exe --extract-pages <in.pdf> <pages> <out.pdf> (pages like 1-3,5,9-12)");
return 2;
}
string inPath = Path.GetFullPath(pos[0]), spec = pos[1], outPath = Path.GetFullPath(pos[2]);
if (!File.Exists(inPath)) { con.WriteLine($"Input not found: {inPath}"); return 2; }
using var importDoc = PdfReader.Open(inPath, PdfDocumentOpenMode.Import);
var indices = CliParsePageRange(spec, importDoc.PageCount, out string err);
if (indices is null) { con.WriteLine(err); return 2; }
using var newDoc = new PdfDocument();
foreach (var idx in indices)
newDoc.AddPage(importDoc.Pages[idx]);
ScrubEmptyOutlines(newDoc);
ScrubDegenerateCropBoxes(newDoc);
CliEnsureParentDir(outPath);
newDoc.Save(outPath);
con.WriteLine($"Extracted {indices.Count} pages -> {outPath}");
return 0;
}
// ============================================================
// --split <in.pdf> <outDir>
// ============================================================
private static int CliSplit(List<string> pos, TextWriter con)
{
if (pos.Count != 2)
{
con.WriteLine("Usage: KillerPDF.exe --split <in.pdf> <outputFolder>");
return 2;
}
string inPath = Path.GetFullPath(pos[0]), outDir = Path.GetFullPath(pos[1]);
if (!File.Exists(inPath)) { con.WriteLine($"Input not found: {inPath}"); return 2; }
Directory.CreateDirectory(outDir);
using var importDoc = PdfReader.Open(inPath, PdfDocumentOpenMode.Import);
string baseName = Path.GetFileNameWithoutExtension(inPath);
int digits = Math.Max(3, importDoc.PageCount.ToString().Length);
for (int i = 0; i < importDoc.PageCount; i++)
{
using var single = new PdfDocument();
single.AddPage(importDoc.Pages[i]);
ScrubEmptyOutlines(single);
ScrubDegenerateCropBoxes(single);
single.Save(Path.Combine(outDir, $"{baseName}-page-{(i + 1).ToString().PadLeft(digits, '0')}.pdf"));
}
con.WriteLine($"Split {importDoc.PageCount} pages into {outDir}");
return 0;
}
// ============================================================
// --decrypt <in.pdf> <out.pdf> [--password <p>]
// ============================================================
// Without a password: the same lossless PDFium strip the GUI uses at
// open time (owner/permissions encryption), with the Import-rebuild
// fallback. With a password: PdfSharpCore opens with the password and
// saves a decrypted copy, the same sequence as the GUI password path.
private static int CliDecrypt(List<string> pos, Dictionary<string, string> options, TextWriter con)
{
if (pos.Count != 2)
{
con.WriteLine("Usage: KillerPDF.exe --decrypt <in.pdf> <out.pdf> [--password <password>]");
return 2;
}
string inPath = Path.GetFullPath(pos[0]), outPath = Path.GetFullPath(pos[1]);
if (!File.Exists(inPath)) { con.WriteLine($"Input not found: {inPath}"); return 2; }
CliEnsureParentDir(outPath);
options.TryGetValue("--password", out string? password);
if (!string.IsNullOrEmpty(password))
{
using var doc = PdfReader.Open(inPath, password!, PdfDocumentOpenMode.Modify);
ScrubEmptyOutlines(doc);
ScrubDegenerateCropBoxes(doc);
doc.Save(outPath);
con.WriteLine($"Decrypted -> {outPath}");
return 0;
}
if (TryPdfiumStripEncryption(inPath, outPath))
{
con.WriteLine($"Decrypted (lossless) -> {outPath}");
return 0;
}
if (TryImportRepairToPath(inPath, outPath))
{
con.WriteLine($"Decrypted via page rebuild -> {outPath} (bookmarks/forms may be dropped)");
return 0;
}
con.WriteLine("Could not decrypt. If the file needs a password to open, pass --password.");
return 1;
}
// ============================================================
// Shared rasterization prep
// ============================================================
// PDFium sizes its bitmap from the un-rotated MediaBox, so pages with
// /Rotate 90/270 clip if rendered directly (same reason TempReload
// strips rotations app-wide). Prep: decrypt if needed, capture per-page
// /Rotate + point dims, strip rotations to a temp, and let callers
// rotate the pixel buffers afterward (BitmapHelpers.RotateBitmapStatic).
// Falls back to rendering the file as-is when PdfSharpCore cannot open
// it (rare parser gaps PDFium tolerates); callers then derive
// dimensions from the rendered pixels.
private static (string RenderPath, int[]? Rotations, (double WPt, double HPt)[]? Dims)
CliPrepareRenderSource(string inPath, string? password, TextWriter con)
{
string workPath = inPath;
if (PdfFileHasEncryption(inPath))
{
var dec = App.MakeTempFile("clidec");
if (!string.IsNullOrEmpty(password))
{
using var pdoc = PdfReader.Open(inPath, password!, PdfDocumentOpenMode.Modify);
pdoc.Save(dec);
}
else if (!TryPdfiumStripEncryption(inPath, dec) && !TryImportRepairToPath(inPath, dec))
{
throw new InvalidOperationException(
"File is encrypted and could not be unlocked - pass --password if it needs one.");
}
workPath = dec;
}
try
{
using var doc = PdfReader.Open(workPath, PdfDocumentOpenMode.Modify);
var rotations = new int[doc.PageCount];
var dims = new (double WPt, double HPt)[doc.PageCount];
bool anyRot = false;
for (int i = 0; i < doc.PageCount; i++)
{
var p = doc.Pages[i];
rotations[i] = ((p.Rotate % 360) + 360) % 360;
dims[i] = (p.Width.Point, p.Height.Point);
if (rotations[i] != 0) { anyRot = true; p.Rotate = 0; }
}
if (!anyRot) return (workPath, rotations, dims);
var renderTemp = App.MakeTempFile("clirender");
ScrubEmptyOutlines(doc);
ScrubDegenerateCropBoxes(doc);
doc.Save(renderTemp);
return (renderTemp, rotations, dims);
}
catch (Exception ex)
{
con.WriteLine("Note: structure parse failed (" + FlattenBatchDetail(ex.Message) +
") - rendering as-is; rotated pages may clip.");
return (workPath, null, null);
}
}
// PNG encoding reuses the app's RenderToPng (DirtyTracking.cs). The
// JPEG variant is CLI-only - no JPEG encoder existed before.
private static byte[] CliEncodeJpeg(byte[] bgra, int width, int height)
{
var bmp = BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgra32, null, bgra, width * 4);
var encoder = new JpegBitmapEncoder { QualityLevel = 90 };
encoder.Frames.Add(BitmapFrame.Create(bmp));
using var ms = new MemoryStream();
encoder.Save(ms);
return ms.ToArray();
}
private static void CliEnsureParentDir(string path)
{
var dir = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
}
private static double CliParseDpi(Dictionary<string, string> options, double fallback)
{
if (options.TryGetValue("--dpi", out var s) &&
double.TryParse(s, out double d) && d >= 24 && d <= 1200)
return d;
return fallback;
}
// ============================================================
// --to-image <in.pdf> <outDir> [--dpi n] [--format png|jpg] [--pages range] [--transparent]
// ============================================================
// PDFium leaves unpainted background pixels as BGRA 0,0,0,0. Encoders
// that drop alpha (JPEG) then show them BLACK, and PNG/flatten output
// carries a useless full-page alpha channel (issue #148, Ryokoxx).
// Default is now composite-over-white via Docnet's transparency
// remover; --transparent keeps the raw alpha for PNG output.
private static int CliToImage(List<string> pos, Dictionary<string, string> options, TextWriter con)
{
if (pos.Count != 2)
{
con.WriteLine("Usage: KillerPDF.exe --to-image <in.pdf> <outputFolder> [--dpi <n>] [--format png|jpg] [--pages <range>] [--transparent]");
return 2;
}
string inPath = Path.GetFullPath(pos[0]), outDir = Path.GetFullPath(pos[1]);
if (!File.Exists(inPath)) { con.WriteLine($"Input not found: {inPath}"); return 2; }
double dpi = CliParseDpi(options, 150);
options.TryGetValue("--format", out var fmtRaw);
string fmt = (fmtRaw ?? "png").ToLowerInvariant();
if (fmt == "jpeg") fmt = "jpg";
if (fmt != "png" && fmt != "jpg") { con.WriteLine("--format must be png or jpg"); return 2; }
// JPEG has no alpha channel, so --transparent only means anything for png.
bool transparent = fmt == "png" && options.ContainsKey("--transparent");
Directory.CreateDirectory(outDir);
options.TryGetValue("--password", out var password);
var (renderPath, rotations, _) = CliPrepareRenderSource(inPath, password, con);
using var dr = DocLib.Instance.GetDocReader(renderPath, new PageDimensions(dpi / 72.0));
int pageCount = dr.GetPageCount();
List<int> selected;
if (options.TryGetValue("--pages", out var rangeSpec))
{
var parsed = CliParsePageRange(rangeSpec, pageCount, out string err);
if (parsed is null) { con.WriteLine(err); return 2; }
selected = parsed;
}
else
{
selected = [.. Enumerable.Range(0, pageCount)];
}
string baseName = Path.GetFileNameWithoutExtension(inPath);
int digits = Math.Max(3, pageCount.ToString().Length);
foreach (var idx in selected)
{
byte[] raw; int w, h;
using (var pr = dr.GetPageReader(idx))
{
raw = transparent
? pr.GetImage()
: pr.GetImage(new Docnet.Core.Converters.NaiveTransparencyRemover());
w = pr.GetPageWidth();
h = pr.GetPageHeight();
}
int rot = rotations != null && idx < rotations.Length ? rotations[idx] : 0;
if (rot != 0) (raw, w, h) = RotateBitmapStatic(raw, w, h, rot);
var bytes = fmt == "png" ? RenderToPng(raw, w, h) : CliEncodeJpeg(raw, w, h);
var name = $"{baseName}-page-{(idx + 1).ToString().PadLeft(digits, '0')}.{fmt}";
File.WriteAllBytes(Path.Combine(outDir, name), bytes);
}
con.WriteLine($"Rendered {selected.Count} pages at {dpi:0} dpi ({fmt}) into {outDir}");
return 0;
}
// ============================================================
// --flatten <in.pdf> <out.pdf> [--dpi n]
// ============================================================
// Same rasterize-and-rebuild the GUI's Save Flattened runs (150 dpi
// default, PNG-embedded pages sized in points), plus the rotation
// handling the GUI gets for free from its normalized working copy.
private static int CliFlatten(List<string> pos, Dictionary<string, string> options, TextWriter con)
{
if (pos.Count != 2)
{
con.WriteLine("Usage: KillerPDF.exe --flatten <in.pdf> <out.pdf> [--dpi <n>]");
return 2;
}
string inPath = Path.GetFullPath(pos[0]), outPath = Path.GetFullPath(pos[1]);
if (!File.Exists(inPath)) { con.WriteLine($"Input not found: {inPath}"); return 2; }
double dpi = CliParseDpi(options, 150);
options.TryGetValue("--password", out var password);
var (renderPath, rotations, dims) = CliPrepareRenderSource(inPath, password, con);
using var dr = DocLib.Instance.GetDocReader(renderPath, new PageDimensions(dpi / 72.0));
int pageCount = dr.GetPageCount();
using var outDoc = new PdfDocument();
for (int i = 0; i < pageCount; i++)
{
byte[] raw; int w, h;
using (var pr = dr.GetPageReader(i))
{
// Composite over white (#148): keeps the /SMask alpha channel out
// of the rebuilt page images entirely.
raw = pr.GetImage(new Docnet.Core.Converters.NaiveTransparencyRemover());
w = pr.GetPageWidth();
h = pr.GetPageHeight();
}
int rot = rotations != null && i < rotations.Length ? rotations[i] : 0;
if (rot != 0) (raw, w, h) = RotateBitmapStatic(raw, w, h, rot);
var png = RenderToPng(raw, w, h);
double wPt, hPt;
if (dims != null && i < dims.Length)
{
// Page keeps its point size; swap for the viewed orientation.
bool swap = rot == 90 || rot == 270;
wPt = swap ? dims[i].HPt : dims[i].WPt;
hPt = swap ? dims[i].WPt : dims[i].HPt;
}
else
{
wPt = w * 72.0 / dpi;
hPt = h * 72.0 / dpi;
}
var newPage = outDoc.AddPage();
newPage.Width = XUnit.FromPoint(wPt);
newPage.Height = XUnit.FromPoint(hPt);
using var xi = XImage.FromStream(() => new MemoryStream(png));
using var gfx = XGraphics.FromPdfPage(newPage);
gfx.DrawImage(xi, 0, 0, newPage.Width.Point, newPage.Height.Point);
}
CliEnsureParentDir(outPath);
outDoc.Save(outPath);
con.WriteLine($"Flattened {pageCount} pages at {dpi:0} dpi -> {outPath}");
return 0;
}
// ============================================================
// --print <in.pdf> [--printer name] [--pages range] [--copies n]
// ============================================================
// Slimmed headless version of the GUI print spool: rasterize at 300
// dpi (the GUI's print resolution), fit-scale each page centered on
// the printable area, build a FixedDocument, and write it to the
// queue via XPS. Copies replicate the page sequence (ticket CopyCount
// is unreliable across drivers - same reason the GUI does this, #83).
private static int CliPrint(List<string> pos, Dictionary<string, string> options, TextWriter con)
{
if (pos.Count != 1)
{
con.WriteLine("Usage: KillerPDF.exe --print <in.pdf> [--printer <name>] [--pages <range>] [--copies <n>]");
return 2;
}
string inPath = Path.GetFullPath(pos[0]);
if (!File.Exists(inPath)) { con.WriteLine($"Input not found: {inPath}"); return 2; }
int copies = 1;
if (options.TryGetValue("--copies", out var copiesRaw) &&
(!int.TryParse(copiesRaw, out copies) || copies < 1 || copies > 99))
{ con.WriteLine("--copies must be 1-99"); return 2; }
options.TryGetValue("--password", out var password);
var (renderPath, rotations, _) = CliPrepareRenderSource(inPath, password, con);
// Resolve the print queue. Match --printer against FullName,
// exact first then substring, both case-insensitive.
using var server = new LocalPrintServer();
PrintQueue? queue = null;
if (options.TryGetValue("--printer", out var printerName) && !string.IsNullOrWhiteSpace(printerName))
{
var queues = server.GetPrintQueues(
[EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections]).ToList();
queue = queues.FirstOrDefault(q =>
string.Equals(q.FullName, printerName, StringComparison.OrdinalIgnoreCase))
?? queues.FirstOrDefault(q =>
q.FullName.IndexOf(printerName, StringComparison.OrdinalIgnoreCase) >= 0);
if (queue is null)
{
con.WriteLine($"Printer not found: {printerName}. Available:");
foreach (var q in queues) con.WriteLine(" " + q.FullName);
return 2;
}
}
else
{
queue = LocalPrintServer.GetDefaultPrintQueue();
}
// Rasterize the selected pages at 300 dpi, rotation-corrected.
var bitmaps = new List<(BitmapSource Bs, int W, int H)>();
List<int> selected;
using (var dr = DocLib.Instance.GetDocReader(renderPath, new PageDimensions(300.0 / 72.0)))
{
int pageCount = dr.GetPageCount();
if (options.TryGetValue("--pages", out var rangeSpec))
{
var parsed = CliParsePageRange(rangeSpec, pageCount, out string err);
if (parsed is null) { con.WriteLine(err); return 2; }
selected = parsed;
}
else
{
selected = [.. Enumerable.Range(0, pageCount)];
}
foreach (var idx in selected)
{
byte[] raw; int w, h;
using (var pr = dr.GetPageReader(idx))
{
raw = pr.GetImage();
w = pr.GetPageWidth();
h = pr.GetPageHeight();
}
int rot = rotations != null && idx < rotations.Length ? rotations[idx] : 0;
if (rot != 0) (raw, w, h) = RotateBitmapStatic(raw, w, h, rot);
var bs = BitmapSource.Create(w, h, 96, 96, PixelFormats.Bgra32, null, raw, w * 4);
bs.Freeze();
bitmaps.Add((bs, w, h));
}
}
// Orient the sheet to the majority of the selected pages.
bool landscape = bitmaps.Count(b => b.W > b.H) * 2 > bitmaps.Count;
var pd = new System.Windows.Controls.PrintDialog { PrintQueue = queue };
var ticket = pd.PrintTicket;
ticket.CopyCount = 1;
ticket.PageOrientation = landscape ? PageOrientation.Landscape : PageOrientation.Portrait;
pd.PrintTicket = ticket;
double aw = pd.PrintableAreaWidth, ah = pd.PrintableAreaHeight;
if (landscape && ah > aw) (aw, ah) = (ah, aw);
var fixedDoc = new FixedDocument();
for (int c = 0; c < copies; c++)
{
foreach (var (bs, w, h) in bitmaps)
{
double wDip = w * 96.0 / 300.0, hDip = h * 96.0 / 300.0;
double s = Math.Min(aw / wDip, ah / hDip);
double sw = wDip * s, sh = hDip * s;
var img = new System.Windows.Controls.Image { Source = bs, Width = sw, Height = sh };
var fp = new FixedPage { Width = aw, Height = ah };
FixedPage.SetLeft(img, (aw - sw) / 2);
FixedPage.SetTop(img, (ah - sh) / 2);
fp.Children.Add(img);
fp.Measure(new Size(aw, ah));
fp.Arrange(new Rect(0, 0, aw, ah));
fp.UpdateLayout();
var pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fixedDoc.Pages.Add(pc);
}
}
// Write the FixedDocument (not its paginator) - see PrintPreviewWindow
// DoPrint for why. Synchronous Write is fine headless.
var writer = PrintQueue.CreateXpsDocumentWriter(queue);
writer.Write(fixedDoc, ticket);
con.WriteLine($"Sent {selected.Count} pages x{copies} to \"{queue.FullName}\".");
return 0;
}
// ============================================================
// --ocr <in.pdf> <out.pdf> [--lang code]
// ============================================================
// Reuses the GUI's searchable-PDF core (Ocr.cs BuildSearchablePdf):
// Docnet render, Tesseract per page, invisible text drawn over each
// word. The GUI's model-download gate is dialog-driven, so the CLI
// has its own silent equivalent honoring the OcrHighQuality setting.
private static int CliOcr(List<string> pos, Dictionary<string, string> options, TextWriter con)
{
if (pos.Count != 2)
{
con.WriteLine("Usage: KillerPDF.exe --ocr <in.pdf> <out.pdf> [--lang <code>] (default eng)");
return 2;
}
string inPath = Path.GetFullPath(pos[0]), outPath = Path.GetFullPath(pos[1]);
if (!File.Exists(inPath)) { con.WriteLine($"Input not found: {inPath}"); return 2; }
options.TryGetValue("--lang", out var langRaw);
string lang = string.IsNullOrWhiteSpace(langRaw) ? "eng" : langRaw!.Trim().ToLowerInvariant();
if (!CliEnsureOcrLanguage(lang, con)) return 1;
options.TryGetValue("--password", out var password);
var (srcForOcr, rotations, _) = CliPrepareRenderSource(inPath, password, con);
CliEnsureParentDir(outPath);
var (pages, words) = BuildSearchablePdf(srcForOcr, outPath,
(i, n) => { if (i == 1 || i == n || i % 10 == 0) con.WriteLine($"OCR page {i}/{n}"); },
CancellationToken.None, lang);
// The render source had /Rotate stripped; put the angles back on the
// output so rotated pages still display rotated. Content and text
// layer share page space, so they stay aligned.
if (rotations != null && rotations.Any(r => r != 0))
{
using var outDoc = PdfReader.Open(outPath, PdfDocumentOpenMode.Modify);
for (int i = 0; i < outDoc.PageCount && i < rotations.Length; i++)
if (rotations[i] != 0) outDoc.Pages[i].Rotate = rotations[i];
ScrubEmptyOutlines(outDoc);
ScrubDegenerateCropBoxes(outDoc);
outDoc.Save(outPath);
}
con.WriteLine($"OCR complete: {pages} pages, {words} words -> {outPath}");
return 0;
}
/// <summary>
/// Silent equivalent of the GUI's model-download gate: nothing is
/// bundled - every language model streams from the tessdata repos on
/// first use, honoring the app's High Quality setting, with the same
/// .part-then-move atomicity. Runs the download on the thread pool -
/// OnStartup's dispatcher is not pumping, so awaiting here directly
/// would deadlock on the captured WPF context.
/// </summary>
private static bool CliEnsureOcrLanguage(string lang, TextWriter con)
{
OcrNativeBootstrap.EnsureLanguageData();
var dest = Path.Combine(OcrNativeBootstrap.TessDataDir, lang + ".traineddata");
if (File.Exists(dest)) return true;
bool hq = App.GetSetting("OcrHighQuality") == "1";
string url = (hq
? "https://raw.githubusercontent.com/tesseract-ocr/tessdata_best/main/"
: "https://raw.githubusercontent.com/tesseract-ocr/tessdata_fast/main/") + lang + ".traineddata";
con.WriteLine($"Downloading OCR language '{lang}' ({(hq ? "high quality" : "standard")})...");
try
{
Task.Run(async () =>
{
using var http = MakeDownloadClient();
using var resp = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
resp.EnsureSuccessStatusCode();
var part = dest + ".part";
using (var s = await resp.Content.ReadAsStreamAsync().ConfigureAwait(false))
using (var f = File.Create(part))
await s.CopyToAsync(f).ConfigureAwait(false);
if (File.Exists(dest)) File.Delete(dest);
File.Move(part, dest);
}).GetAwaiter().GetResult();
return true;
}
catch (Exception ex)
{
con.WriteLine($"Could not download language '{lang}': " + FlattenBatchDetail(ex.Message));
con.WriteLine("Check the language code (e.g. eng, spa, fra, deu, jpn, tur, ben, chi_sim, chi_tra) and your connection.");
return false;
}
}
}
}