同步 pdf 打印和标准打印

发布于 2024-11-18 10:48:22 字数 817 浏览 2 评论 0 原文

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

欢迎任何帮助。谢谢。

i need to print a pdf file, a standar print, other pdf file, other standar print, and so on.
But, when i sent to the printer the sheets are mixed.

i desire:

   PDF
   PrintPage
   PDF
   PrintPage
   PDF
   PrintPage

but, i got (for example):

   PDF
   PDF
   PrintPage
   PrintPage
   PrintPage
   PDF

I'm using the following code to achive the task:

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();
}

Any help will be welcome. thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

§普罗旺斯的薰衣草 2024-11-25 10:48:22

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

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,

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();

}

and in the pd_PrintPageHandler method, call this printPage function with the next PDF file.

窝囊感情。 2024-11-25 10:48:22

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.

小鸟爱天空丶 2024-11-25 10:48:22

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

即在调用异步之后:

process.Start();

添加对 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:

process.Start();

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文