Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Reflection;
- /*
- Compiled with
- C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:gm_pdf.exe gm_pdf.cs
- */
- public class GM_PDF
- {
- public static void Main()
- {
- string CurrentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
- string[] FullPdfFiles = Directory.GetFiles(CurrentPath, "*.pdf", SearchOption.TopDirectoryOnly);
- List<string> FileNames = new List<string>();
- foreach (string FilePath in FullPdfFiles)
- {
- string FileName = Path.GetFileName(FilePath);
- FileName = FileName.Replace(".pdf", "");
- if (FileName != "output_gm_compiled")
- {
- FileNames.Add(FileName);
- }
- }
- int FilesLength = FileNames.Count;
- Console.WriteLine("Found " + FilesLength + " items in current directory.");
- Console.WriteLine("Processing PDF to PNG...");
- foreach (string FileName in FileNames)
- {
- Console.WriteLine(" > Processing " + FileName + ".pdf");
- ExecCli("gm convert -density 200 \"" + FileName + ".pdf\" -quality 100 -crop 1460x940+95+285 -colorspace Gray -bordercolor white -border 120x200 output_gm_" + FileName + ".png");
- }
- Console.WriteLine("Combining PNG...");
- List<string> PngFileNames = new List<string>(FileNames);
- for (int i=0; i<FilesLength; i+=2)
- {
- int NextIndex = i + 1;
- if (NextIndex < FilesLength)
- {
- string CurrentFile = "output_gm_" + FileNames[i] + ".png";
- string NextFile = "output_gm_" + FileNames[NextIndex] + ".png";
- Console.WriteLine(" > Combining " + CurrentFile + " and " + NextFile);
- ExecCli("gm convert \"" + CurrentFile + "\" \"" + NextFile + "\" -gravity South -chop 0x200 -append \"" + CurrentFile + "\"");
- PngFileNames.Remove(FileNames[NextIndex]);
- File.Delete(NextFile);
- }
- }
- Console.WriteLine("Combining PDF...");
- string FinalArg = "gm convert";
- foreach (string FileName in PngFileNames)
- {
- FinalArg += " \"output_gm_" + FileName + ".png\" ";
- }
- FinalArg += " -compress jpeg -resize 1140x -extent 1240x1753-50 -gravity center -units PixelsPerInch -density 150x150 -repage 1240x1753 output_gm_compiled.pdf";
- ExecCli(FinalArg);
- Console.WriteLine("Cleaning up...");
- foreach (string FileName in PngFileNames)
- {
- File.Delete("output_gm_" + FileName + ".png");
- }
- Console.WriteLine("Done");
- Console.ReadLine();
- }
- private static void ExecCli (string CommandString)
- {
- Process Proc = new Process();
- ProcessStartInfo ProcInfo = new ProcessStartInfo();
- ProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
- ProcInfo.FileName = "cmd.exe";
- ProcInfo.Arguments = "/C " + CommandString;
- Proc.StartInfo = ProcInfo;
- Proc.Start();
- Proc.WaitForExit();
- if (Proc.ExitCode != 0)
- {
- Console.WriteLine("\n<!> Something happened.\n The process has stopped unexpectedly from a command line execution.");
- Console.ReadLine();
- Environment.Exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement