5

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
  • 2,331
  • 4
  • 37
  • 59

2 Answers2

8

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;
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • 1
    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 Sep 10 '12 at 13:30
  • 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 – gwt Sep 10 '12 at 13:33
  • 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? – Peter Ritchie Sep 10 '12 at 13:37
  • I want the print preview dialog to be a5 size and I want the image to fit the printpreview dialog , is it clear ? – gwt Sep 10 '12 at 13:51
  • 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. – Peter Ritchie Sep 10 '12 at 13:55
  • I exactly want that , how about with printpriviewdialog ? what you say is with printdialog not with printpreviewdialog – gwt Sep 10 '12 at 13:59
  • Sorry, that should have been " `PrintPreviewDialog.ClientSize` property and set it's location with the `PrintPreviewDialog.Location`..." – Peter Ritchie Sep 10 '12 at 14:02
  • I set that this way : printPreviewDialog1.ClientSize=new Size(200, 300); but again the page is in A4 size and does not change – gwt Sep 10 '12 at 14:15
  • The size of a window/dialog is in pixels, 200x300 pixels is not likely to be A4 size. – Peter Ritchie Sep 10 '12 at 18:46
1

Just check my answer from the link below :

Printing Envelopes from C#

Thank you.

Community
  • 1
  • 1
nassimlouchani
  • 423
  • 4
  • 8