I am attempting to print a WPF control 4" tall by 3" wide.
I have used a ScaleTransform
on the control (a Canvas
) to scale it accordingly; however, when I print to a printer part of the image is cut off (the top and left edges).
According to this thread:
The reason of this problem is that the printer provides an unprinted margin around the edge of the paper, but the
PrintDialog.PrintVisual
method intends to print to the edge of the paper. So the area that lies in the unprinted margin around the edge of the paper is clipped.
The thread fails to mention how to retrieve the margins or how to force the printer to ignore these margins. How do I obtain these values so that I can print using WPF without clipping?
From the File menu, choose "Page Setup" and then set all of your margins to zero. Your document will print across the full width of the paper, as long as your printer's settings are set similarly.
If you have a printer that supports borderless printing, you can find the borderless printing option from the printer or print properties window that appears when you are printing an image. Many newer inkjet printers allow for borderless printing.
You'll need to combine information from the PrintDocumentImageableArea
with the Measure
and Arrange
members on your UIElement
:
// I could not find another way to change the margins other than the dialog
var result = printDialog.ShowDialog();
if (result.HasValue && result.Value)
{
var queue = printDialog.PrintQueue;
// Contains extents and offsets
var area = queue.GetPrintCapabilities(printDialog.PrintTicket)
.PageImageableArea;
// scale = area.ExtentWidth and area.ExtentHeight and your UIElement's bounds
// margin = area.OriginWidth and area.OriginHeight
// 1. Use the scale in your ScaleTransform
// 2. Use the margin and extent information to Measure and Arrange
// 3. Print the visual
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With