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...
Moving to WPF...
 
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
Hi jacobgtd,

I recommend you convert what you need to print to a PDF then print it.

I use a PDF generation library called PDFFlow and here is some simple code to create a table (grid) and set the PDF to landscape mode:

C#:
 //Create a document builder:
   DocumentBuilder.New()
 
   //Add a section and section content:
   .AddSection()
           .SetMargins(50)
           .SetOrientation(PageOrientation.landscape)
           .AddParagraph("Lorem ipsum dolor sit amet")
   //Build a file:
   .ToDocument().Build("Result.pdf");

use the "AddTable" method allows you to add a table, configure the table and add content to it inside this method.
C#:
mySectionBuilder.AddTable(...)

Call the AddTableToSection method to add a table, configure the table and add content to it inside this method.

C#:
mySectionBuilder.AddTableToSection(...)]

Call the TableBuilder.New method and pass the instance of SectionBuilder you want to add this table to in this method.
Call the AddTable method to add a table and specify the column width.
C#:

C#:
myRepeatingAreaBuilder.AddTable(...)
myRepeatingAreaBuilder.AddTableToRepeatingArea(...)
   myTableBuilder.SetContentRowStyleHorizontalAlignment(HorizontalAlignment.Center)


Call the AddColumn() method for each column you want to add.
C#:
myTableBuilder.AddColumn().AddColumn().AddColumn();

Add a row to the table by calling one of the AddRow or AddRowToTable methods

C#:
 myTable.AddRow(...)

This will work 100%, I hope I solved your problem :)
 
Last edited:
Back
Top Bottom