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;
                }
            }
        }
Exemplo n.º 2
0
        //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());
        }
Exemplo n.º 3
0
        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;
                }
            }
        }
        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;
                }
            }
        }
Exemplo n.º 5
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;
                }
            }
        }
Exemplo n.º 6
0
        private void Start2()
        {
            string inputFile = @"E:\__test_data\i1.pdf";

            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=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(@"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;
                }
            }
        }
Exemplo n.º 7
0
        public void Start()
        {
            string inputFile = @"E:\gss_test\test_postscript.ps";

            GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();

            // 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;

                    //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;
                }
            }
        }