Click here to Skip to main content
15,887,135 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Dave Kreskowiak17-Aug-23 3:13
mveDave Kreskowiak17-Aug-23 3:13 
GeneralRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Richard MacCutchan17-Aug-23 3:23
mveRichard MacCutchan17-Aug-23 3:23 
AnswerRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
jschell17-Aug-23 5:37
jschell17-Aug-23 5:37 
QuestionHow would you test a minimal api that uses a dictionary for saving entities? Pin
Nikol Dimitrova 202314-Aug-23 13:13
Nikol Dimitrova 202314-Aug-23 13:13 
AnswerRe: How would you test a minimal api that uses a dictionary for saving entities? Pin
jschell15-Aug-23 4:36
jschell15-Aug-23 4:36 
GeneralRe: How would you test a minimal api that uses a dictionary for saving entities? Pin
Nikol Dimitrova 202315-Aug-23 7:46
Nikol Dimitrova 202315-Aug-23 7:46 
AnswerRe: How would you test a minimal api that uses a dictionary for saving entities? Pin
Gerry Schmitz15-Aug-23 4:37
mveGerry Schmitz15-Aug-23 4:37 
QuestionCreating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
jkirkerx27-Jul-23 9:08
professionaljkirkerx27-Jul-23 9:08 
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 ..
C#
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.
C#
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);
            Font fontLargeRegular = new Font("Arial", 12, FontStyle.Regular);
            Font fontMedium = new Font("Arial", 10);
            Font fontSmall = new Font("Arial", 8);
            Font fontTiny = new Font("Arial", 6, FontStyle.Italic);

            // Create the brushes we need to draw with
            SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);

            // Define the printable area.
            RectangleF printArea = e.PageSettings.PrintableArea;

            if (e.Graphics != null)
            {
                // Measure the size of the text.
                SizeF textSize = e.Graphics.MeasureString("BILL OF LADING", fontTitleBold);

                // Calculate the starting position to center the text.
                float x = printArea.Left + (printArea.Width - textSize.Width) / 2;
                float y = printArea.Top + (printArea.Height - textSize.Height) / 2;

                // Draw the text on the page.
                e.Graphics.DrawString("Date: ", fontMedium, brush, x, y);

                // Continue with your drawing logic for the rest of the document
                // You can access the 'Bol' property here if needed
                // For example, e.Graphics.DrawString(Bol.SomeProperty, fontMedium, brush, x, y);

                // For example, you can access the Bol property here:
                if (Bol != null)
                {
                    string date = Bol.BolDate.ToString("dd-MM-yyyy");
                    e.Graphics.DrawString("Date: " + date, fontMedium, brush, x, y);
                    e.Graphics.DrawString("Shipper: " + Bol.ShipperName, fontMedium, brush, x, y + 20);
                    e.Graphics.DrawString("Vendor: " + Bol.ShipFromVendor, fontMedium, brush, x, y + 40);

                    // Continue with more drawing logic as needed
                }

            }

        }

        // Override the OnPrintPage method to call your custom PrintPage event handler
        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            base.OnPrintPage(e);
            PrintDocument_PrintPage(this, e);
        }
    }
}

On another note; since this is my first time, what's the best way to test during document composition. I threw out my cheap printer with cheap toner, and can't see having to print a sheet a paper on every test. Should I build a temp print preview first, and get it close enough there, and then use paper to fine tune?

Now I'm wondering if I should change my print dialog to show the preview first, and then hit the print button. The downside to being a rogue programmer is that I have no team to ask for help, and any help or opinions would be appreciated.
If it ain't broke don't fix it
Discover my world at jkirkerx.com

AnswerRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
Richard Andrew x6427-Jul-23 10:55
professionalRichard Andrew x6427-Jul-23 10:55 
GeneralRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
jkirkerx27-Jul-23 11:14
professionaljkirkerx27-Jul-23 11:14 
GeneralRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
Dave Kreskowiak27-Jul-23 18:02
mveDave Kreskowiak27-Jul-23 18:02 
GeneralRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
jkirkerx28-Jul-23 6:26
professionaljkirkerx28-Jul-23 6:26 
QuestionNuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 6:25
professionalRichard Andrew x642-Jul-23 6:25 
AnswerRe: NuGet Packages and Library Files Pin
Gerry Schmitz2-Jul-23 7:38
mveGerry Schmitz2-Jul-23 7:38 
GeneralRe: NuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 8:36
professionalRichard Andrew x642-Jul-23 8:36 
GeneralRe: NuGet Packages and Library Files Pin
Gerry Schmitz2-Jul-23 11:03
mveGerry Schmitz2-Jul-23 11:03 
AnswerRe: NuGet Packages and Library Files Pin
Dave Kreskowiak2-Jul-23 9:27
mveDave Kreskowiak2-Jul-23 9:27 
GeneralRe: NuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 10:38
professionalRichard Andrew x642-Jul-23 10:38 
AnswerRe: NuGet Packages and Library Files Pin
Mirza Ahmed Ali Baig3-Sep-23 23:30
Mirza Ahmed Ali Baig3-Sep-23 23:30 
QuestionIsolated/protected execution environment? Pin
GregJ728-May-23 14:16
GregJ728-May-23 14:16 
AnswerRe: Isolated/protected execution environment? Pin
Dave Kreskowiak29-May-23 3:58
mveDave Kreskowiak29-May-23 3:58 
GeneralRe: Isolated/protected execution environment? Pin
GregJ729-May-23 6:47
GregJ729-May-23 6:47 
AnswerRe: Isolated/protected execution environment? Pin
jschell29-May-23 6:10
jschell29-May-23 6:10 
QuestionHardly any .onnx modules. But plenty of .pt Modules included. Pin
Member 159402389-May-23 15:53
Member 159402389-May-23 15:53 
AnswerRe: Hardly any .onnx modules. But plenty of .pt Modules included. Pin
Pete O'Hanlon9-May-23 20:51
mvePete O'Hanlon9-May-23 20:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.