Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6.  
  7. /*
  8. Compiled with
  9. C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:gm_pdf.exe gm_pdf.cs
  10. */
  11.  
  12. public class GM_PDF
  13. {
  14. public static void Main()
  15. {
  16. string CurrentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
  17. string[] FullPdfFiles = Directory.GetFiles(CurrentPath, "*.pdf", SearchOption.TopDirectoryOnly);
  18. List<string> FileNames = new List<string>();
  19.  
  20. foreach (string FilePath in FullPdfFiles)
  21. {
  22. string FileName = Path.GetFileName(FilePath);
  23. FileName = FileName.Replace(".pdf", "");
  24.  
  25. if (FileName != "output_gm_compiled")
  26. {
  27. FileNames.Add(FileName);
  28. }
  29. }
  30.  
  31. int FilesLength = FileNames.Count;
  32. Console.WriteLine("Found " + FilesLength + " items in current directory.");
  33.  
  34. Console.WriteLine("Processing PDF to PNG...");
  35. foreach (string FileName in FileNames)
  36. {
  37. Console.WriteLine(" > Processing " + FileName + ".pdf");
  38.  
  39. ExecCli("gm convert -density 200 \"" + FileName + ".pdf\" -quality 100 -crop 1460x940+95+285 -colorspace Gray -bordercolor white -border 120x200 output_gm_" + FileName + ".png");
  40. }
  41.  
  42. Console.WriteLine("Combining PNG...");
  43. List<string> PngFileNames = new List<string>(FileNames);
  44. for (int i=0; i<FilesLength; i+=2)
  45. {
  46. int NextIndex = i + 1;
  47.  
  48. if (NextIndex < FilesLength)
  49. {
  50. string CurrentFile = "output_gm_" + FileNames[i] + ".png";
  51. string NextFile = "output_gm_" + FileNames[NextIndex] + ".png";
  52.  
  53. Console.WriteLine(" > Combining " + CurrentFile + " and " + NextFile);
  54.  
  55. ExecCli("gm convert \"" + CurrentFile + "\" \"" + NextFile + "\" -gravity South -chop 0x200 -append \"" + CurrentFile + "\"");
  56. PngFileNames.Remove(FileNames[NextIndex]);
  57. File.Delete(NextFile);
  58. }
  59. }
  60.  
  61. Console.WriteLine("Combining PDF...");
  62. string FinalArg = "gm convert";
  63. foreach (string FileName in PngFileNames)
  64. {
  65. FinalArg += " \"output_gm_" + FileName + ".png\" ";
  66. }
  67.  
  68. FinalArg += " -compress jpeg -resize 1140x -extent 1240x1753-50 -gravity center -units PixelsPerInch -density 150x150 -repage 1240x1753 output_gm_compiled.pdf";
  69. ExecCli(FinalArg);
  70.  
  71. Console.WriteLine("Cleaning up...");
  72. foreach (string FileName in PngFileNames)
  73. {
  74. File.Delete("output_gm_" + FileName + ".png");
  75. }
  76.  
  77. Console.WriteLine("Done");
  78. Console.ReadLine();
  79. }
  80.  
  81. private static void ExecCli (string CommandString)
  82. {
  83. Process Proc = new Process();
  84. ProcessStartInfo ProcInfo = new ProcessStartInfo();
  85. ProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
  86. ProcInfo.FileName = "cmd.exe";
  87. ProcInfo.Arguments = "/C " + CommandString;
  88.  
  89. Proc.StartInfo = ProcInfo;
  90. Proc.Start();
  91. Proc.WaitForExit();
  92.  
  93. if (Proc.ExitCode != 0)
  94. {
  95. Console.WriteLine("\n<!> Something happened.\n The process has stopped unexpectedly from a command line execution.");
  96. Console.ReadLine();
  97. Environment.Exit(1);
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement