Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Creating a print event handler in a class, and calling it from my print dialog, I need human help on this

Creating a print event handler in a class, and calling it from my print dialog, I need human help on this

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpasp-netmongodbdotnetwinforms
5 Posts 3 Posters 23 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    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);
    
    Richard Andrew x64R 1 Reply Last reply
    0
    • J jkirkerx

      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);
      
      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      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.

      J 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        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.

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • J jkirkerx

          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

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • D 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 Kreskowiak

            J Offline
            J Offline
            jkirkerx
            wrote on last edited by
            #5

            I'm beginning to realize that now.

            If it ain't broke don't fix it Discover my world at jkirkerx.com

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups