How can i set PrintPriviewDialog papersize to a5 in c# codebehind?
11,089
Solution 1
You can get the A5 PaperSize
object from the PrinterSettings
property on the PrintDocument
object. It has a PaperSizes
property that holds all the paper sizes for the selected printer. You can use LINQ to find the one you want. For example:
var paperSize = printDoc.PrinterSettings.PaperSizes.Cast<PaperSize>().FirstOrDefault(e => e.PaperName == "A5");
printDoc.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
Solution 2
Just check my answer from the link below :
Thank you.
Author by
gwt
Updated on June 17, 2022Comments
-
gwt about 2 years
i have a windows forms application , with a simple windows form that contains a pannel . I have set the panel size to :560, 579 in pixels and I have set the print document size this way :
System.Drawing.Printing.PaperSize a = new System.Drawing.Printing.PaperSize("A5 (148 x 210 mm)", 584, 827); printDocument1.PrinterSettings.DefaultPageSettings.PaperSize = a;
now I want the printpreviewdialoge page size to be a5 or at least the same size as my print document and fit it , how can i acheive that ?
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Bitmap b = new Bitmap(pnlPrint.Width, pnlPrint.Height); pnlPrint.DrawToBitmap(b, new System.Drawing.Rectangle(0, 0, pnlPrint.Width, pnlPrint.Height)); e.Graphics.DrawImage(b,0,0); } private void Print() { PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog(); var paperSize = printDocument1.PrinterSettings.PaperSizes.Cast<PaperSize>().FirstOrDefault(e => e.PaperName == "A5"); printDocument1.PrinterSettings.DefaultPageSettings.PaperSize = paperSize; printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog(); }
-
gwt almost 12 yearswell thanks but I steel have the problem ! that the printpriviewdialoge does not fit to my panel which I have drawn it to bit map :( I added my complete code , the printpriviewdialoge is steel a4
-
gwt almost 12 yearsmy pannel is at the top corner of the print priview and the rest is white , I want the rest to fit to my panel image
-
Peter Ritchie almost 12 yearsOkay, it's unclear what you're asking. Do you want the paper size to be something specific, or do you want the print preview dialog to be a certain size?
-
gwt almost 12 yearsI want the print preview dialog to be a5 size and I want the image to fit the printpreview dialog , is it clear ?
-
Peter Ritchie almost 12 yearsYou want the dialog to be of a5 size, on the screen? I don't really think that's what you want. But, you can set the size of the dialog with the
PrintDialog.ClientSize
property and set it's location with thePrintDialog.Location
property. -
gwt almost 12 yearsI exactly want that , how about with printpriviewdialog ? what you say is with printdialog not with printpreviewdialog
-
Peter Ritchie almost 12 yearsSorry, that should have been "
PrintPreviewDialog.ClientSize
property and set it's location with thePrintPreviewDialog.Location
..." -
gwt almost 12 yearsI set that this way : printPreviewDialog1.ClientSize=new Size(200, 300); but again the page is in A4 size and does not change
-
Peter Ritchie almost 12 yearsThe size of a window/dialog is in pixels, 200x300 pixels is not likely to be A4 size.