WPF print multiple pages with preview

The more I read about the subject, the less I understand, so I apologize in advance if the bottom layer seems completely outside the wall.

I have a usercontrol that contains a flowdocument - a view with the corresponding viewmodel. The goal is to send this to the preview window, where the user can view the document and also print it.

I took the code from the example http://www.eggheadcafe.com/tutorials/aspnet/9cbb4841-8677-49e9-a3a8-46031e699b2e/wpf-printing-and-print-pr.aspx

When below is called

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 

This will lead to viewing the preview window and display the user control in which the document is stored. However, it shows only the first of what should be several pages. I was assuming that the whole point of writing xps and then reading it again in this window is to solve the problem of printing a visual image, but I obviously do not understand the whole process. Any help in going through my thick head that I need to do to be able to view all the pages in the document would be greatly appreciated.

Greetings

I thought the above using xpsdocument and getfixeddocumentsequence would convert the flowdocument to a fixeddocument, but seeing that this is not the case, I may have spelled it incorrectly ??

+4
source share
1 answer

You can print the visual image on XPS. However, as I understand it, this is your job for managing pages. If your visual is too large to fit on the page, you need to find a way to divide it into several pages.

The source code that I posted here prints a list of elements on many pages:

https://bitbucket.org/paulstovell/samples/src/a323f0c65ea4/XPS%20Report%20Generator/

If you can find a way to divide your visual effects (maybe create 3 forms with 15 elements per form) into pages, you can 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(); 

Also, note that if you create a visual visualization and print it, you need to define it first, otherwise you may get a blank screen. Here is an example of creating a data page (of course, you will resize 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; } 
+6
source

Source: https://habr.com/ru/post/1346485/


All Articles