14

Please help me on how to set my paper size in c# code. I am using the API printDocument.

Below is my code:

 ppvw = new PrintPreviewDialog();
 ppvw.Document = printDoc;
 ppvw.PrintPreviewControl.StartPage = 0;
 ppvw.PrintPreviewControl.Zoom = 1.0;
 ppvw.PrintPreviewControl.Columns = 10;


 // Showing the Print Preview Page
 printDoc.BeginPrint += new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
 printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);


 if (ppvw.ShowDialog() != DialogResult.OK)
 {
     printDoc.BeginPrint -= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
     printDoc.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
 }


 printDoc.PrinterSettings.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("a2", 5.0,5.0);
 printDoc.Print();
ag93
  • 343
  • 4
  • 17

5 Answers5

29
PrinterSettings ps = new PrinterSettings();
PrintDocument recordDoc = new PrintDocument();
recordDoc.PrinterSettings = ps;

here's a way to set the paper size by kind like 'A4' for example

IEnumerable<PaperSize> paperSizes = ps.PaperSizes.Cast<PaperSize>();
PaperSize sizeA4 = paperSizes.First<PaperSize>(size => size.Kind == PaperKind.A4); // setting paper size to A4 size
recordDoc.DefaultPageSettings.PaperSize = sizeA4;

and here's another way to set a custom paper size

recordDoc.DefaultPageSettings.PaperSize = new PaperSize("210 x 297 mm", 800, 800);
PrintPreviewDialog ppvw = new PrintPreviewDialog();
ppvw .Document = recordDoc;
ppvw.ShowDialog();

Hope it works.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Mawardy
  • 3,618
  • 2
  • 33
  • 37
  • 7
    can you please tell, what do you mean by 800, 800? – Fawad Jul 19 '16 at 05:57
  • 2
    @Fawad in this constructor the PaperSize class accepts 3 arguments : 1-papersize name it has no relation to the 2nd & 3rd arguments 2-width of the paper 3-height of the paper check those links for detailed info. https://msdn.microsoft.com/en-us/library/7dbh1cyh(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.drawing.printing.papersize(v=vs.110).aspx – Mawardy Jul 24 '16 at 21:54
  • 3
    @Fawad: 800, 800 specifies a page size of 8" x 8". (It is width and height, in hundredths of an inch.) – Jeff Roe Mar 31 '17 at 16:33
1

Constructor for paper size is PaperSize(String, Int32, Int32)

5.0 (5) X 5.0 (5) is too little,,, Unless "Custom Size" is your string.. or 420 x 594 for A2...

and also try enumerating foreach PaperSize size in printer.PaperSizes and check whether A2 is there.. or not..

By default it sets Rawkind to custom, You also need to set Rawkind as mentioned in http://msdn.microsoft.com/en-us/library/system.drawing.printing.papersize.rawkind.aspx

lakshmanaraj
  • 4,145
  • 23
  • 12
  • thank for reply ... but if i give 420X594 it show only A4 size paper .. please tell me how to set cusom size –  Mar 26 '09 at 09:01
0

I am using Visual Basic, with this code I can get the form to show it all in printpreview, still print a slitely cut page on the right.

PrintForm1.Form = Me
PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = True
PrintForm1.PrinterSettings.DefaultPageSettings.PaperSize = New Printing.PaperSize("Custom", Me.Height, (Me.Width + 47))
PrintForm1.PrinterSettings.DefaultPageSettings.Margins = New Printing.Margins(3, 3, 3, 3)
PrintForm1.PrinterSettings.DefaultPageSettings.PaperSize.RawKind = Printing.PaperKind.A4Small
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview    'PrintForm1.PrintAction = Printing.PrintAction.PrintToPrinter
PrintForm1.Print()  'PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)  '
Tom
  • 26,212
  • 21
  • 100
  • 111
Svig
  • 1
0

Try this. I think this code will help you to solve this problem.

Private Sub bt_Save_Click(sender As Object, e As EventArgs) Handles bt_Save.Click
    MsgBox("Saved", MsgBoxStyle.Information)
    If MsgBox("you want to print now?", MsgBoxStyle.Question + vbOKCancel, "Printing") = MsgBoxResult.Ok Then
        Try
            PrintPreviewDialog1.Document = ImportBillPrintDocument
            ImportBillPrintDocument.PrinterSettings.DefaultPageSettings.PaperSize = pkCustomSize1
            ImportBillPrintDocument.DefaultPageSettings.PaperSize = pkCustomSize1

            PrintPreviewDialog1.WindowState = FormWindowState.Maximized
            PrintPreviewDialog1.ShowDialog()
        Catch ex As Exception

        End Try

    End If
End Sub
  • How does it solve the problem if you don't document what the pkCustomSize1 value is? Never use an empty Try-Catch. Question was in c#. – LarsTech Oct 24 '18 at 17:22
  • `Dim pkCustomSize1 As New PaperSize("Custom", 810, HeightVar)` I can't writing c#, just written vb.net only. – Sarunpong P. Jan 12 '19 at 08:27
  • if you don't using `Try-Catch` this code couldn't be working. – Sarunpong P. Jan 12 '19 at 08:34
  • I didn't say not to use a Try-Catch — "empty" was the important word. – LarsTech Jan 13 '19 at 21:14
  • OK, I just use `try-catch` with empty because I don't want program to show the error exception in that time but now I insert `msgbox()` to show what error code founded when the users is using to find the error position easily. But if you want to get an exception can insert `msgbox(ex.tostring)` – Sarunpong P. Jan 19 '19 at 03:45
-1

You can use as follws and user can set the page size within the setting form.

        private void button1_Click(object sender, EventArgs e)
        {
            PrintDialog printdg = new PrintDialog();
            if (printdg.ShowDialog() == DialogResult.OK)
            {
                PrintDocument pd = new PrintDocument();
                pd.PrinterSettings = printdg.PrinterSettings;
                pd.PrintPage += PrintPage;
                pd.Print();
                pd.Dispose();
            }
        }
        private void PrintPage(object o, PrintPageEventArgs e)
        {
           // Printng logic
        }
Thilina H
  • 5,754
  • 6
  • 26
  • 56