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. C#
  4. Changing paper size at run time

Changing paper size at run time

Scheduled Pinned Locked Moved C#
question
3 Posts 2 Posters 0 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.
  • Y Offline
    Y Offline
    Yaron K
    wrote on last edited by
    #1

    :confused:Hi I have a code which prints to the printer using the PrintDocument & PrintPreviewDialog classes. I need to specify at run time which paper size I want to use: A4, Letter, A3 etc. These are not custome sizes but rather standard sizes, listed in the PaperKind enum. How can I force a certain paper size at run time? The PaperSize.Kind property is read only. I tried using the PageSetupDialog too, hoping that I could simply pass it the requierd page size without having to display the dialog, but could not make it work. thanks

    H 1 Reply Last reply
    0
    • Y Yaron K

      :confused:Hi I have a code which prints to the printer using the PrintDocument & PrintPreviewDialog classes. I need to specify at run time which paper size I want to use: A4, Letter, A3 etc. These are not custome sizes but rather standard sizes, listed in the PaperKind enum. How can I force a certain paper size at run time? The PaperSize.Kind property is read only. I tried using the PageSetupDialog too, hoping that I could simply pass it the requierd page size without having to display the dialog, but could not make it work. thanks

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Again, as I recommended before - and as MSDN recommends - enumerate the PrintDocument.PrinterSettings.PaperSizes (one the PrintDocument.PrinterSettings.PrinterName is set to the desired printer). If you look at the documentation for PrintDocument, the best place to set the PaperSize is in the handler for the PrintDocument.QueryPageSettings event, which occurs before each PrintDocument.PrintPage event:

      // Declared in class to cache PaperSize;
      private PaperSize paperSize;
       
      // Method to enumerate PaperSizes from selected Printer.
      private PaperSize GetPaperSize(PrinterSettings printer, PaperKind kind)
      {
      if (printer == null) throw new ArgumentNullException("printer");
      if (this.paperSize == null)
      {
      foreach (PaperSize size in printer.PaperSizes
      {
      if (size.Kind == kind)
      {
      this.paperSize = size;
      return this.paperSize;
      }
      }
      }
      else return this.paperSize;

      // Only occurs if requested PaperSize with PaperKind doesn't exist.
      throw new ArgumentException("The requested paper size is not supported " +
      "by this printer.", "kind");
      }
       
      // Handler for PrintDocument.QueryPageSettings event.
      private void printDocument_QueryPageSettings(object sender,
      QueryPageSettingsEventHandler e)
      {
      try
      {
      e.PageSettings.PaperSize =
      this.GetPaperSize(e.PageSettings.PrinterSettings, PaperKind.A3);
      }
      catch (Exception ex)
      {
      MessageBox.Show(string.Format("Could not print the document: {0}",
      ex.Message), "Print Error", MessageBoxButtons.OK,
      MessageBoxIcon.Error);
      e.Cancel = true;
      }
      }

      This should work; if not, read the documentation for PrintDocument, PageSettings, and PrinterSettings.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      Y 1 Reply Last reply
      0
      • H Heath Stewart

        Again, as I recommended before - and as MSDN recommends - enumerate the PrintDocument.PrinterSettings.PaperSizes (one the PrintDocument.PrinterSettings.PrinterName is set to the desired printer). If you look at the documentation for PrintDocument, the best place to set the PaperSize is in the handler for the PrintDocument.QueryPageSettings event, which occurs before each PrintDocument.PrintPage event:

        // Declared in class to cache PaperSize;
        private PaperSize paperSize;
         
        // Method to enumerate PaperSizes from selected Printer.
        private PaperSize GetPaperSize(PrinterSettings printer, PaperKind kind)
        {
        if (printer == null) throw new ArgumentNullException("printer");
        if (this.paperSize == null)
        {
        foreach (PaperSize size in printer.PaperSizes
        {
        if (size.Kind == kind)
        {
        this.paperSize = size;
        return this.paperSize;
        }
        }
        }
        else return this.paperSize;

        // Only occurs if requested PaperSize with PaperKind doesn't exist.
        throw new ArgumentException("The requested paper size is not supported " +
        "by this printer.", "kind");
        }
         
        // Handler for PrintDocument.QueryPageSettings event.
        private void printDocument_QueryPageSettings(object sender,
        QueryPageSettingsEventHandler e)
        {
        try
        {
        e.PageSettings.PaperSize =
        this.GetPaperSize(e.PageSettings.PrinterSettings, PaperKind.A3);
        }
        catch (Exception ex)
        {
        MessageBox.Show(string.Format("Could not print the document: {0}",
        ex.Message), "Print Error", MessageBoxButtons.OK,
        MessageBoxIcon.Error);
        e.Cancel = true;
        }
        }

        This should work; if not, read the documentation for PrintDocument, PageSettings, and PrinterSettings.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        Y Offline
        Y Offline
        Yaron K
        wrote on last edited by
        #3

        Thanks, that works well.

        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