How to setup linearized PDF(web fast view property) using qpdf in asp.net application

1.5k views Asked by At

QPDF could convert a pdf to linearized pdf(web fast view property). I could use the command line: qpdf --linearize input.pdf output.pdf to transfer a pdf to a linearized pdf.

How could I use it on asp.net program?

My code is like that

    private void LaunchCommandLineApp()
    {
        // For the example
        string ex4 = @"C:\Program Files\qpdf-7.0.b1\bin\qpdf.exe";

        // Use ProcessStartInfo class
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardError = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.FileName = ex4;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = " --linearize input.pdf output.pdf";

        try
        {
            // Start the process with the info we specified.
            // Call WaitForExit and then the using statement will close.
            using (Process exeProcess = Process.Start(startInfo))
            {
                if (exeProcess != null)
                {
                    string op = exeProcess.StandardOutput.ReadToEnd();
                    exeProcess.WaitForExit();
                    Console.WriteLine(op);
                }
            }
        }
        catch (Exception ex)
        {
            // Log error.
        }
    }

Is there any other solution to use Qpdf in asp.net? Thanks much!

1

There are 1 answers

0
wbing520 On

Finally, I use the qpdf.exe to make it work as the following code.

    private void RunQpdfExe(string output)
    {
        string QPDFPath = @"C:\Program Files\qpdf-5.1.2\bin\";
        string newfile = ExportFilePath + "Lin.pdf";

        try
        {
            // Use ProcessStartInfo class
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = false;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.FileName = QPDFPath + "qpdf.exe";
            startInfo.Verb = "runas";
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //startInfo.Arguments = " --check " + output;
            startInfo.WorkingDirectory = Path.GetDirectoryName(QPDFPath);
            startInfo.Arguments = " --linearize " + output + " " + newfile;

            // Start the process with the info we specified.
            // Call WaitForExit and then the using statement will close.
            using (Process exeProcess = Process.Start(startInfo))
            {
                if (exeProcess != null)
                {
                    string op = exeProcess.StandardOutput.ReadToEnd();
                    exeProcess.WaitForExit();
                }
            }

            //Rename the output file back
            File.Delete(output);
            File.Copy(newfile, output);
            File.Delete(newfile);

        }
        catch (Exception)
        {
            // Log error.
        }
    }

Also, I added the "write" permission to asp.net user role on my output folder as well. Hope it helps.