简体   繁体   中英

Setting the paper size

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

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

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)  '

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

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
        }

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