Exemplo n.º 1
0
        /// <summary>
        /// Merge PDF files.
        /// </summary>
        /// <param name="inputPdfFiles"></param>
        /// <param name="outputPdfFile"></param>
        public static void MergePdf(string[] inputPdfFiles, string outputPdfFile)
        {
            PDFConvert converter = new PDFConvert();

            converter.ThrowOnlyException = true; // rethrow exceptions

            //gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -sOutputFile=out.pdf in1.pdf in2.pdf in3.pdf
            List <string> gsArgs = new List <string>();

            gsArgs.Add("-gs");
            gsArgs.Add("-sDEVICE=pdfwrite");
            gsArgs.Add("-dNOPAUSE");
            gsArgs.Add("-dQUIET");
            gsArgs.Add("-dBATCH");
            gsArgs.Add("-sOutputFile=" + outputPdfFile);

            foreach (string inputPdfFile in inputPdfFiles)
            {
                gsArgs.Add(inputPdfFile);
            }

            if (!converter.Initialize(gsArgs.ToArray()))
            {
                throw new ApplicationException("Merge PDF failed.");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get PDF Page Count.
        /// </summary>
        /// <param name="inputPdfFile"></param>
        /// <returns></returns>
        public static int GetPdfPageCount(string inputPdfFile)
        {
            string     inputFile = inputPdfFile.Replace('\\', '/');
            PDFConvert converter = new PDFConvert();

            converter.RedirectIO         = true;
            converter.ThrowOnlyException = true; // rethrow exceptions

            //gs -q -dNODISPLAY -c "(input.pdf) (r) file runpdfbegin pdfpagecount = quit"
            List <string> gsArgs = new List <string>();

            gsArgs.Add("-gs");
            gsArgs.Add("-dNOPAUSE");
            gsArgs.Add("-dQUIET");
            gsArgs.Add("-dNODISPLAY");
            gsArgs.Add("-dBATCH");
            gsArgs.Add("--permit-file-read=" + inputFile);
            gsArgs.Add("-c");
            string cValue = string.Format("({0}) (r) file runpdfbegin pdfpagecount = quit", inputFile);

            gsArgs.Add(cValue);

            int pageCount = 0;

            using (StringWriter writer = new StringWriter())
            {
                try
                {
                    converter.StdOut = writer;
                    if (converter.Initialize(gsArgs.ToArray()))
                    {
                        pageCount = Int32.Parse(writer.ToString().Trim());
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            return(pageCount);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get PDF Page Count.
        /// </summary>
        /// <param name="inputPdfFile"></param>
        /// <returns></returns>
        public static int GetPdfPageCount(string inputPdfFile)
        {
            PDFConvert converter = new PDFConvert();

            converter.RedirectIO         = true;
            converter.ThrowOnlyException = true; // rethrow exceptions

            //gs -q -sPDFname=test.pdf pdfpagecount.ps
            List <string> gsArgs = new List <string>();

            gsArgs.Add("-gs");
            gsArgs.Add("-dNOPAUSE");
            gsArgs.Add("-dQUIET");
            gsArgs.Add("-dBATCH");
            gsArgs.Add("-sPDFname=" + inputPdfFile);
            gsArgs.Add("Library/pdfpagecount.ps");

            int pageCount = 0;

            using (StringWriter writer = new StringWriter())
            {
                try
                {
                    converter.StdOut = writer;
                    if (converter.Initialize(gsArgs.ToArray()))
                    {
                        pageCount = Int32.Parse(writer.ToString().Replace("%%Pages: ", String.Empty));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            return(pageCount);
        }