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();
}
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;
Just check my answer from the link below :
Printing Envelopes from C#
Thank you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With