WPF PrintVisual on a receive printer - this is cropping an image

I print a visual image in WPF on a receipt printer (Star TSP 700II). When visually small, this is normal and it prints well.

However, when the image grows, it will crop the image, and it will be printed to a certain size on a roll in a Star printer, and then simply cut out without printing the rest of the image.

PrintDialog.PrintVisual(Grid1, "Test"); 

I tried to customize the PageMediaSize page, but without changing anything in the printout.

Interestingly, when printing to Microsoft XPS Document Writer, the saved file has a full image.

enter image description here

I also noticed that the size it prints is always maximum height = height of an A4 page. The question is how to make it print at A4 height (when I print a test document from the printer settings, it can do it).

+6
source share
3 answers

Ok, I solved it using the following class. Basically I put what I want to print inside the scrollviewer, and put the glass panel on it, then pass that glass panel to my print assistant and now print without cropping:

 public static class PrintHelper { public static FixedDocument GetFixedDocument(FrameworkElement toPrint, PrintDialog printDialog) { var capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket); var pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight); var visibleSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); var fixedDoc = new FixedDocument(); //If the toPrint visual is not displayed on screen we neeed to measure and arrange it toPrint.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); toPrint.Arrange(new Rect(new Point(0, 0), toPrint.DesiredSize)); // var size = toPrint.DesiredSize; //Will assume for simplicity the control fits horizontally on the page double yOffset = 0; while (yOffset < size.Height) { var vb = new VisualBrush(toPrint) { Stretch = Stretch.None, AlignmentX = AlignmentX.Left, AlignmentY = AlignmentY.Top, ViewboxUnits = BrushMappingMode.Absolute, TileMode = TileMode.None, Viewbox = new Rect(0, yOffset, visibleSize.Width, visibleSize.Height) }; var pageContent = new PageContent(); var page = new FixedPage(); ((IAddChild)pageContent).AddChild(page); fixedDoc.Pages.Add(pageContent); page.Width = pageSize.Width; page.Height = pageSize.Height; var canvas = new Canvas(); FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth); FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight); canvas.Width = visibleSize.Width; canvas.Height = visibleSize.Height; canvas.Background = vb; page.Children.Add(canvas); yOffset += visibleSize.Height; } return fixedDoc; } public static void ShowPrintPreview(FixedDocument fixedDoc) { var wnd = new Window(); var viewer = new DocumentViewer(); viewer.Document = fixedDoc; wnd.Content = viewer; wnd.ShowDialog(); } public static void PrintNoPreview(PrintDialog printDialog,FixedDocument fixedDoc) { printDialog.PrintDocument(fixedDoc.DocumentPaginator, "Test Print No Preview"); } } 
+8
source

In the past few days, I also had this problem.

The solution was to convert the root element into memory.

 PrintDialog dlg = new PrintDialog(); // Let it meassure to the printer default width // and use an infinity height Grid1.Meassure(new Size(dlg.PrintableAreaWidth, double.PositiveInfinity)); // Let it arrange to the meassured size Grid1.Arrange(new Rect(Grid1.DesiredSize)); // Update the element Grid1.UpdateLayout(); 

Then create a new document for the printer:

You should check the printer’s shutdown settings (for example, use the Receipt cutting mode).

 // Create a new papersize with the printer default width, and the Grids height dlg.PrintTicket.PageMediaSize = new PageMediaSize(dlg.PrintableAreaWidth, Grid1.ActualHeight); // Let print ! dlg.PrintVisual(Grid1, "blah"); 

This works like a charm to me and saves a lot of code.

As a reception printer, pagination is not required, I think it is very simple to use.

Note that I DO NOT use this method to render the UIElement created in XAML, all of this is done in code with the StackPanel as the root element.

+1
source

You are using PrintDialog.PrintVisual, which should only print what you can see. For multi-page results, you will need to do more.

You can try DocumentPaginator http://msdn2.microsoft.com/en-us/library/system.windows.documents.documentpaginator.aspx

or

PrintDialog.PrintDocument http://msdn2.microsoft.com/en-us/library/system.windows.controls.printdialog.printdocument.aspx .

0
source

All Articles