繁体   English   中英

如何在ASP.NET应用程序中使用qpdf设置线性化PDF(Web快速查看属性)

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

QPDF可以将pdf转换为线性化的pdf(网络快速查看属性)。 我可以使用命令行:qpdf --linearize input.pdf output.pdf将pdf转换为线性化的pdf。

我如何在asp.net程序上使用它?

我的代码就是这样

    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.
        }
    }

在asp.net中还有使用Qpdf的其他解决方案吗? 非常感谢!

最后,我使用qpdf.exe使它像以下代码一样工作。

    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.
        }
    }

另外,我还在输出文件夹上为asp.net用户角色添加了“写”权限。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM