带预览的多页 WPF 打印 [英] WPF printing of multiple pages with preview
问题描述
我对这个主题读得越多,我理解的就越少,如果下面的内容看起来完全不合时宜,请提前道歉.
The more I read about the subject the less I understand so apologies in advance if the below seems completely off the wall.
我有一个包含流文档的用户控件 - 一个具有相应视图模型的视图.目的是将其发送到预览窗口,用户可以在其中查看和打印文档.
I have a usercontrol that contains a flowdocument - a view with a corresponding viewmodel. The aim is to send this to a preview window where the user can view the document and also print it.
当下面被调用时
Public Shared Sub PrintPreview(owner As Window, data As FormData)
Using xpsStream As New MemoryStream()
Using package__1 As Package = Package.Open(xpsStream, FileMode.Create, FileAccess.ReadWrite)
Dim packageUriString As String = "memorystream://data.xps"
Dim packageUri As New Uri(packageUriString)
PackageStore.AddPackage(packageUri, package__1)
Dim xpsDocument__2 As New XpsDocument(package__1, CompressionOption.Maximum, packageUriString)
Dim writer As XpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument__2)
Dim visual As New Form(data)
Dim printTicket As New PrintTicket()
printTicket.PageMediaSize = A4PaperSize
writer.Write(visual, printTicket)
Dim document As FixedDocumentSequence = xpsDocument__2.GetFixedDocumentSequence()
xpsDocument__2.Close()
Dim printPreviewWnd As New PrintPreviewWindow(document)
printPreviewWnd.Owner = owner
printPreviewWnd.ShowDialog()
PackageStore.RemovePackage(packageUri)
End Using
End Using
这会打开打印预览窗口并显示保存流文件的用户控件.但是,它只显示应该是多页的第一页.我假设编写 xps 然后在此窗口中再次读取它的全部目的是解决打印视觉的问题,但我显然误解了整个过程.任何帮助我了解我需要做什么才能查看文档中的所有页面的帮助将不胜感激.
This brings up the print preview window and shows the user control that holds the flowdocument. However, it only shows the first of what should be multiple pages. I was under the assumption the whole point of writing the xps and then reading it back again in this window was to work around the problem of printing a visual but I'm obviously misunderstanding the whole process. Any help in getting through my thick head what I need to do to be able to view all of the pages in the document would be much appreciated.
干杯
我认为上面使用 xpsdocument 和 getfixeddocumentsequence 会将流文档转换为固定文档,但看到它没有,我可能写错了吗??
I thought the above with using xpsdocument and getfixeddocumentsequence would convert the flowdocument to a fixeddocument but seeing it doesn't, am I perhaps writing it wrongly??
推荐答案
您可以将视觉效果打印到 XPS.但是,据我所知,管理页面是您的工作.如果您的视觉对象太大而无法放在一个页面上,您需要找到一种方法将其拆分为多个页面.
You can print a visual to an XPS. However, as I understand it, it is your job to manage pages. If your visual is too big to fit on a page, you need to find a way to split it onto multiple pages.
我在这里发布的源代码打印了多页的项目列表:
The source code I posted here prints a list of items over many pages:
https://bitbucket.org/paulstovell/samples/src/a323f0c65ea4/XPS%20Report%20Generator/
如果您能找到一种将视觉效果(可能创建 3 个表单,每个表单 15 个项目)拆分为页面的方法,那么您可以使用此方法:
If you can find a way to split your visuals (perhaps create 3 forms, with 15 items per form) into pages, you can then use this:
using (var doc = new XpsDocument("P:\\Test2.xps", FileAccess.Write))
{
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
var collator = writer.CreateVisualsCollator();
collator.BeginBatchWrite();
collator.Write(form1);
collator.Write(form2);
collator.Write(form3);
collator.EndBatchWrite();
}
var doc2 = new XpsDocument("P:\\Test2.xps", FileAccess.Read);
var seq = doc2.GetFixedDocumentSequence();
var window = new Window();
window.Content = new DocumentViewer {Document = seq};
window.ShowDialog();
另外,请注意,如果您要新建一个视觉对象并打印它,您需要先调整它的大小,否则您可能会看到一个空白屏幕.这是生成一页数据的示例(当然,您会更改尺寸以适合 A4 纸).
Also, note that if you're newing up a visual and printing it, you'll need to size it first, otherwise you may get an empty screen. Here's an example of generating a page of data (of course you'd change the sizes to fit an A4 sheet).
private StackPanel CreatePage()
{
var panel = new StackPanel();
panel.Width = 1000;
panel.Height = 1000;
for (var i = 0; i < 10; i++)
{
panel.Children.Add(new TextBlock() {Text = "Item " + i});
}
panel.Measure(new Size(1000, 1000));
panel.Arrange(new Rect(0, 0, 1000, 1000));
return panel;
}
这篇关于带预览的多页 WPF 打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!