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.
source share