Creating a print event handler in a class, and calling it from my print dialog, I need human help on this
-
This is my first time trying to to print directly to a printer using the PrintDialog and PrintDocument. I thought it would be like creating a PDF, where I just compose and create a file and print, but it looks like I'm forced to use an event handler to compose my print document using Drawing. I was on the internet, and the examples where very simple and basic, but my document is going to be huge, and used many times in my c# winforms .Net Core 7 app. So I used ChatGPT to help me design a class, that I can call as my event handler, but I'm missing something in my class, or got it wrong on how all of this works. On my Print Dialog Form, when I hit the print button, my code example ... The
printDefault.Question
is what I'm trying to call, where Question is where I'm stumped ..if (result == DialogResult.OK)
{
// Record the printer name used when the print job was accepted
this.PrintObject.PrintSettings.DefaultPrinter = printDialog.PrinterSettings.PrinterName;if (this.PrintObject.PrintSettings.DefaultDocument != null) { var printDefault = new PrintDefault { Bol = this.PrintBol }; // Subscribe to the PrintPage event of your custom PrintDocument. printDocument.PrintPage += new PrintPageEventHandler(printDefault.Question); // Start the printing process. printDocument.Print(); this.PrintComplete = true; }
}
And my class .... I figured that my class is an extension of PrintDocument, but I'm not sure. One of the dumb requirements is that I need the MongoDb collection document to generate the BOL form to print, because that has all my data I need to populate the document I'm trying to create. I'm not really sure if my design is sound or not, and may have to think of an alternative. But I feel like I'm really close, just missing something that I didn't think of.
namespace SimpleBol.Classes.DirectPrint
{
public class PrintDefault : PrintDocument
{
public BILLOFLADINGS Bol { get; set; } = null!;// Create a PrintPage event handler method public void PrintDocument\_PrintPage(object sender, PrintPageEventArgs e) { // Create the fonts we need to print with Font fontTitleBold = new Font("Arial", 16, FontStyle.Bold); Font fontTitleRegular = new Font("Arial", 16, FontStyle.Regular); Font fontLargeBold = new Font("Arial", 12, FontStyle.Bold);
-
This is my first time trying to to print directly to a printer using the PrintDialog and PrintDocument. I thought it would be like creating a PDF, where I just compose and create a file and print, but it looks like I'm forced to use an event handler to compose my print document using Drawing. I was on the internet, and the examples where very simple and basic, but my document is going to be huge, and used many times in my c# winforms .Net Core 7 app. So I used ChatGPT to help me design a class, that I can call as my event handler, but I'm missing something in my class, or got it wrong on how all of this works. On my Print Dialog Form, when I hit the print button, my code example ... The
printDefault.Question
is what I'm trying to call, where Question is where I'm stumped ..if (result == DialogResult.OK)
{
// Record the printer name used when the print job was accepted
this.PrintObject.PrintSettings.DefaultPrinter = printDialog.PrinterSettings.PrinterName;if (this.PrintObject.PrintSettings.DefaultDocument != null) { var printDefault = new PrintDefault { Bol = this.PrintBol }; // Subscribe to the PrintPage event of your custom PrintDocument. printDocument.PrintPage += new PrintPageEventHandler(printDefault.Question); // Start the printing process. printDocument.Print(); this.PrintComplete = true; }
}
And my class .... I figured that my class is an extension of PrintDocument, but I'm not sure. One of the dumb requirements is that I need the MongoDb collection document to generate the BOL form to print, because that has all my data I need to populate the document I'm trying to create. I'm not really sure if my design is sound or not, and may have to think of an alternative. But I feel like I'm really close, just missing something that I didn't think of.
namespace SimpleBol.Classes.DirectPrint
{
public class PrintDefault : PrintDocument
{
public BILLOFLADINGS Bol { get; set; } = null!;// Create a PrintPage event handler method public void PrintDocument\_PrintPage(object sender, PrintPageEventArgs e) { // Create the fonts we need to print with Font fontTitleBold = new Font("Arial", 16, FontStyle.Bold); Font fontTitleRegular = new Font("Arial", 16, FontStyle.Regular); Font fontLargeBold = new Font("Arial", 12, FontStyle.Bold);
I can't help you with the print class, but I do have an idea about the testing. Do you have MS Office installed on your development machine? If so, I believe that also installs a "Print to PDF" printer driver. You could use that to print to a PDF file instead of the printer.
The difficult we do right away... ...the impossible takes slightly longer.
-
I can't help you with the print class, but I do have an idea about the testing. Do you have MS Office installed on your development machine? If so, I believe that also installs a "Print to PDF" printer driver. You could use that to print to a PDF file instead of the printer.
The difficult we do right away... ...the impossible takes slightly longer.
I didn't think of that! I have CutePDF that I print all my orders with, before I email them. Thanks!
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
I didn't think of that! I have CutePDF that I print all my orders with, before I email them. Thanks!
If it ain't broke don't fix it Discover my world at jkirkerx.com
On top of that, ChatGPT should not be used to write code. It sucks at writing anything beyond "Hello World" and WILL lead you down incorrect paths.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
On top of that, ChatGPT should not be used to write code. It sucks at writing anything beyond "Hello World" and WILL lead you down incorrect paths.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakI'm beginning to realize that now.
If it ain't broke don't fix it Discover my world at jkirkerx.com