C#打印一份pdf文件并重叠处理
当我打印文件A.pdf和B.pdf时,我运行以下代码:C#打印一份pdf文件并重叠处理,c#,visual-studio,printing,merge,C#,Visual Studio,Printing,Merge,当我打印文件A.pdf和B.pdf时,我运行以下代码: if (File.Exists(@"C:\Users\Luca\Desktop\A.pdf") == true && File.Exists(@"C:\Users\Luca\Desktop\B.pdf") == true) { using (PdfDocument one = PdfReader.Open(@"C:\Users\Luca\Desktop\A.pdf", PdfDocum
if (File.Exists(@"C:\Users\Luca\Desktop\A.pdf") == true && File.Exists(@"C:\Users\Luca\Desktop\B.pdf") == true)
{
using (PdfDocument one = PdfReader.Open(@"C:\Users\Luca\Desktop\A.pdf", PdfDocumentOpenMode.Import))
using (PdfDocument two = PdfReader.Open(@"C:\Users\Luca\Desktop\B.pdf", PdfDocumentOpenMode.Import))
using (PdfDocument outPdf = new PdfDocument())
{
CopyPages(one, outPdf);
CopyPages(two, outPdf);
outPdf.Save(@"C:\Users\Luca\Desktop\C.pdf");
}
}
但是程序显示了一个错误:
"System.IO.IOException: 'The process cannot access the 'C: \ Users \ Luca \ Desktop \ B.pdf' file because it is in use by another process.'.
我尝试使用
Task.Delay(500)的方法
以允许打印时间,但该错误再次显示以前打开文件失败且文件流仍然打开而未关闭时所导致的错误。
当我遇到类似错误时,重新启动电脑关闭所有应用程序解决了此错误
附加说明下面的代码永远不会停止您的程序
Task.Delay(500);
我们必须等待任务完成
Task.Delay(500).Wait();
根据我的测试,我没有遇到你得到的错误。不过,我有两个建议。首先,请检查您的pdf文件是否已被其他应用程序使用。其次,您可以将pdf路径放置到d驱动器。