同步pdf打印和标准打印

时间:2011-07-04 19:05:25

标签: c# pdf printing sync printdocument

我需要打印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();
}

欢迎任何帮助。感谢。

3 个答案:

答案 0 :(得分:2)

我无法从这个小例子中完全理解这个问题,但我的猜测是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函数。

答案 1 :(得分:1)

ProcessStartInfo以异步方式运行。所以你开始使用一个或多个acrobat32 exes,每个都需要时间来加载和运行他们的打印功能。与此同时,您的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看起来也是异步的...由于事件处理程序调用,但很难确定。

答案 2 :(得分:1)

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

即。调用异步后:

process.Start();

添加对process.WaitForExit();的调用,以确保按顺序运行。

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