本文介绍了构建一个包含多个页面的fixeddocument,每次都不需要showdialog进行打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,其按钮点击时加载了用户控件。作为报告,每个页面都是一个用户控件,我将多个用户控件加载为多个页面。

当我尝试使用FixedDocument进行打印时,如果我为每个用户控件调用PrintDialog.ShowDialog,我可以打印控制正确,但这意味着用户必须为每个页面设置打印机。

有没有办法只显示一次PrintDialog,获取打印机设置,然后应用其余设置页面并随后打印页面?



我尝试过:



我试图在我通过Document.Pages.Add()调用的print函数的调用中共享FixedDocument对象。

但是如上所述,如果我每个都有ShowDialog迭代,否则失败。

I have a WPF application that has user controls being loaded on button clicks. As a report each page is a user control and I load multiple user controls as multiple pages.
When I try to print using FixedDocument if I call PrintDialog.ShowDialog for each user control I am able to print the controls correctly, but that means the user has to setup the printer for each page.
Is there a way I can show the PrintDialog only once, get the printer settings and then apply the settings for the rest of the pages and print the pages subsequently?

What I have tried:

I have tried to share the FixedDocument object across the calls to the print function I have that calls through Document.Pages.Add().
However as mentioned it works if I have the ShowDialog for each iteration and fails otherwise.

推荐答案

// select printer and get printer settings
PrintDialog pd = new PrintDialog();
//catch when user cancel process
//if (!pd.ShowDialog()) return;

// create a document and set PageSize from PrintDialog!
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);

//create page by page
foreach(var p in YourPages) //loop through your collection of controls  
{
    // create a page
    FixedPage page1 = new FixedPage();
    page1.Width = document.DocumentPaginator.PageSize.Width; //trick!
    page1.Height = document.DocumentPaginator.PageSize.Height; //trick!
    //further instructions
}





请参阅:

[]


这篇关于构建一个包含多个页面的fixeddocument,每次都不需要showdialog进行打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:42