0

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?非常感谢!

4

1 回答 1

0

最后,我使用 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 用户角色添加了“写入”权限。希望能帮助到你。

于 2017-09-05T13:09:14.213 回答