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
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on July 05, 2022Comments
-
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 over 15 yearsthank for reply ... but if i give 420X594 it show only A4 size paper .. please tell me how to set cusom size
-
Fawad almost 8 yearscan you please tell, what do you mean by 800, 800?
-
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 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 over 5 yearsHow 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. over 5 years
Dim pkCustomSize1 As New PaperSize("Custom", 810, HeightVar)
I can't writing c#, just written vb.net only. -
Sarunpong P. over 5 yearsif you don't using
Try-Catch
this code couldn't be working. -
LarsTech over 5 yearsI didn't say not to use a Try-Catch — "empty" was the important word.
-
Sarunpong P. over 5 yearsOK, I just use
try-catch
with empty because I don't want program to show the error exception in that time but now I insertmsgbox()
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 insertmsgbox(ex.tostring)