C# 设置纸张尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/684801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the paper size
提问by
Please help me on how to set my paper size in c# code. I am using the API printDocument.
请帮助我如何在 C# 代码中设置我的纸张大小。我正在使用 API 打印文档。
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();
回答by lakshmanaraj
Constructor for paper size is PaperSize(String, Int32, Int32)
纸张大小的构造函数是 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...
5.0 (5) X 5.0 (5) 太小,,,除非“自定义尺寸”是您的字符串.. 或 A2 的 420 x 594 ...
and also try enumerating foreach PaperSize size in printer.PaperSizes and check whether A2 is there.. or not..
并尝试在printer.PaperSizes中枚举foreach PaperSize尺寸并检查A2是否存在..与否..
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
默认情况下,它将 Rawkind 设置为自定义,您还需要按照http://msdn.microsoft.com/en-us/library/system.drawing.printing.papersize.rawkind.aspx 中所述设置 Rawkind
回答by Svig
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.
我正在使用 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) '
回答by Thilina H
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
}
回答by Mawardy
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
这是一种按类型设置纸张尺寸的方法,例如“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;
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.
希望它有效。
回答by Sarunpong P.
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