【发布时间】:2019-09-11 16:46:01
【问题描述】:
我正在使用 XpsDocumentWriter 类 (MSDN) - 特别是使用 WriteAsync(FixedDocument) 方法。
这是我用来调用此WriteAsync 方法的代码:
PrintDocumentImageableArea area = null;
var xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(ref area);
var fixedDocument = GenerateDocument();
xpsDocumentWriter.WritingCompleted += DocumentWriterOnWritingCompleted;
try
{
xpsDocumentWriter.WriteAsync(fixedDocument);
}
catch
{
Console.WriteLine("Error occurred");
}
我遇到的问题是,当我打印一个说test.pdf 的文档时,如果我在 PDF 查看器中打开该文件,然后尝试再次打印到该文件名,它会引发异常(由于文件正在打开) - 这个异常在上面的 try-catch 语句中被捕获。
这在我的 PC 和其他一些人的 PC 上很好,但是我发现有 2 台 PC 在相同的情况下不会抛出异常。我还使用以下代码检查了WritingCompleted 事件是否有错误:
private void DocumentWriterOnWritingCompleted(object sender, WritingCompletedEventArgs e)
{
if (e.Error != null)
{
Console.WriteLine("Error has occurred");
}
}
这段代码在那些有问题的机器上没有捕捉到任何东西。
其他信息 - 我已经检查过,在没有检测到错误的 PC 上,文档不会被覆盖,它只是无法静默地写入文档。 WritingCompleted 事件在所有机器上触发,但没有任何错误。
最奇怪的是,这是我临时设置的用于创建FixedDocument的代码:
public FixedDocument DocumentGenerator()
{
FixedDocument fixedDocument = new FixedDocument();
for (int numberOfPages = 0; numberOfPages < 50; numberOfPages++)
{
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage()
{
Width = new Size(96 * 8.5, 96 * 11).Width,
Height = new Size(96 * 8.5, 96 * 11).Height
};
//Add a canvas with a TextBlock and a Rectangle as children.
Canvas canvas = new Canvas();
fixedPage.Children.Add(canvas);
TextBlock textBlock = new TextBlock();
textBlock.Text =
string.Format("Page {0} / {1}\n\nThis Is Page {0}.",
i + 1, 1000);
textBlock.FontSize = 24;
canvas.Children.Add(textBlock);
Rectangle rect = new Rectangle();
rect.Width = 200;
rect.Height = 200;
rect.Fill =
new SolidColorBrush(Color.FromArgb(200, 20, 50, 200));
canvas.Children.Add(rect);
((IAddChild)pageContent).AddChild(fixedPage);
fixedDocument.Pages.Add(pageContent);
}
return fixedDocument;
}
当numberOfPages 为 ~> 30 时,问题 PC 上没有显示错误,但当 numberOfPages 为 ~
所以我想知道是否有人以前见过这样的东西?
为什么FixedDocument 中的内容会对是否由于尝试覆盖被另一个程序锁定的文件而引发错误有任何影响?
【问题讨论】:
标签: c# wpf printing xpsdocument