设置纸张尺寸

时间:2009-03-26 07:20:35

标签: c# .net printing

请帮我讲解如何用c#代码设置纸张尺寸。我正在使用API​​ printDocument。

以下是我的代码:

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

5 个答案:

答案 0 :(得分:16)

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

这是一种通过类似'A4'设置纸张尺寸的方法,例如

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;

这是另一种设置自定义纸张尺寸的方法

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

希望它有效。

答案 1 :(得分:0)

纸张尺寸的构造函数是PaperSize(String,Int32,Int32)

5.0(5)X 5.0(5)太少了,,,除非“自定义尺寸”是你的字符串...或者是A2 x 420 x 594 ...

并尝试在printer.PaperSizes中枚举foreach PaperSize大小并检查A2是否存在..或者不是..

默认情况下,它将Rawkind设置为自定义,您还需要按照http://msdn.microsoft.com/en-us/library/system.drawing.printing.papersize.rawkind.aspx中提到的设置Rawkind

答案 2 :(得分:0)

我正在使用Visual Basic,使用此代码我可以在printpreview中显示所有内容,仍然在右侧打印一个简单的剪切页面。

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

答案 3 :(得分:0)

您可以按如下方式使用,用户可以在设置表单中设置页面大小。

        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
        }

答案 4 :(得分:0)

尝试一下。我认为这段代码将帮助您解决此问题。

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