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 the paper size at run time

Changing the paper size at run time

Scheduled Pinned Locked Moved C#
question
12 Posts 5 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

    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. thanks

    M 1 Reply Last reply
    0
    • Y Yaron K

      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. thanks

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #2

      In your Print_Page event of PrintDocument: e.PageSettings.PaperSize Mazy No sig. available now.

      M Y 2 Replies Last reply
      0
      • M Mazdak

        In your Print_Page event of PrintDocument: e.PageSettings.PaperSize Mazy No sig. available now.

        M Offline
        M Offline
        Mehdi Moshtaghi
        wrote on last edited by
        #3

        To Mazdak!! Damn man don't you have anything else to do. Go study your exams !!!:cool:

        1 Reply Last reply
        0
        • M Mazdak

          In your Print_Page event of PrintDocument: e.PageSettings.PaperSize Mazy No sig. available now.

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

          Thanks, but i still have the same problem. I am trying to: private void pd_PrintPage(object sender, PrintPageEventArgs e) { e.PageSettings.PaperSize.Kind = PaperKind.A3; } but i does not compile because PaperSize.Kind is read-only.

          H M 2 Replies Last reply
          0
          • Y Yaron K

            Thanks, but i still have the same problem. I am trying to: private void pd_PrintPage(object sender, PrintPageEventArgs e) { e.PageSettings.PaperSize.Kind = PaperKind.A3; } but i does not compile because PaperSize.Kind is read-only.

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

            You have to construct a new PaperSize like so:

            private void pd_PrintPage(object sender, PrintPageEventArgs e)
            {
            // I don't rem
            e.PageSettings.PaperSize =
            new PaperSize("Custom", 11.69 * 100, 16.54 * 100);
            }

            You should also take a look at the PrinterSetttings.PaperSizes documentation. This is a collection that contains each PaperSize that the printer supports. You can enumerate and determine if the printer supports A3 and then get that PaperSize:

            foreach (PaperSize size in PrinterSettings.PaperSizes)
            if (size.Kind == PaperKind)
            return size;
            return null; // or throw an Exception that A3 is not supported

            -----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-----

            I 1 Reply Last reply
            0
            • Y Yaron K

              Thanks, but i still have the same problem. I am trying to: private void pd_PrintPage(object sender, PrintPageEventArgs e) { e.PageSettings.PaperSize.Kind = PaperKind.A3; } but i does not compile because PaperSize.Kind is read-only.

              M Offline
              M Offline
              Mazdak
              wrote on last edited by
              #6

              THis is sample code from MSDN:

              private void MyButtonPrint_Click(object sender, System.EventArgs e)
              {
              // Set the paper size based upon the selection in the combo box.
              if (comboPaperSize.SelectedIndex != -1) {
              printDoc.DefaultPageSettings.PaperSize =
              printDoc.PrinterSettings.PaperSizes[comboPaperSize.SelectedIndex];
              }

                // Set the paper source based upon the selection in the combo box.
                if (comboPaperSource.SelectedIndex != -1) {
                   printDoc.DefaultPageSettings.PaperSource = 
                      printDoc.PrinterSettings.PaperSources\[comboPaperSource.SelectedIndex\];
                }
                
                // Set the printer resolution based upon the selection in the combo box.
                if (comboPrintResolution.SelectedIndex != -1) 
                {
                   printDoc.DefaultPageSettings.PrinterResolution= 
                      printDoc.PrinterSettings.PrinterResolutions\[comboPrintResolution.SelectedIndex\];
                }
              
                // Print the document with the specified paper size, source, and print resolution.
                printDoc.Print();
              

              }

              Mazy No sig. available now.

              1 Reply Last reply
              0
              • H Heath Stewart

                You have to construct a new PaperSize like so:

                private void pd_PrintPage(object sender, PrintPageEventArgs e)
                {
                // I don't rem
                e.PageSettings.PaperSize =
                new PaperSize("Custom", 11.69 * 100, 16.54 * 100);
                }

                You should also take a look at the PrinterSetttings.PaperSizes documentation. This is a collection that contains each PaperSize that the printer supports. You can enumerate and determine if the printer supports A3 and then get that PaperSize:

                foreach (PaperSize size in PrinterSettings.PaperSizes)
                if (size.Kind == PaperKind)
                return size;
                return null; // or throw an Exception that A3 is not supported

                -----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-----

                I Offline
                I Offline
                IntegraSoft
                wrote on last edited by
                #7

                I tried this ans it doesnt work, The paper size is still 8.5 x 11. Do I Have to do something else. Thanks. Carlos Eduardo Hernandez P.

                H 1 Reply Last reply
                0
                • I IntegraSoft

                  I tried this ans it doesnt work, The paper size is still 8.5 x 11. Do I Have to do something else. Thanks. Carlos Eduardo Hernandez P.

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

                  See Mazdak's answer that he(/she?) copied from MSDN. I was just answering why you couldn't change the paper size with the code you were using. I thought it seemed weird that the original reply was doing this in PrintDocument.PrintPage.

                  -----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-----

                  I 1 Reply Last reply
                  0
                  • H Heath Stewart

                    See Mazdak's answer that he(/she?) copied from MSDN. I was just answering why you couldn't change the paper size with the code you were using. I thought it seemed weird that the original reply was doing this in PrintDocument.PrintPage.

                    -----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-----

                    I Offline
                    I Offline
                    IntegraSoft
                    wrote on last edited by
                    #9

                    I try that as well and it didnt work. Am i doing somehting wrong? thanks. Carlos Eduardo Hernandez P.

                    H 1 Reply Last reply
                    0
                    • I IntegraSoft

                      I try that as well and it didnt work. Am i doing somehting wrong? thanks. Carlos Eduardo Hernandez P.

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

                      Are you sure the printer supports that size of paper? Use the other technique I mentioned where you get the sizes that it supports and see if that paper size is in the collection.

                      -----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-----

                      I 1 Reply Last reply
                      0
                      • H Heath Stewart

                        Are you sure the printer supports that size of paper? Use the other technique I mentioned where you get the sizes that it supports and see if that paper size is in the collection.

                        -----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-----

                        I Offline
                        I Offline
                        IntegraSoft
                        wrote on last edited by
                        #11

                        I try using the enumeration of papersize and it didnt work. But I really need to define a custom size, not one defined on the printer. Before using .Net, I was using VB6, and in Windows 98, Windows 95, it was very easy to print in any paper size in the printer. But after windows NT, or windows 2000, my vb6 applications could not print on any sizes on those windows. I found out that after windows nt, it has to be creted a form and then attach it to the printer and print. Could it be that I have to do the same thing. Thanks. Carlos Eduardo Hernandez P.

                        H 1 Reply Last reply
                        0
                        • I IntegraSoft

                          I try using the enumeration of papersize and it didnt work. But I really need to define a custom size, not one defined on the printer. Before using .Net, I was using VB6, and in Windows 98, Windows 95, it was very easy to print in any paper size in the printer. But after windows NT, or windows 2000, my vb6 applications could not print on any sizes on those windows. I found out that after windows nt, it has to be creted a form and then attach it to the printer and print. Could it be that I have to do the same thing. Thanks. Carlos Eduardo Hernandez P.

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

                          Why didn't enumerating work? That's supported by the Framework plus some Windows function that are P/Invoked and has more to do with the print server and the printer driver than the printer itself. As far as forms, A3 is already supported so long as you use the dimensions that I gave in a previous example - no rounding! You could try it, though. After all, all the print classes and methods in the Framework are just wrappers for all the functionality in Windows anyway.

                          -----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-----

                          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