Exemple #1
0
 public void Start(List <string> str)
 {
     // YOU NEED TO HAVE ADMINISTRATOR RIGHTS TO RUN THIS CODE
     foreach (var file in str)
     {
         string inputFile = file;
         try
         {
             using (GhostscriptProcessor processor = new GhostscriptProcessor())
             {
                 List <string> switches = new List <string>();
                 switches.Add("-empty");
                 switches.Add("-dPrinted");
                 switches.Add("-dBATCH");
                 switches.Add("-dNOPAUSE");
                 switches.Add("-dNOSAFER");
                 switches.Add("-dNumCopies=1");
                 switches.Add("-sDEVICE=mswinpr2");
                 switches.Add("-sOutputFile=%printer%" + AppSettings.PrinterName);
                 switches.Add("-f");
                 switches.Add(inputFile);
                 processor.StartProcessing(switches.ToArray(), null);
             }
         }
         catch (Exception e)
         {
         }
     }
 }
Exemple #2
0
        public void PrintPdf(string inputfile)
        {
            GhostscriptVersionInfo _lastInstalledVersion = GetGhostscriptVersion();

            string printerName = System.Drawing.Printing.PrinterSettings.InstalledPrinters.Cast <string>().ToList().Find(f => f.Contains("XPS"));

            using (GhostscriptProcessor processor = new GhostscriptProcessor(_lastInstalledVersion))
            {
                List <string> switches = new List <string>
                {
                    "-empty",
                    "-dPrinted",
                    "-dBATCH",
                    "-dNOPAUSE",
                    "-dNOSAFER",
                    "-dNumCopies=1",
                    "-sDEVICE=mswinpr2",
                    "-sOutputFile=%printer%" + printerName,
                    "-f",
                    inputfile
                };

                processor.StartProcessing(switches.ToArray(), null);
            }
        }
Exemple #3
0
        public void Start()
        {
            string inputFile  = @"E:\gss_test\test.pdf";
            string outputFile = @"E:\gss_test\output\page-%03d.png";

            int pageFrom = 1;
            int pageTo   = 50;

            using (GhostscriptProcessor ghostscript = new GhostscriptProcessor())
            {
                ghostscript.Processing += new GhostscriptProcessorProcessingEventHandler(ghostscript_Processing);

                List <string> switches = new List <string>();
                switches.Add("-empty");
                switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                switches.Add("-dFirstPage=" + pageFrom.ToString());
                switches.Add("-dLastPage=" + pageTo.ToString());
                switches.Add("-sDEVICE=png16m");
                switches.Add("-r96");
                switches.Add("-dTextAlphaBits=4");
                switches.Add("-dGraphicsAlphaBits=4");
                switches.Add(@"-sOutputFile=" + outputFile);
                switches.Add(@"-f");
                switches.Add(inputFile);

                ghostscript.Process(switches.ToArray());
            }
        }
Exemple #4
0
        private static void OptimizarDump()
        {
            var dump   = Directory.GetCurrentDirectory() + @"\dump";
            var dumped = Directory.GetCurrentDirectory() + @"\dumped";
            GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(Directory.GetFiles(Directory.GetCurrentDirectory(), "gsdll32.dll").FirstOrDefault());

            using (GhostscriptProcessor processor = new GhostscriptProcessor(gvi, true))
            {
                //processor.Processing += new GhostscriptProcessorProcessingEventHandler(processor_Processing);

                List <string> switches = new List <string>();
                switches.Add("-empty");
                //switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                //switches.Add(@"-sFONTPATH=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts));
                //switches.Add("-dFirstPage=" + pageFrom.ToString());
                //switches.Add("-dLastPage=" + pageTo.ToString());
                switches.Add("-sDEVICE=pdfwrite");
                switches.Add("-CompressPages=true");
                switches.Add("-dCompatibilityLevel=1.5");
                switches.Add("-dPDFSETTINGS=/ebook");
                switches.Add("-dEmbedAllFonts=false");
                switches.Add("-dSubsetFonts=false");

                switches.Add(@"-sOutputFile=" + dumped);
                switches.Add(@"-f");
                switches.Add(dump);

                // if you dont want to handle stdio, you can pass 'null' value as the last parameter
                //LogStdio stdio = new LogStdio();
                processor.StartProcessing(switches.ToArray(), null);
            }
        }
        protected static void PrintToGhostscript(string printer, string outputFilename, PrintDocument printFunc)
        {
            String      postscriptFile = outputFilename + ".ps";
            PrintDialog printDialog    = new PrintDialog
            {
                AllowPrintToFile = true,
                PrintToFile      = true
            };

            System.Drawing.Printing.PrinterSettings printerSettings = printDialog.PrinterSettings;
            printerSettings.PrintToFile   = true;
            printerSettings.PrinterName   = printer;
            printerSettings.PrintFileName = postscriptFile;
            printFunc(postscriptFile, printerSettings.PrinterName);
            ReleaseCOMObject(printerSettings);
            ReleaseCOMObject(printDialog);
            GhostscriptProcessor gsproc = new GhostscriptProcessor();
            List <string>        gsArgs = new List <string>
            {
                "gs",
                "-dBATCH",
                "-dNOPAUSE",
                "-dQUIET",
                "-dSAFER",
                "-dNOPROMPT",
                "-sDEVICE=pdfwrite",
                String.Format("-sOutputFile=\"{0}\"", string.Join(@"\\", outputFilename.Split(new string[] { @"\" }, StringSplitOptions.None))),
                @"-f",
                postscriptFile
            };

            gsproc.Process(gsArgs.ToArray());
            File.Delete(postscriptFile);
        }
Exemple #6
0
        static void Main(string[] args)
        {
            string inFile  = args[0];
            string outFile = args[1];

            string[] pages = new string[args.Length - 2];
            for (int i = 2; i < args.Length; i++)
            {
                pages[i - 2] = args[i];
            }
            Console.WriteLine();
            GhostscriptProcessor gp = new GhostscriptProcessor(GhostscriptVersionInfo.GetLastInstalledVersion(), true);

            foreach (string p in pages)
            {
                gp.Processing += GPProcess;
                gp.StartProcessing(GSExtractSwitches(inFile, "temp" + p + ".pdf", p), null);
                while (gp.IsRunning)
                {
                }
            }
            gp.Processing += GPProcess;
            gp.StartProcessing(GSCombineSwitches(pages, outFile), null);
            while (gp.IsRunning)
            {
            }
            foreach (string p in pages)
            {
                DeleteTempFiles(p);
            }
        }
Exemple #7
0
        private void CompressDocument()
        {
            List <string> gsArgs = new List <string>();

            gsArgs.Add("-empty");
            gsArgs.Add("-dSAFER");
            gsArgs.Add("-dBATCH");
            gsArgs.Add("-dNOPAUSE");
            gsArgs.Add("-dNOPROMPT");

            gsArgs.Add("-sDEVICE=pdfwrite");
            gsArgs.Add("-dCompatibilityLevel=1.4");
            // dPDFSETTINGS is basically our commpression option
            //  - /screen selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting.
            //  - /ebook selects medium-resolution output similar to the Acrobat Distiller "eBook" setting.
            //  - /printer selects output similar to the Acrobat Distiller "Print Optimized" setting.
            //  - /prepress selects output similar to Acrobat Distiller "Prepress Optimized" setting.
            //  - /default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.
            // gsArgs.Add("-dPDFSETTINGS=/screen");
            gsArgs.Add("-dPDFSETTINGS=/ebook");

            gsArgs.Add("-sOutputFile=" + _outputFile + "");
            gsArgs.Add("-f");
            gsArgs.Add(_inputFile);

            using (GhostscriptProcessor processor = new GhostscriptProcessor(_gs_verssion_info, true))
                processor.StartProcessing(gsArgs.ToArray(), null);
        }
        private void Add_Watermark_To_PDF_Document()
        {
            string inputFile  = @"E:\gss_test\test.pdf";
            string outputFile = @"E:\gss_test\output\test-watermarked.pdf";

            List <string> switches = new List <string>();

            switches.Add(string.Empty);

            // set required switches
            switches.Add("-dBATCH");
            switches.Add("-dNOPAUSE");
            switches.Add("-dNOPAUSE");
            switches.Add("-sDEVICE=pdfwrite");
            switches.Add("-sOutputFile=" + outputFile);
            switches.Add("-c");
            switches.Add(POSTSCRIPT_APPEND_WATERMARK);
            switches.Add("-f");
            switches.Add(inputFile);

            // create a new instance of the GhostscriptProcessor
            using (GhostscriptProcessor processor = new GhostscriptProcessor())
            {
                // start processing pdf file
                processor.StartProcessing(switches.ToArray(), null);
            }

            // show new pdf
            Process.Start(outputFile);
        }
 private static void checkGhostScriptInstall()
 {
     try
     {
         using (GhostscriptProcessor processor = new GhostscriptProcessor())
         {
             //c'est du vent, on n'a pas besoin de ghostscript:
         }
     }
     catch (Exception e)
     {
         //il faut installer ghostscript
         MessageBox.Show("GhostScript 32bit doit être installé en mode administrateur pour utiliser Mobile spool, lancement du téléchargement...", "Spool Wiilog - Erreur installation", MessageBoxButtons.OK, MessageBoxIcon.Error);
         using (var client = new WebClient())
         {
             client.DownloadFile("http://wiilog.fr/dl/mobilespool/dep/gs950w32.exe", "gs950w32.exe");
             Process proc = new Process();
             proc.StartInfo.FileName        = "gs950w32.exe";
             proc.StartInfo.UseShellExecute = true;
             proc.StartInfo.Verb            = "runas";
             try
             {
                 proc.Start();
             }
             catch (Exception)
             {
                 MessageBox.Show("La dépendance n'a pas pu être installé correctement, veuillez relancer l'application", "Spool Wiilog - Erreur installation", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }
     }
 }
Exemple #10
0
        public bool ConvertPsToPdf(string inputFile, string outputFile)
        {
            GhostscriptVersionInfo gv = GhostscriptVersionInfo.GetLastInstalledVersion();

            try
            {
                using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true))
                {
                    processor.Processing += new GhostscriptProcessorProcessingEventHandler(processor_Processing);

                    List <string> switches = new List <string>();
                    switches.Add("-empty");
                    switches.Add("-dQUIET");
                    switches.Add("-dSAFER");
                    switches.Add("-dBATCH");
                    switches.Add("-dNOPAUSE");
                    switches.Add("-dNOPROMPT");
                    switches.Add("-sDEVICE=pdfwrite");
                    switches.Add("-dCompatibilityLevel=1.4");
                    switches.Add("-sOutputFile=" + outputFile);
                    switches.Add("-c");
                    switches.Add("-f");
                    switches.Add(inputFile.ToString());

                    processor.StartProcessing(switches.ToArray(), null);
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
        public void Start()
        {
            string inputFile  = Path.GetFullPath(@"../../../test/test.pdf").Replace("\\", "/");
            string outputFile = Path.GetFullPath(@"../../../test/output/test-t2.tiff").Replace("\\", "/");

            Directory.CreateDirectory(Path.GetDirectoryName(outputFile));

            GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();

            string h = gsPipedOutput.ClientHandle;
            string outputPipeHandle = "%handle%" + int.Parse(h).ToString("X2");

            using (GhostscriptProcessor processor = new GhostscriptProcessor())
            {
                //"C:\Program Files\gs\gs9.15\bin\gswin64.exe" -sDEVICE=tiff24nc -r300 -dNOPAUSE -dBATCH -sOutputFile="Invoice 1_%03ld.tiff" "Invoice 1.pdf"

                List <string> switches = new List <string>();
                //switches.Add("-empty");
                switches.Add("-dQUIET");
                switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                switches.Add("-dPrinted");
                //switches.Add("-sDEVICE=pdfwrite");
                switches.Add("-sDEVICE=tiff24nc");
                switches.Add("-sOutputFile=" + outputPipeHandle);
                switches.Add("-f");
                switches.Add(inputFile);

                try
                {
                    processor.Process(switches.ToArray());

                    byte[] rawDocumentData = gsPipedOutput.Data;
                    var    memStream       = new MemoryStream(rawDocumentData);
                    var    image           = new Bitmap(memStream);
                    image.Save(outputFile);
                    //if (writeToDatabase)
                    //{
                    //    Database.ExecSP("add_document", rawDocumentData);
                    //}
                    //else if (writeToDisk)
                    //{
                    File.WriteAllBytes(outputFile + "__piped_output.data", rawDocumentData);
                    //}
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    gsPipedOutput.Dispose();
                    gsPipedOutput = null;
                }
            }
        }
        /// CreateProcessor
        #region CreateProcessor

        /// <summary>
        /// Creates ghostscript processor
        /// </summary>
        /// <returns>ghostscript processor</returns>
        private static GhostscriptProcessor CreateProcessor()
        {
            GhostscriptVersionInfo gsVersion = new GhostscriptVersionInfo(GetLib());

            // create processor and set constructor 'fromMemory' option to true so the app will be able to run multiple instances of the Ghostscript
            GhostscriptProcessor processor = new GhostscriptProcessor(gsVersion, true);

            return(processor);
        }
        private void Start2()
        {
            string inputFile  = @"E:\__test_data\i1.pdf";
            string outputFile = @"E:\gss_test\output\page-%03d.png";

            GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();

            string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2");

            using (GhostscriptProcessor processor = new GhostscriptProcessor())
            {
                //"C:\Program Files\gs\gs9.15\bin\gswin64.exe" -sDEVICE=tiff24nc -r300 -dNOPAUSE -dBATCH -sOutputFile="Invoice 1_%03ld.tiff" "Invoice 1.pdf"

                List <string> switches = new List <string>();
                switches.Add("-empty");
                switches.Add("-dQUIET");
                switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                switches.Add("-dPrinted");
                switches.Add("-sDEVICE=pdfwrite");
                //switches.Add("-sDEVICE=tiffsep1");
                switches.Add("-sOutputFile=" + outputPipeHandle);
                //switches.Add("-q");
                switches.Add("-f");
                switches.Add(inputFile);

                try
                {
                    processor.Process(switches.ToArray());

                    byte[] rawDocumentData = gsPipedOutput.Data;
                    var    memStream       = new MemoryStream(rawDocumentData);
                    //var image = new Bitmap(memStream);
                    //image.Save(@"Invocie 1.tiff");
                    //if (writeToDatabase)
                    //{
                    //    Database.ExecSP("add_document", rawDocumentData);
                    //}
                    //else if (writeToDisk)
                    //{
                    //    File.WriteAllBytes(@"E:\gss_test\output\test_piped_output.pdf", rawDocumentData);
                    //}
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    gsPipedOutput.Dispose();
                    gsPipedOutput = null;
                }
            }
        }
        //https://www.ghostscript.com/download/gsdnld.html

        public ProcessResult Process(string key, string psFilename)
        {
            log.DebugFormat("PDFWriter process: key={0}, psFilename={1}", key, psFilename);

            GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();

            // pipe handle format: %handle%hexvalue
            string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2");

            FileInfo fileInfo       = new FileInfo(psFilename);
            var      outputFilename = Path.Combine(fileInfo.Directory.FullName,
                                                   string.Format("{0}.pdf", Path.GetFileNameWithoutExtension(fileInfo.Name)));

            try
            {
                using (GhostscriptProcessor processor = new GhostscriptProcessor())
                {
                    List <string> switches = new List <string>();
                    switches.Add("-empty");
                    switches.Add("-dQUIET");
                    switches.Add("-dSAFER");
                    switches.Add("-dBATCH");
                    switches.Add("-dNOPAUSE");
                    switches.Add("-dNOPROMPT");
                    switches.Add("-sDEVICE=pdfwrite");
                    switches.Add("-o" + outputPipeHandle);
                    switches.Add("-q");
                    switches.Add("-f");
                    switches.Add(psFilename);

                    try
                    {
                        processor.StartProcessing(switches.ToArray(), null);
                        byte[] rawDocumentData = gsPipedOutput.Data;
                        File.WriteAllBytes(outputFilename, rawDocumentData);

                        log.DebugFormat("PDFWriter success: {0}", outputFilename);
                    }
                    catch (Exception ex)
                    {
                        log.Error("PDFWriter failed", ex);
                    }
                    finally
                    {
                        gsPipedOutput.Dispose();
                        gsPipedOutput = null;
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("PDFWriter failed", ex);
            }

            return(new ProcessResult());
        }
Exemple #15
0
        public void Start()
        {
            // gs -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT --permit-file-read=W:/Projects/sites/library.visyond.gov/80/lib/CS/Ghostscript.NET/test/ -c '(W:/Projects/sites/library.visyond.gov/80/lib/CS/Ghostscript.NET/test/test.pdf) (r) file runpdfbegin pdfpagecount = quit'

            string inputFile = @"../../../test/test.pdf";

            GhostscriptStdIO stdioCb = new GhostscriptViewerStdIOHandler();

            try
            {
                using (GhostscriptProcessor ghostscript = new GhostscriptProcessor())
                {
                    ghostscript.Processing += new GhostscriptProcessorProcessingEventHandler(ghostscript_Processing);

                    List <string> switches = new List <string>();
                    //switches.Add("-empty");
                    switches.Add("-dSAFER");
                    switches.Add("-dBATCH");
                    switches.Add("-dNOPAUSE");
                    switches.Add("-dNOPROMPT");

                    // report the page count as per https://stackoverflow.com/questions/4826485/ghostscript-pdf-total-pages :
                    //
                    // of course this spells trouble when the `outputFile` path itself contains double quotes   :-(
                    // Also you cannot feed this baby *relative* paths as the GhostScript DLL will consider its own location as directory '.':
                    string ap = Path.GetFullPath(inputFile).Replace("\\", "/");
                    //  as per https://stackoverflow.com/questions/50730501/ggt-an-output-file-with-a-count-of-pdf-pages-for-each-file-with-ghostscript#answer-61310660 :
                    // (make sure **all** paths havee forward slashes here, as they must match **exactly**!)
#if false // oddly enough --permit-file-read doesn't fly while -I does, but I've seen this same flaky behaviour on the commandline...   :-(
                    switches.Add($"--permit-file-read={ Path.GetDirectoryName(ap).Replace("\\", "/") }");
#else
                    switches.Add($"-I{ Path.GetDirectoryName(ap).Replace("\\", "/") }");
#endif
                    switches.Add("-c");
                    switches.Add($"({ap}) (r) file runpdfbegin pdfpagecount = quit");

                    if (!File.Exists(ap))
                    {
                        throw new ApplicationException($"input file does not exist: {inputFile}{ (inputFile != ap ? $" --> {ap}" : "") }");
                    }

                    Console.WriteLine("CMD: {0}", String.Join(" ", switches.ToArray()));

                    ghostscript.Process(switches.ToArray(), stdioCb);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error Exception: {ex}");
            }
            finally
            {
                Console.WriteLine(stdioCb.ToString());
            }
        }
Exemple #16
0
        public void Process(GhostscriptVersionInfo ghostscriptVersion, bool fromMemory, GhostscriptStdIO stdIO_callback)
        {
            if (ghostscriptVersion == null)
            {
                throw new ArgumentNullException("ghostscriptVersion");
            }

            using (GhostscriptProcessor processor = new GhostscriptProcessor(ghostscriptVersion, fromMemory))
            {
                processor.StartProcessing(this.GetSwitches(), stdIO_callback);
            }
        }
        public void Start()
        {
            string inputFile = @"..\..\..\test\gre.pdf";

            GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();

            // pipe handle format: %handle%hexvalue
            string h = gsPipedOutput.ClientHandle;
            string outputPipeHandle = "%handle%" + int.Parse(h).ToString("X2");

            using (GhostscriptProcessor processor = new GhostscriptProcessor())
            {
                List <string> switches = new List <string>();
                //switches.Add("-empty");
                switches.Add("-dQUIET");
                switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                switches.Add("-sDEVICE=pdfwrite");
                switches.Add($"-o{ outputPipeHandle }");
                switches.Add("-q");
                switches.Add("-f");
                switches.Add(inputFile);

                try
                {
                    processor.StartProcessing(switches.ToArray(), null);

                    byte[] rawDocumentData = gsPipedOutput.Data;

                    //if (writeToDatabase)
                    //{
                    //    Database.ExecSP("add_document", rawDocumentData);
                    //}
                    //else if (writeToDisk)
                    //{
                    //    File.WriteAllBytes(@"..\..\..\test\output\test_piped_output.pdf", rawDocumentData);
                    //}
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    gsPipedOutput.Dispose();
                    gsPipedOutput = null;
                }
            }
        }
        public static Image ToThumbnail(Stream stream, int dpi, int pageNumber = 1)
        {
            GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(@"./_libs/gsdll64.dll");

            GhostscriptProcessor proc = new GhostscriptProcessor(gvi);

            using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
            {
                rasterizer.Open(stream);

                var result = rasterizer.GetPage(dpi, dpi, pageNumber);

                return(result);
            }
        }
Exemple #19
0
        private static string PsToPDF(string psfilename)
        {
            string inputFile = psfilename;
            GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();

            string pdfFilePath = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";


            // pipe handle format: %handle%hexvalue
            string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2");

            using (GhostscriptProcessor processor = new GhostscriptProcessor())
            {
                List <string> switches = new List <string>();
                switches.Add("-empty");
                switches.Add("-dQUIET");
                switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                switches.Add("-sDEVICE=pdfwrite");
                switches.Add("-o" + outputPipeHandle);
                switches.Add("-q");
                switches.Add("-f");
                switches.Add(inputFile);

                try
                {
                    processor.StartProcessing(switches.ToArray(), null);

                    byte[] rawDocumentData = gsPipedOutput.Data;

                    File.WriteAllBytes(pdfFilePath, rawDocumentData);
                    return(pdfFilePath);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message + "hallo");
                }
                finally
                {
                    gsPipedOutput.Dispose();
                    gsPipedOutput = null;
                }
            }
        }
        public static void Print(string printDocPath, string printerName, bool showWindow = false)
        {
            using (var processor = new GhostscriptProcessor())
            {
                List <string> switches = new List <string>();
                switches.Add("-empty");
                switches.Add("-dPrinted");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOSAFER");
                switches.Add("-dNumCopies=1");
                switches.Add("-sDEVICE=mswinpr2");
                switches.Add("-sOutputFile=%printer%" + printerName);
                switches.Add("-f");
                switches.Add(printDocPath);

                processor.StartProcessing(switches.ToArray(), null);
            }
        }
Exemple #21
0
        private static void StarOptimizer(string path)
        {
            //GhostscriptVersionInfo gv = GhostscriptVersionInfo.GetLastInstalledVersion();

            GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(Directory.GetFiles(Directory.GetCurrentDirectory(), "gsdll32.dll").FirstOrDefault());

            using (GhostscriptProcessor processor = new GhostscriptProcessor(gvi, true))
            {
                //processor.Processing += new GhostscriptProcessorProcessingEventHandler(processor_Processing);

                var fi = new System.IO.FileInfo(path);

                List <string> switches = new List <string>();
                switches.Add("-empty");
                //switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                //switches.Add(@"-sFONTPATH=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts));
                //switches.Add("-dFirstPage=" + pageFrom.ToString());
                //switches.Add("-dLastPage=" + pageTo.ToString());
                switches.Add("-sDEVICE=pdfwrite");
                switches.Add("-CompressPages=true");
                switches.Add("-dCompatibilityLevel=1.5");
                switches.Add("-dPDFSETTINGS=/screen");
                switches.Add("-dEmbedAllFonts=false");
                switches.Add("-dSubsetFonts=false");
                //switches.Add("-dColorImageDownsampleType=/Bicubic");
                //switches.Add("-dColorImageResolution=144");
                //switches.Add("-dGrayImageDownsampleType=/Bicubic");
                //switches.Add("-dGrayImageResolution=144");
                //switches.Add("-dMonoImageDownsampleType=/Bicubic");
                //switches.Add("-dMonoImageResolution=144");
                switches.Add(@"-sOutputFile=D:\Projecto Compacter\Ghost" + fi.Name);
                switches.Add(@"-f");
                switches.Add(path);

                // if you dont want to handle stdio, you can pass 'null' value as the last parameter
                //LogStdio stdio = new LogStdio();
                processor.StartProcessing(switches.ToArray(), null);
            }
        }
        protected override void Print(string printerName, string filePath)
        {
            byte[] buffer = File.ReadAllBytes(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\gsdll32.dll");

            using (GhostscriptProcessor processor = new GhostscriptProcessor(buffer))
            {
                var switches = GsCommandSwitches.Select(s => s.Replace("{printerName}", printerName).Replace("{fileName}", filePath)).ToArray();

                Logger.LogInfo(string.Join(" ", switches));

                var callback = new CallbackStdIO();
                processor.StartProcessing(switches, callback);

                Logger.LogInfo("GS StdOut:\n" + callback.OutLog);
                if (!string.IsNullOrEmpty(callback.ErrorLog))
                {
                    Logger.LogError("GS StdError:\n" + callback.ErrorLog);
                }
            }
        }
Exemple #23
0
        public void StartPrint(string printerName, string filePath)
        {
            Console.WriteLine(printerName);
            using (GhostscriptProcessor processor = new GhostscriptProcessor())
            {
                List <string> switches = new List <string>();
                switches.Add("-empty");
                switches.Add("-dPrinted");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOSAFER");
                switches.Add("-dNumCopies=1");
                switches.Add("-sDEVICE=mswinpr2");
                switches.Add("-sOutputFile=%printer%" + printerName);
                switches.Add("-f");
                switches.Add(filePath);

                processor.StartProcessing(switches.ToArray(), null);
            }
        }
Exemple #24
0
        public void Start()
        {
            string inputFile  = @"E:\gss_test\test.pdf";
            string outputFile = @"E:\gss_test\output\page-%03d.png";

            //string inputFile = @"E:\gss_test\multipage.ps";
            //string outputFile = @"E:\gss_test\output\multipage.pdf";

            int pageFrom = 1;
            int pageTo   = 50;

            GhostscriptVersionInfo gv = GhostscriptVersionInfo.GetLastInstalledVersion();

            using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true))
            {
                processor.Processing += new GhostscriptProcessorProcessingEventHandler(processor_Processing);

                List <string> switches = new List <string>();
                switches.Add("-empty");
                switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                switches.Add(@"-sFONTPATH=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts));
                switches.Add("-dFirstPage=" + pageFrom.ToString());
                switches.Add("-dLastPage=" + pageTo.ToString());
                switches.Add("-sDEVICE=png16m");
                switches.Add("-r96");
                switches.Add("-dTextAlphaBits=4");
                switches.Add("-dGraphicsAlphaBits=4");

                //switches.Add("-sDEVICE=pdfwrite");

                switches.Add(@"-sOutputFile=" + outputFile);
                switches.Add(@"-f");
                switches.Add(inputFile);

                processor.StartProcessing(switches.ToArray(), null);
            }
        }
Exemple #25
0
        private void ConvertToJPEG(string inputFileName)
        {
            Ghostscript.NET.GhostscriptVersionInfo gv = Ghostscript.NET.GhostscriptVersionInfo.GetLastInstalledVersion(Ghostscript.NET.GhostscriptLicense.GPL | Ghostscript.NET.GhostscriptLicense.AFPL, Ghostscript.NET.GhostscriptLicense.GPL);

            string outputPath = System.IO.Path.GetDirectoryName(inputFileName) + "\\" + System.IO.Path.GetFileNameWithoutExtension(inputFileName) + ".jpg";

            System.IO.StreamReader r = new System.IO.StreamReader(inputFileName);
            string pdfText           = r.ReadToEnd();

            System.Text.RegularExpressions.Regex           rx1     = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");
            System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);

            //GhostscriptSharp.GhostscriptWrapper.GenerateOutput(singleFilePath, outputPath, gss);

            using (GhostscriptProcessor gsp = new GhostscriptProcessor(gv, true))
            {
                gsp.Processing += new GhostscriptProcessorProcessingEventHandler(ghostscriptProcessing);

                List <string> switches = new List <string>();

                switches.Add("-empty");
                switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                switches.Add(@"-sFONTPATH=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts));
                switches.Add("-dFirstPage=1");
                switches.Add("-dLastPage=" + matches.Count.ToString());
                switches.Add("-sDEVICE=jpeg");
                switches.Add("-r96");
                switches.Add("-dTextAlphaBits=4");
                switches.Add("-dGraphicsAlphaBits=4");
                switches.Add(@"-sOutputFile=" + outputPath);
                switches.Add(@"-f");
                switches.Add(inputFileName);

                gsp.StartProcessing(switches.ToArray(), null);
            }
        }
Exemple #26
0
        public void Start()
        {
            string inputFile  = Path.GetFullPath(@"../../../test/test.pdf").Replace("\\", "/");
            string outputFile = Path.GetFullPath(@"../../../test/output/page-%03d.png").Replace("\\", "/");

            Directory.CreateDirectory(Path.GetDirectoryName(outputFile));

            int pageFrom = 1;
            int pageTo   = 50;

            GhostscriptVersionInfo gv = GhostscriptVersionInfo.GetLastInstalledVersion();

            using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true))
            {
                processor.Processing += new GhostscriptProcessorProcessingEventHandler(processor_Processing);

                List <string> switches = new List <string>();
                //switches.Add("-empty");
                switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                switches.Add($"-sFONTPATH={ System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts) }");
                switches.Add("-dFirstPage=" + pageFrom.ToString());
                switches.Add("-dLastPage=" + pageTo.ToString());
                switches.Add("-sDEVICE=png16m");
                switches.Add("-r96");
                switches.Add("-dTextAlphaBits=4");
                switches.Add("-dGraphicsAlphaBits=4");
                switches.Add($"-sOutputFile={outputFile}");
                switches.Add("-f");
                switches.Add(inputFile);

                // if you don't want to handle stdio, you can pass 'null' value as the last parameter
                LogStdio stdio = new LogStdio();
                processor.StartProcessing(switches.ToArray(), stdio);
            }
        }
Exemple #27
0
        private void CompressDocument(string path, string outpath)
        {
            // Sourced from https://gist.github.com/firstdoit/6390547
            List <string> gsArgs = new List <string>();


            gsArgs.Add("-empty");
            gsArgs.Add("-dSAFER");
            gsArgs.Add("-dBATCH");
            gsArgs.Add("-dNOPAUSE");
            gsArgs.Add("-dNOPROMPT");

            gsArgs.Add("-sDEVICE=pdfwrite");
            gsArgs.Add("-dCompatibilityLevel=1.4");
            // dPDFSETTINGS is basically our commpression option
            //  - /screen selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting.
            //  - /ebook selects medium-resolution output similar to the Acrobat Distiller "eBook" setting.
            //  - /printer selects output similar to the Acrobat Distiller "Print Optimized" setting.
            //  - /prepress selects output similar to Acrobat Distiller "Prepress Optimized" setting.
            //  - /default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.
            gsArgs.Add("-dPDFSETTINGS=/screen");


            gsArgs.Add("-sOutputFile=" + outpath + "");
            gsArgs.Add("-f");
            gsArgs.Add(path);



            using (GhostscriptProcessor processor = new GhostscriptProcessor(_gs_verssion_info, true))
            {
                processor.Processing += new GhostscriptProcessorProcessingEventHandler(processor_Processing);

                processor.Completed += processor_Completed;

                processor.StartProcessing(gsArgs.ToArray(), null);
            }
        }
        /// Convert
        #region Convert

        /// <summary>
        /// Converts postscript file to pdf
        /// </summary>
        /// <param name="psFileName">postscript file name</param>
        /// <returns>pdf file name</returns>
        public static void ConvertPStoPDF(string psFileName, string pdfFileName)
        {
            if (string.IsNullOrWhiteSpace(psFileName))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(pdfFileName))
            {
                return;
            }

            try
            {
                GhostscriptProcessor process = CreateProcessor();
                process.Process(ConvertPStoPDFArgs(psFileName, pdfFileName));
                process.Dispose();
            }
            catch (Exception ex)
            {
                WPFNotifier.DebugError(ex);
            }
        }
Exemple #29
0
        private void ConvertPsToPdf(string inputFile, string outputFile)
        {
            GhostscriptVersionInfo gv = GhostscriptVersionInfo.GetLastInstalledVersion();

            using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true))
            {
                List <string> switches = new List <string>();
                switches.Add("-empty");
                switches.Add("-dQUIET");
                switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                switches.Add("-sDEVICE=pdfwrite");
                switches.Add("-dCompatibilityLevel=1.4");
                switches.Add("-sOutputFile=" + outputFile);
                switches.Add("-c");
                switches.Add("-f");
                switches.Add(inputFile.ToString());

                processor.StartProcessing(switches.ToArray(), null);
            }
        }
        public void Start()
        {
            // For users who distribute their gsdll32.dll or gsdll64.dll with their application: Ghostscript.NET by default
            // peeks into the registry to collect all installed Ghostscript locations. If you want to use ghostscript dll from
            // a custom location, it's recommended to do something like this:

            GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(@"e:\dummyfolder\myapplication\gsdll32.dll");

            // and then pass that GhostscriptVersionInfo to the required constructor or method

            // sample #1
            GhostscriptProcessor proc = new GhostscriptProcessor(gvi);

            // sample #2
            GhostscriptRasterizer rast = new GhostscriptRasterizer();

            rast.Open("test.pdf", gvi, true);

            // sample #3
            GhostscriptViewer view = new GhostscriptViewer();

            view.Open("test.pdf", gvi, true);
        }