Skip to main content
 首页 » 编程设计

c#之同步 pdf 打印和标准打印

2024年02月23日12yxwkf

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

欢迎任何帮助。谢谢。

请您参考如下方法:

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