简体   繁体   中英

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

    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM