【问题标题】:Sync pdf print and standard print同步 pdf 打印和标准打印
【发布时间】:2011-07-04 19:05:24
【问题描述】:

我需要打印 pdf 文件、标准打印件、其他 pdf 文件、其他标准打印件等。 但是,当我发送到打印机时,纸张是混合的。

我渴望:

   PDF
   PrintPage
   PDF
   PrintPage
   PDF
   PrintPage

但是,我得到了(例如):

   PDF
   PDF
   PrintPage
   PrintPage
   PrintPage
   PDF

我正在使用以下代码来完成任务:

while( ... ) {
    ProcessStartInfo starter = new ProcessStartInfo("path to acrobt32.exe", "/t mypdf001.pdf");
    starter.CreateNoWindow = true;
    starter.RedirectStandardOutput = true;
    starter.UseShellExecute = false;
    Process process = new Process();
    process.StartInfo = starter;
    process.Start();


    PrintDocument pd = new PrintDocument();
    pd.DocumentName = "Work";
    pd.PrintPage += new PrintPageEventHandler(pd_PrintPageHandler);
    pd.Print();
}

欢迎任何帮助。谢谢。

【问题讨论】:

    标签: c# pdf printing sync printdocument


    【解决方案1】:

    我无法从这个小例子中完全理解问题,但我猜pd.Print() 方法是异步的。

    您想让打印同步。最好的方法是将代码包装在一个函数中并从pd_PrintPageHandler 调用该函数,我假设在打印页面时会调用该函数。

    一个简单的例子来说明我的意思,

    function printPage(pdfFilePath)
    {
        ProcessStartInfo starter = new ProcessStartInfo("path to acrobt32.exe", pdfFilePath);
        starter.CreateNoWindow = true;
        starter.RedirectStandardOutput = true;
        starter.UseShellExecute = false;
        Process process = new Process();
        process.StartInfo = starter;
        process.Start();
    
    
        PrintDocument pd = new PrintDocument();
        pd.DocumentName = "Work";
        pd.PrintPage += new PrintPageEventHandler(pd_PrintPageHandler);
        pd.Print();
    
    }
    

    pd_PrintPageHandler 方法中,使用下一个PDF 文件调用此printPage 函数。

    【讨论】:

      【解决方案2】:

      ProcessStartInfo 异步运行。因此,您启动了 1 个或多个 acrobat32 exe,每一个都需要时间来加载和运行它们的打印功能。与此同时,您的 PrintDocument 类正在运行它自己的一组打印程序......所以所有文档都以不可预测的顺序显示。

      看到这个:Async process start and wait for it to finish

      还有这个:http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx

      您需要启动 acrobat,等待它完成。然后启动您的 PrintDocument(无论是什么)并等待它完成。冲洗并重复。

      PrintDocument 看起来也是异步的...由于事件处理程序调用,但这很难确定。

      【讨论】:

        【解决方案3】:

        由于您使用外部进程打印 PDF,等待该进程退出以保持打印操作同步可能会有所帮助。

        即异步调用后:

        process.Start();
        

        添加对 process.WaitForExit(); 的呼叫以保持一切正常。

        您可能确实需要对 PrintDocument 执行相同的操作。在这种情况下,您应该能够阻塞线程,直到触发 OnEndPrint 事件: example

        【讨论】:

        • 您可以指定等待 acrobat 关闭的最大毫秒数,例如:WaitForExit(5000) 最多等待 5 秒。
        猜你喜欢
        • 1970-01-01
        • 2012-09-25
        • 1970-01-01
        • 2021-11-02
        • 2017-06-25
        • 1970-01-01
        • 1970-01-01
        • 2023-04-06
        相关资源
        最近更新 更多