Esempio n. 1
0
        private static string GetOuputFile(ref QPDFOutput output, out bool delete, string tempFolderPath)
        {
            if (!String.IsNullOrWhiteSpace(output.OutputFilePath))
            {
                delete = false;
                return(output.OutputFilePath);
            }
            else
            {
                delete = true;
                output.OutputFilePath = Path.Combine(tempFolderPath, string.Format("{0}.pdf", Guid.NewGuid()));

                return(output.OutputFilePath);
            }
        }
Esempio n. 2
0
        public static void ModifyPDF(QPDFDocument document, QPDFEnvironment environment, QPDFOutput qoutput)
        {
            if (environment == null)
            {
                environment = Environment;
            }

            // check if QPDF is installed!
            if (!File.Exists(environment.QPDFPath))
            {
                throw new QPDFException(String.Format("File '{0}' not found. Check if QPDF application is installed.", environment.QPDFPath));
            }

            bool   deleteInput   = false;
            string inputFilePath = GetSourceFile(ref document, out deleteInput, environment.TempFolderPath);

            if (!File.Exists(inputFilePath))
            {
                // check if the PDF supplied is valid
                throw new QPDFException(String.Format("File '{0}' not found.", inputFilePath));
            }

            // setup our output
            bool   deleteOutput   = false;
            string outputFilePath = GetOuputFile(ref qoutput, out deleteOutput, environment.TempFolderPath);

            // setup our arguments
            string arguments = GetDocumentArguments(ref document, outputFilePath);

            try {
                var output = new StringBuilder();
                var error  = new StringBuilder();

                using (Process process = new Process()) {
                    process.StartInfo.FileName               = environment.QPDFPath;
                    process.StartInfo.Arguments              = arguments;
                    process.StartInfo.UseShellExecute        = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;
                    process.StartInfo.RedirectStandardInput  = true;

                    using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                        using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false)) {
                            DataReceivedEventHandler outputHandler = (sender, e) => {
                                if (e.Data == null)
                                {
                                    outputWaitHandle.Set();
                                }
                                else
                                {
                                    output.AppendLine(e.Data);
                                }
                            };

                            DataReceivedEventHandler errorHandler = (sender, e) => {
                                if (e.Data == null)
                                {
                                    errorWaitHandle.Set();
                                }
                                else
                                {
                                    error.AppendLine(e.Data);
                                }
                            };

                            process.OutputDataReceived += outputHandler;
                            process.ErrorDataReceived  += errorHandler;

                            try {
                                process.Start();

                                process.BeginOutputReadLine();
                                process.BeginErrorReadLine();

                                if (process.WaitForExit(environment.Timeout) && outputWaitHandle.WaitOne(environment.Timeout) && errorWaitHandle.WaitOne(environment.Timeout))
                                {
                                    if (process.ExitCode != 0 && !File.Exists(outputFilePath))
                                    {
                                        throw new QPDFException(String.Format("QPDF conversion of '{0}' failed. QPDF output: \r\n{1}", document.InputFilePath, error));
                                    }
                                }
                                else
                                {
                                    if (!process.HasExited)
                                    {
                                        process.Kill();
                                    }

                                    throw new QPDFTimeoutException();
                                }
                            }
                            finally {
                                process.OutputDataReceived -= outputHandler;
                                process.ErrorDataReceived  -= errorHandler;
                            }
                        }
                }

                // setup our output callback!
                if (qoutput.OutputCallback != null)
                {
                    byte[] pdfFileBytes = File.ReadAllBytes(outputFilePath);
                    qoutput.OutputCallback(document, pdfFileBytes);
                }
            }
            finally {
                // delete the output file if we have specified too
                if (deleteOutput && File.Exists(outputFilePath))
                {
                    File.Delete(outputFilePath);
                }

                // now check the input
                if (deleteInput && File.Exists(document.InputFilePath))
                {
                    File.Delete(document.InputFilePath);
                }
            }
        }
Esempio n. 3
0
 public static void ModifyPDF(QPDFDocument document, QPDFOutput output)
 {
     ModifyPDF(document, null, output);
 }