我正在尝试使用Windows 8.1应用程序中的自定义注释/布局打印大型PDF文件。 我已经使用PrintDocument打印工作,如同Microsoft示例项目here以及Mike Taulty's Blog here中的示例一样。
基本上我为PDF的每个页面生成UIElement,其中包含用于渲染的PDF位图的Image控件,以及用于使用BuildPageAsync(pageNumber)保存的绘图的Canvas。我正在使用第三方库将PDF呈现为位图。
我可以用较小的分辨率渲染它们,但这不是一个可接受的解决方案。
/// <summary>
/// Occurs when the PrintManager requests the final collection of pages to send to the printer (user clicks PRINT button)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void printDocument_AddPages(object sender, AddPagesEventArgs e)
{
this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
for (int i = 0; i < _pageNumberList.Count; i++)
{
_printDocument.AddPage(await BuildPageAsync(_pageNumberList[i]));
}
_printDocument.AddPagesComplete();
}
);
}
这适用于较小的PDF。但是,对于包含大量页面的PDF,渲染要发送到PrintDocument的所有位图会导致系统内存不足,导致生成的打印作业有空白页。
有没有办法将批量页面发送到PrintDocument,然后在Windows 8.1中使用C#按需处理使用过的页面或渲染/准备UIElement页面?
提前感谢您的任何帮助/建议。
更新:显然,在Channel9上打印视频的介绍说,要打印文档和图像(真实打印),我需要使用DirectX和C ++:http://channel9.msdn.com/Blogs/One-Dev-Minute/Print-from-your-app。
我有什么方法可以在我的C#和XAML应用程序中实现它?这是上面提到的C ++示例:http://code.msdn.microsoft.com/windowsapps/Direct2Dapp-printing-sample-9869f99c
更新2:为了进一步说明,是否可以执行与打印预览页面相同的操作(一次请求一页)进行实际打印
/// <summary>
/// Occurs when the PrintManager requests a particular print page to be shown in the preview
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument_GetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async () =>
{
var pageNumber = _pageNumberList[e.PageNumber - 1]; //get the actual page number from the list based on the requested page number
var previewPage = await BuildPageAsync(pageNumber); //get the page here (possibly release other pages if they are not needed)
_printDocument.SetPreviewPage(e.PageNumber, previewPage);
});
}
更新3:以下是MSDN上与相关和未答复的一些论坛帖子:
更新4:由于目前此处和MSDN上没有答案,因此限制要打印的页数似乎是最佳选择。