Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i set PrintPriviewDialog papersize to a5 in c# codebehind?

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();

    }
like image 905
gwt Avatar asked Sep 10 '12 12:09

gwt


2 Answers

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;
like image 69
Peter Ritchie Avatar answered Oct 03 '22 22:10

Peter Ritchie


Just check my answer from the link below :

Printing Envelopes from C#

Thank you.

like image 25
nassimlouchani Avatar answered Oct 04 '22 00:10

nassimlouchani