// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Spludlow.Printing { public class GhostScript { static string[] ProgramVaribles; static string ProgramPath; static GhostScript() { ProgramVaribles = new string[] { "", }; ProgramVaribles = null; string rootDir = @"C:\Program Files\gs"; if (Directory.Exists(rootDir) == false) throw new ApplicationException("Ghost Script Top Directory missing:\t" + rootDir); List gsDirs = new List(); foreach (string dir in Directory.GetDirectories(rootDir)) if (Path.GetFileName(dir).ToLower().StartsWith("gs") == true) gsDirs.Add(dir); if (gsDirs.Count == 0) throw new ApplicationException("Ghost Script Program Directory not found:\t" + rootDir); ProgramPath = gsDirs[gsDirs.Count - 1] + @"\bin\gswin64c.exe"; if (File.Exists(ProgramPath) == false) throw new ApplicationException("Ghost Script Program EXE not found:\t" + ProgramPath); Spludlow.Log.Info("Ghost Script Program using:\t" + ProgramPath); } public static void PrintPdf(string pdfFilename, string pageSize, bool landscape, string printerType, string printerInfo) { PrintDoc printDoc = new PrintDoc(pageSize, landscape); using (Spludlow.TempDirectory tempDir = new TempDirectory()) { string[] bitmapFilenames = RenderPdf(pdfFilename, tempDir.Path); for (int index = 0; index < bitmapFilenames.Length; ++index) { if (index > 0) printDoc.NewPage(); printDoc.Place(bitmapFilenames[index], 0, 0, 1, 1); } } string address = Environment.MachineName + ":Run"; string printData = printDoc.Save(); Spludlow.Printing.Printer.PrintAt(address, printData, printerType, printerInfo); } public static string[] RenderPdf(string pdfFilename, string outputFilename) { return RenderPdf(pdfFilename, outputFilename, 600, false, false, false); } public static string[] RenderPdf(string pdfFilename, string outputFilename, int dpi) { return RenderPdf(pdfFilename, outputFilename, dpi, false, false, false); } public static string[] RenderPdf(string pdfFilename, string outputFilename, int dpi, bool loadFonts) { return RenderPdf(pdfFilename, outputFilename, dpi, loadFonts, false, false); } public static string[] RenderPdf(string pdfFilename, string outputFilename, int dpi, bool loadFonts, bool grey, bool dontAntialias) { string extention = Path.GetExtension(outputFilename).ToLower(); string device; switch (extention) { case ".png": if (grey == false) device = "png16m"; else device = "pnggray"; break; case ".jpg": // the jpeg and jpeggray devices -dJPEGQ=N (integer from 0 to 100, default 75) case ".jpeg": if (grey == false) device = "jpeg"; else device = "jpeggray"; break; case ".tif": case ".tiff": if (grey == false) device = "tiff32nc"; // tiff32nc Produces 32-bit CMYK output(8 bits per component) else device = "tiffgray"; break; default: throw new ApplicationException("GhostScript RenderPdf; Unsupported traget extention: " + extention); } using (Spludlow.TempDirectory tempDir = new TempDirectory()) { StringBuilder arguments = new StringBuilder(); if (loadFonts == true) arguments.Append(@"-sFONTPATH=C:\Windows\Fonts -dEmbedAllFonts=true "); if (dontAntialias == false) arguments.Append("-dTextAlphaBits=4 -dGraphicsAlphaBits=4 "); arguments.Append("-dSAFER -dBATCH -dNOPAUSE -sDEVICE=@DEVICE -r@DPIx@DPI -o \"@OUTPUT\" \"@INPUT\""); arguments.Replace("@DEVICE", device); arguments.Replace("@DPI", dpi.ToString()); arguments.Replace("@OUTPUT", tempDir.Path + @"\bitmap%8d" + extention); arguments.Replace("@INPUT", pdfFilename); Spludlow.SpawnProcess.ProcessExitInfo info = Spludlow.SpawnProcess.Run(ProgramPath, arguments.ToString(), tempDir.Path, ProgramVaribles, 15 * 60, false); if (info.ExitCode != 0 || info.StandardError.Length > 0) { Spludlow.Log.Error("Ghost Script Exit Code: " + info.ExitCode, new object[] { info.ExitCode, info.StandardOutput, info.StandardError }); throw new ApplicationException("Ghost Script Exit Code: " + info.ExitCode); } List tempFilenames = new List(); foreach (string tempFilename in Directory.GetFiles(tempDir.Path)) { string name = Path.GetFileName(tempFilename); if (name.StartsWith("bitmap") == false || name.EndsWith(extention) == false) { Spludlow.Log.Warning("Ghost Script Unexpected output file:\t" + tempFilename); continue; } tempFilenames.Add(tempFilename); } if (tempFilenames.Count == 0) throw new ApplicationException("Ghost Script no output files from:\t" + pdfFilename); string outputDirecory = null; string outputName = null; string digits = null; if (tempFilenames.Count > 1) { outputDirecory = Path.GetDirectoryName(outputFilename); outputName = Path.GetFileNameWithoutExtension(outputFilename); digits = new string('0', tempFilenames.Count.ToString().Length); } List resultFilenames = new List(); foreach (string tempFilename in tempFilenames) { if (tempFilenames.Count > 1) { int page = Int32.Parse(Path.GetFileName(tempFilename).Substring(6, 8)); outputFilename = outputDirecory + @"\" + outputName + "-" + page.ToString(digits) + extention; } File.Copy(tempFilename, outputFilename); resultFilenames.Add(outputFilename); } return resultFilenames.ToArray(); } } } }