Question How to print a grid ?

jacobgtd

New member
Joined
Jul 9, 2021
Messages
4
Programming Experience
Beginner
Hi everyone! I am completely lost so I figured I would try to come here for some help. Basically, I've created this c# WPF program and it's basically complete, but it just needs to be able to print one of the controls (a grid). I've looked basically everywhere and I can't figure out for the life of me how to do it besides:
Bad way to do this:
            PrintDialog printDialog = new PrintDialog();
            Visual visual = grid;
            printDialog.PrintVisual(visual, "grid printing");
This will print off, but I can't figure out how to switch it to landscape mode, or add any padding around it. It also doesn't give any option from which printer to print it from. I've also tried to somehow convert it to a PDF but I couldn't get that to work either. I'm a little bit new so I'm sorry if this isn't the most interesting thing in the world. Any help would be great! Thanks!
 
Solution
Try do it this way...
C#:
            PrintDialog __PrintDialog = new();
            if (__PrintDialog.ShowDialog() == true)
            {
                PrintTicket __Ticket = __PrintDialog.PrintTicket;
                __Ticket.PageOrientation = PageOrientation.Landscape;
                __Ticket.OutputColor = OutputColor.Color;
                __Ticket.OutputQuality = OutputQuality.High;
                __Ticket.PageBorderless = PageBorderless.None;
                __PrintDialog.PrintTicket = __Ticket;
                __PrintDialog.PrintVisual(this, "Printing stuff");
            }
All your needed printer properties can be set on __Ticket
The correct orientation is already set.
For choosing your printer, that is done when...

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Moving to WPF...
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+

NoUserHere

Well-known member
Joined
Sep 5, 2018
Messages
2,138
Programming Experience
10+
Try do it this way...
C#:
            PrintDialog __PrintDialog = new();
            if (__PrintDialog.ShowDialog() == true)
            {
                PrintTicket __Ticket = __PrintDialog.PrintTicket;
                __Ticket.PageOrientation = PageOrientation.Landscape;
                __Ticket.OutputColor = OutputColor.Color;
                __Ticket.OutputQuality = OutputQuality.High;
                __Ticket.PageBorderless = PageBorderless.None;
                __PrintDialog.PrintTicket = __Ticket;
                __PrintDialog.PrintVisual(this, "Printing stuff");
            }
All your needed printer properties can be set on __Ticket
The correct orientation is already set.
For choosing your printer, that is done when the dialog appears.
To make it work, you need to add a reference to the ReechFramework. The IDE can help you import the required reference.
 
Solution
Top Bottom