Setting the paper size

84,744

Solution 1

PrinterSettings ps = new PrinterSettings();
PrintDocument recordDoc = new PrintDocument();
recordDoc.PrinterSettings = ps;

here's a way to set the paper size by kind like 'A4' for example

IEnumerable<PaperSize> paperSizes = ps.PaperSizes.Cast<PaperSize>();
PaperSize sizeA4 = paperSizes.First<PaperSize>(size => size.Kind == PaperKind.A4); // setting paper size to A4 size
recordDoc.DefaultPageSettings.PaperSize = sizeA4;

and here's another way to set a custom paper size

recordDoc.DefaultPageSettings.PaperSize = new PaperSize("210 x 297 mm", 800, 800);
PrintPreviewDialog ppvw = new PrintPreviewDialog();
ppvw .Document = recordDoc;
ppvw.ShowDialog();

Hope it works.

Solution 2

Constructor for paper size is PaperSize(String, Int32, Int32)

5.0 (5) X 5.0 (5) is too little,,, Unless "Custom Size" is your string.. or 420 x 594 for A2...

and also try enumerating foreach PaperSize size in printer.PaperSizes and check whether A2 is there.. or not..

By default it sets Rawkind to custom, You also need to set Rawkind as mentioned in http://msdn.microsoft.com/en-us/library/system.drawing.printing.papersize.rawkind.aspx

Share:
84,744
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    Please help me on how to set my paper size in c# code. I am using the API printDocument.

    Below is my code:

     ppvw = new PrintPreviewDialog();
     ppvw.Document = printDoc;
     ppvw.PrintPreviewControl.StartPage = 0;
     ppvw.PrintPreviewControl.Zoom = 1.0;
     ppvw.PrintPreviewControl.Columns = 10;
    
    
     // Showing the Print Preview Page
     printDoc.BeginPrint += new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
     printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
    
    
     if (ppvw.ShowDialog() != DialogResult.OK)
     {
         printDoc.BeginPrint -= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
         printDoc.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
     }
    
    
     printDoc.PrinterSettings.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("a2", 5.0,5.0);
     printDoc.Print();
    
  • Admin
    Admin over 15 years
    thank for reply ... but if i give 420X594 it show only A4 size paper .. please tell me how to set cusom size
  • Fawad
    Fawad almost 8 years
    can you please tell, what do you mean by 800, 800?
  • Mawardy
    Mawardy almost 8 years
    @Fawad in this constructor the PaperSize class accepts 3 arguments : 1-papersize name it has no relation to the 2nd & 3rd arguments 2-width of the paper 3-height of the paper check those links for detailed info. msdn.microsoft.com/en-us/library/7dbh1cyh(v=vs.110).aspx msdn.microsoft.com/en-us/library/…
  • Jeff Roe
    Jeff Roe about 7 years
    @Fawad: 800, 800 specifies a page size of 8" x 8". (It is width and height, in hundredths of an inch.)
  • LarsTech
    LarsTech over 5 years
    How does it solve the problem if you don't document what the pkCustomSize1 value is? Never use an empty Try-Catch. Question was in c#.
  • Sarunpong P.
    Sarunpong P. over 5 years
    Dim pkCustomSize1 As New PaperSize("Custom", 810, HeightVar) I can't writing c#, just written vb.net only.
  • Sarunpong P.
    Sarunpong P. over 5 years
    if you don't using Try-Catch this code couldn't be working.
  • LarsTech
    LarsTech over 5 years
    I didn't say not to use a Try-Catch — "empty" was the important word.
  • Sarunpong P.
    Sarunpong P. over 5 years
    OK, I just use try-catch with empty because I don't want program to show the error exception in that time but now I insert msgbox() to show what error code founded when the users is using to find the error position easily. But if you want to get an exception can insert msgbox(ex.tostring)