WPF 固定文档分页
如何让固定文档自动分页?我有以下代码,可以用来将美化面板放入 DocViewer 中。当面板超出单个页面时会出现此问题。现在,我们只需剪辑即可。基本上,我想创建一种相当通用的方法来打印用户正在查看的内容。我的做法合理吗?
public void CreateReport(Panel details)
{
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
fixedPage.DataContext = this.DataContext;
fixedPage.Margin = new Thickness(10);
fixedPage.Children.Add(details);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
// This makes the array of controls invisibile, then climbs the details structure
// and makes the controls within details appropriate for the DocumentViewwer (i.e. TextBoxes are
// non-editable, no borders, no scroll bars, etc).
prePrintPrepare(details, fixedPage, new FrameworkElement[] { controlToMakeInvisible });
_dv = new DocViewer();
_dv.documentViewer1.Document = fixedDoc;
_dv.Show();
}
How do I get FixedDocument to automatically paginate? I have the following code that I can use to put prettified Panel into a DocViewer. The problem occurs when the Panel extends past a single page. Right now, we simply clip. Basically, I want to create a fairly generic way to print what the user is viewing. Is my approach reasonable?
public void CreateReport(Panel details)
{
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
fixedPage.DataContext = this.DataContext;
fixedPage.Margin = new Thickness(10);
fixedPage.Children.Add(details);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
// This makes the array of controls invisibile, then climbs the details structure
// and makes the controls within details appropriate for the DocumentViewwer (i.e. TextBoxes are
// non-editable, no borders, no scroll bars, etc).
prePrintPrepare(details, fixedPage, new FrameworkElement[] { controlToMakeInvisible });
_dv = new DocViewer();
_dv.documentViewer1.Document = fixedDoc;
_dv.Show();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我讨厌回答我自己的问题,但是,以下给了我一个合理的解决方案:
http://social .msdn.microsoft.com/Forums/en-US/wpf/thread/bd89e0d2-73df-4b9b-9210-b8be83ff29d6/
斯科特
I hate answering my own question, but, the following gave me a reasonable solution:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bd89e0d2-73df-4b9b-9210-b8be83ff29d6/
Scott
对于那些对不同且可能更“高级”的解决方案感兴趣的人,请继续查看我的存储库 在 GitHub 上,我演示了如何创建 XAML 报告、对它们进行分页,然后从中生成可打印的固定文档。
神奇的事情发生在 Paginator.cs 中,其中代码遍历所有自定义附加属性,并使用它们来确定在何处设置页码、哪些元素仅在第一页上显示,等等...
全部报告被定义为纯 XAML 用户控件,并在分页完成后转换为固定页面。好处是您可以在纯 XAML 中定义文档/报告,添加一些附加属性,然后分页器代码会处理其余的事情。
编辑:
对于那些难以理解代码的人,您所需要做的就是创建一个工厂 (Func),分页器可以使用它来创建 XAML 用户控件的实例。
举个例子:
如果你想从这些页面获取一个
FixedDocument
,只需调用其余的代码只是一些 MVVM 的东西(现在我实际上会做不同的事情)和打印等等。 。
For those who are interested in a different and maybe more "advanced" solution, go ahead and check out my repository on GitHub where I demonstrate how to create XAML reports, paginate them, then produce a printable FixedDocument from them.
The magic happens in Paginator.cs where the code goes through all the custom attached properties and uses them to determine where to set page number, which elements to show only on first page, and more...
All reports are defined as plain XAML user controls, and are converted to fixed pages after pagination is done. The benefit is that you can define your document/report in pure XAML, add a few attached properties, and the paginator code takes care of the rest.
EDIT:
For those having trouble understanding the code, all you need to do is create a factory (Func) that the Paginator can use to create instances of your XAML UserControls.
So for example:
If you want to get a
FixedDocument
from those pages, just callThe rest of the code is just some MVVM stuff (which I actually would do differently nowadays) and printing and so forth...
已经很久没有回答这个问题了。
我尝试了 Doo Dah 的答案,但问题是它不处理流文档的页面填充。
因此,我编写了自己的解决方案(Doo Dah 的答案帮助我完成了它):
您必须构建之前将显示的内容的 FlowDocument 并传递它
到方法。
添加了 PrintDialog 变量以从预览窗口调用方法,并且可以传递当前打印机设置。
如果您从主程序调用它,您可以传递 new PrintDialog() 或 null,没有区别,因为它将创建一个 new PrintDialog 如果您传递
null
对于具有不同类型文本(标题、文本、字体)的 Flowdocument,这对我来说效果很好。
它应该适用于混合的图片和文本,或者也仅适用于图片 - 它使用的是视觉效果,而不是流程文档中的特定内容,因此它也应该适用于分页符。
我没有尝试 Shahin Dohan 的答案,因为这是同样的问题。
它是用 MVVM 编写的,当其他人编写时很难理解。
在我看来,最好编写一个没有 mvvm 的小示例程序,人们可以使其适应 mvvm 或仅使用代码。
我了解 mvvm 的机会,但为了向某人展示如何工作,我只看到缺点(如果您不展示特定的 mvvm 机制)
It's been a long time since the question has been answered.
I tried Doo Dah's answer, but the problem was that it doesn't take care of the page paddings of a flowdocument.
Therefore I wrote my own solution (Doo Dah's answer helped me to complete it):
You had to build a FlowDocument of the content you will show before and pass it
to the Method.
Added the PrintDialog variable to call the Method from my preview window and can pass the current printer settings.
If you call it from your main programm, you either can pass a
new PrintDialog()
ornull
, there is no difference, because it will create anew PrintDialog
if you are passingnull
This worked fine for me with a Flowdocument with different types of text (header, text, font).
It should work with pictures and text mixed, or only pictures, too - it's using the visuals and not something specific from a flowdocument, therefore it should work with pagebreaks, too.
I don't tryed Shahin Dohan'S answer, because it's the same problem as so often.
It's written at MVVM and very hard to understand when another person has written it.
At my opinion it would be better to write a little example programm without mvvm and the people can adept it to mvvm or use only the code.
I understand the opportunities of mvvm, but to show someone how to works something i see only disadvantages (if you will not show a specific mvvm mechanic)