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 :

Printing Envelopes from C#

Thank you.

Share:
11,089
gwt
Author by

gwt

Updated on June 17, 2022

Comments

  • gwt
    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
    gwt almost 12 years
    well 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
    gwt almost 12 years
    my 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
    Peter Ritchie almost 12 years
    Okay, 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
    gwt almost 12 years
    I want the print preview dialog to be a5 size and I want the image to fit the printpreview dialog , is it clear ?
  • Peter Ritchie
    Peter Ritchie almost 12 years
    You 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 the PrintDialog.Location property.
  • gwt
    gwt almost 12 years
    I exactly want that , how about with printpriviewdialog ? what you say is with printdialog not with printpreviewdialog
  • Peter Ritchie
    Peter Ritchie almost 12 years
    Sorry, that should have been " PrintPreviewDialog.ClientSize property and set it's location with the PrintPreviewDialog.Location..."
  • gwt
    gwt almost 12 years
    I set that this way : printPreviewDialog1.ClientSize=new Size(200, 300); but again the page is in A4 size and does not change
  • Peter Ritchie
    Peter Ritchie almost 12 years
    The size of a window/dialog is in pixels, 200x300 pixels is not likely to be A4 size.