2011-07-04 103 views
1

我需要打印一个pdf文件,标准打印,其他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(); 
} 

任何帮助将受到欢迎。谢谢。

回答

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方法,调用此函数printPage用下一个PDF文件。

1

ProcessStartInfo以异步方式运行。因此,您可以踢出一个或多个acrobat32 exes,每个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看起来也是异步的,但很难说清楚。

1

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

即调用异步后:

process.Start(); 

添加调用process.WaitForExit();保持对事物的顺序运行。

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

+0

它没有用,因为acrobat阅读器有时保持打开状态。 – Gabriel

+0

您可以指定等待acrobat关闭的最大ms数,例如:'WaitForExit(5000)'最多等待5秒钟。 –