2017-08-30 54 views
0

qpdf可以将pdf转换为线性化pdf(网页快速查看属性)。我可以使用命令行: qpdf --linearize input.pdf output.pdf将pdf转换为线性化的pdf。如何在asp.net应用程序中使用qpdf

我怎么能在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?非常感谢!

回答

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

相关问题