vb.net 以编程方式为 Crystal Report 设置自定义纸张大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17835212/
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
Programmatically set Custom Paper Size for Crystal Report
提问by Vinod John
I have created custom paper Size "SUPP 15 x 14" in Setting - Printers - File - Server Properties.
Now I'm trying to set custom Paper Size for Crystal Report using VB.net 2005.
我已经创建了自定义纸张大小“SUPP 15 x 14”,Setting - Printers - File - Server Properties.
现在我正在尝试使用 VB.net 2005 为 Crystal Report 设置自定义纸张大小。
When I run report from VB.net, the Crystal report viewer shows the correct preview for custom paper size but when I give print command it prints with the default printer paper size. (e.g Letter)
当我从 VB.net 运行报告时,Crystal 报告查看器显示自定义纸张尺寸的正确预览,但是当我给出打印命令时,它会使用默认的打印机纸张尺寸进行打印。(例如字母)
Here's the code I'm using to print:
这是我用来打印的代码:
Public Sub ...
'...
Dim ObjCrReport as new ReportDocument
'...
ObjCrReport.SetDataSource(ObjPrintDataSet.Tables("PrintData"))
SetReportPageSize("SUPP 15 x 14", 1)
'...
End Sub
Private Sub BtnPrintDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrintDoc.Click
Try
'Print command
ObjCrReport.PrintToPrinter(1, False, 0, 0)
Catch ex As Exception
MessageBox.Show(ex.Message, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Sub SetReportPageSize(ByVal mPaperSize As String, ByVal PaperOrientation As Integer)
Try
Dim ObjPrinterSetting As New System.Drawing.Printing.PrinterSettings
Dim PkSize As New System.Drawing.Printing.PaperSize
ObjPrinterSetting.PrinterName = "Epson FX1170"
For i As Integer = 0 To ObjPrinterSetting.PaperSizes.Count - 1
If ObjPrinterSetting.PaperSizes.Item(i).PaperName = mPaperSize.Trim Then
PkSize = ObjPrinterSetting.PaperSizes.Item(i)
Exit For
End If
Next
If PkSize IsNot Nothing Then
Dim myAppPrintOptions As CrystalDecisions.CrystalReports.Engine.PrintOptions = ObjCrReport.PrintOptions
myAppPrintOptions.PrinterName = "Epson FX1170"
myAppPrintOptions.PaperSize = CType(PkSize.RawKind, CrystalDecisions.Shared.PaperSize)
ObjCrReport.PrintOptions.PaperOrientation = IIf(PaperOrientation = 1, _
CrystalDecisions.Shared.PaperOrientation.Portrait, _
CrystalDecisions.Shared.PaperOrientation.Landscape)
End If
PkSize = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
If I use myAppPrintOptions.PaperSize = PaperSize.PaperLegal
, then Print Preview & Printing appear correct, but I want to set custom paper size which is not showing in the PaperSize
class.
如果我使用myAppPrintOptions.PaperSize = PaperSize.PaperLegal
,则打印预览和打印显示正确,但我想设置未在PaperSize
课程中显示的自定义纸张尺寸。
What's wrong with above code? Why is it printing Letter Size where Crystal report preview otherwise shows custom paper in the size preview? Is there a better way to accomplish my goal?
上面的代码有什么问题?为什么在 Crystal 报表预览中打印 Letter Size 时会在尺寸预览中显示自定义纸张?有没有更好的方法来实现我的目标?
回答by Myk Agustin
This Method works with an Epson LX-300+ ii Dot-Matrix Printerand later models
此方法适用于Epson LX-300+ ii 点阵打印机和更新型号
If you are using a Printer especially for Printing Receiptshere are the steps on how to set your printer for desired paper size
如果您使用打印机专门用于打印收据,这里是有关如何将打印机设置为所需纸张尺寸的步骤
First Set up Printer to be used: Go to Devices and Printersin Printers select the Printer you are going to use and click - right click Printer PropertiesClick Preferences...Button. Under MainTab - Change Document Sizeto User Defineda new New Window will appear. in Paper Size Name specify the name (i.e. OR Paper) and change paper width and height as desired Click Savethen OK
首先设置要使用的打印机:转到打印机中的设备和打印机,选择您要使用的打印机并单击 - 右键单击打印机属性单击首选项...按钮。在MainTab - Change Document Sizeto User Defined 下,将出现一个新窗口。在纸张尺寸名称中指定名称(即或纸张)并根据需要更改纸张宽度和高度单击保存然后确定
then set your printer by Pressing right click then set as Default Printer
Add these line of code for your printing. You can still use parameters and datasets.
为您的打印添加这些代码行。您仍然可以使用参数和数据集。
Dim c As Integer
Dim doctoprint As New System.Drawing.Printing.PrintDocument()
doctoprint.PrinterSettings.PrinterName = "EPSON L1300 Series"
Dim rawKind As Integer
For c = 0 To doctoprint.PrinterSettings.PaperSizes.Count - 1
If doctoprint.PrinterSettings.PaperSizes(c).PaperName = "OR Receipts" Then
rawKind = CInt(doctoprint.PrinterSettings.PaperSizes(c).GetType().GetField("kind", Reflection.BindingFlags.Instance Or
Reflection.BindingFlags.NonPublic).GetValue(doctoprint.PrinterSettings.PaperSizes(c)))
Exit For
End If
Next
Report1.PrintOptions.PaperSize = CType(rawKind, CrystalDecisions.Shared.PaperSize)
frmPreview.CrystalReportViewer1.ReportSource = Report1
Report1.PrintToPrinter(1, False, 1, 1)
回答by hytham ali
you can do as this
你可以这样做
var rep = new YursCrystalReport();
var printerSettings = new System.Drawing.Printing.PrinterSettings();
var pSettings = new System.Drawing.Printing.PageSettings(printerSettings);
pSettings.PaperSize = new System.Drawing.Printing.PaperSize("newsize", 3000, 3000);//custom size hundredths (100=1 inch)
pSettings.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
rep.PrintOptions.DissociatePageSizeAndPrinterPaperSize = true;
rep.PrintOptions.CopyFrom(printerSettings, pSettings);