Program.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Diagnostics;
  2. using UglyToad.PdfPig;
  3. if (args.Length != 2)
  4. {
  5. Console.WriteLine("Err: 2 args required, <inputDir> <outputDir>");
  6. return 1;
  7. }
  8. var inputDir = args[0];
  9. var outputDir = args[1];
  10. if (!Directory.Exists(inputDir))
  11. {
  12. Console.WriteLine("Err: inputDir dose not exist");
  13. return 1;
  14. }
  15. if (!Directory.Exists(outputDir))
  16. {
  17. Console.WriteLine("Err: outputDir dose not exist");
  18. return 1;
  19. }
  20. Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;
  21. var fullInputDir = Path.GetFullPath(inputDir);
  22. var fullOutputDir = Path.GetFullPath(outputDir);
  23. var filePathsToProcess = Directory.GetFiles(fullInputDir);
  24. var signatureJpg = new byte[] { 0xFF, 0xD8, 0xFF };
  25. void ProcessFunc(string inputFilePath)
  26. {
  27. var outputFileDir = Path.Combine(fullOutputDir, Path.GetFileNameWithoutExtension(inputFilePath));
  28. if (Directory.Exists(outputFileDir) == false)
  29. {
  30. lock (Console.Out) Console.WriteLine("Create dir: " + outputFileDir);
  31. Directory.CreateDirectory(outputFileDir);
  32. }
  33. var pdfBytes = File.ReadAllBytes(inputFilePath);
  34. using var document = PdfDocument.Open(pdfBytes);
  35. var pages = document.GetPages().ToArray();
  36. for (var pageIndex = 0; pageIndex < pages.Length; pageIndex++)
  37. {
  38. var page = pages[pageIndex];
  39. var images = page.GetImages().ToArray();
  40. for (var imageIndex = 0; imageIndex < images.Length; imageIndex++)
  41. {
  42. var image = images[imageIndex];
  43. var outputFileName = $"P{pageIndex + 1:0000}_{imageIndex + 1:000}";
  44. if (image.TryGetPng(out var bytes))
  45. {
  46. var finalOutputPath = Path.Combine(outputFileDir, outputFileName + ".png");
  47. if (File.Exists(finalOutputPath))
  48. {
  49. Console.WriteLine($"SKIP {finalOutputPath}");
  50. continue;
  51. }
  52. File.WriteAllBytes(finalOutputPath, bytes);
  53. Console.WriteLine($"SAVE {finalOutputPath}");
  54. }
  55. else if (image.TryGetBytes(out var irlBytes))
  56. {
  57. var finalOutputPath = Path.Combine(outputFileDir, outputFileName + ".bin");
  58. if (File.Exists(finalOutputPath))
  59. {
  60. Console.WriteLine($"SKIP {finalOutputPath}");
  61. continue;
  62. }
  63. File.WriteAllBytes(finalOutputPath, bytes);
  64. Console.WriteLine($"SAVE {finalOutputPath}");
  65. }
  66. else
  67. {
  68. bytes = image.RawBytes.ToArray();
  69. var span = (ReadOnlySpan<byte>)bytes;
  70. var extName = ".bin";
  71. if (span.Length > signatureJpg.Length && span.StartsWith(signatureJpg))
  72. {
  73. extName = ".jpg";
  74. }
  75. else
  76. {
  77. int bp = 0;
  78. }
  79. var finalOutputPath = Path.Combine(outputFileDir, outputFileName + extName);
  80. if (File.Exists(finalOutputPath))
  81. {
  82. Console.WriteLine($"SKIP {finalOutputPath}");
  83. continue;
  84. }
  85. File.WriteAllBytes(finalOutputPath, bytes);
  86. Console.WriteLine($"SAVE {finalOutputPath}");
  87. }
  88. }
  89. }
  90. }
  91. Parallel.ForEach(filePathsToProcess, ProcessFunc);
  92. // for debug
  93. //foreach (var s in filePathsToProcess) ProcessFunc(s);
  94. return 0;