同步 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();
}
欢迎任何帮助。谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法完全理解这个小示例中的问题,但我的猜测是 pd.Print() 方法是异步的。
您想让打印同步。最好的方法是将代码包装在一个函数中,并从 pd_PrintPageHandler 调用该函数,我假设在打印页面时调用该函数。
通过一个简单的示例来说明我的意思,
并在
pd_PrintPageHandler
方法中,使用下一个 PDF 文件调用此printPage
函数。I cannot fully understand the issue from this small example but my guess is that the
pd.Print()
method is asynchronous.You want to make the printing synchronous. The best way would be to wrap the code in a function and call that function from the
pd_PrintPageHandler
which I assume is called when the page gets printed.A quick example to show what I mean,
and in the
pd_PrintPageHandler
method, call thisprintPage
function with the next PDF file.ProcessStartInfo 异步运行。因此,您启动 1 个或多个 acrobat32 exe,每个都需要时间来加载和运行其打印功能。与此同时,您的 PrintDocument 类正在运行它自己的一组打印过程...因此所有文档都以不可预测的顺序显示。
请参阅:异步进程启动并等待其完成
和这个: http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx
您需要启动 acrobat,等待它完成。然后启动 PrintDocument(无论是什么)并等待它完成。冲洗并重复。
PrintDocument 看起来也是异步的......由于事件处理程序调用,但这很难确定。
ProcessStartInfo runs asynchronously. So you your kicking off 1 or more acrobat32 exes, each of which take time to load and run their print function. In the meantime your PrintDocument class is running it's own set of print procedures... So all of the docs are showing up in an unpredicatble order.
See this: Async process start and wait for it to finish
and this: http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx
You'll need to start acrobat, wait for it to finish. Then start your PrintDocument (whatever that is) and wait for it to finish. Rinse and Repeat.
PrintDocument looks like it's asynchronous as well... due to the event handler call, but that's hard to tell for sure.
由于您使用外部进程来打印 PDF,因此等待该进程退出可能有助于保持打印操作同步。
即在调用异步之后:
添加对
process.WaitForExit();
的调用以保持事物按顺序运行。您可能确实需要对 PrintDocument 执行相同的操作。在这种情况下,您应该能够阻止线程,直到 OnEndPrint 事件:
示例
Since you're using an external process to print the PDFs it might help to wait for that process to exit in order to keep the print operations synchronized.
i.e. After the call to the asynchronous:
Add a call to
process.WaitForExit();
to keep things running in order.You probably do need to do the same for the PrintDocument. In that case, you should be able to just block the thread until the OnEndPrint event is triggered:
example