0

以下是我在 Windows 窗体中进行打印的过程。我使用了 PrintDocument 类。它包含 PrintPage 事件,我用它来绘制我需要打印的图形并按预期成功获得结果。

下面是代码:

public PrintDocument Printing
{
   m_printDocument = new PrintDocument();
   m_printDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
}

OnPrintPage 的代码如下:

protected virtual void OnPrintPage(object sender, PrintPageEventArgs e)
 {
      //Image img = I have the things to be printing in the form of image.
      e.Graphics.DrawImage(img, new Point(0,0));
 }

在 WPF 中:

我正在使用固定文档,并使用以下代码可以打印

PrintDialog print = new PrintDialog();
print.PrintDocument(FixedDocument.DocumentPaginator, "Print") //Where Fixed document contains the data to be printed.

这导致内存不足,无法继续执行程序。但我得到了固定文件没有任何问题。任何解决方案...?我希望 WPF 中也有类似 Windows 表单的东西......

4

1 回答 1

3

当我的页面包含大量视觉元素(绘图/图像/复杂图表)时,我曾经得到这个。而不是一次打印完整的文档(这可能导致内存不足)

print.PrintDocument(FixedDocument.DocumentPaginator, "Print")

我打印了它的一页。

 PrintQueue selectedPrntQueue = printDialog.PrintQueue;     
 XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(selectedPrntQueue);
 SerializerWriterCollator collator = writer.CreateVisualsCollator();
 collator.BeginBatchWrite();
 var paginator = FixedDocument.DocumentPaginator;
 FixedPage fixedPage = paginator.GetFixedPage(printedPageCount)
 ContainerVisual newPage = new ContainerVisual();
 Size sz = new Size(pageSize.Height.Value, pageSize.Width.Value);
 fixedPage.Measure(sz);
 fixedPage.Arrange(new Rect(new Point(), sz));
 fixedPage.UpdateLayout();
 newPage.Children.Add(fixedPage);
 collator.Write(newPage);

打印几页后我必须进行 GC(我的幻数是 10)。您可能需要根据您的要求对其进行调整。

于 2013-08-26T11:03:08.310 回答