Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

vb.net - Programmatically set Custom Paper Size for Crystal Report

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.

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)

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This Method works with an Epson LX-300+ ii Dot-Matrix Printer and later models

If you are using a Printer especially for Printing Receipts here are the steps on how to set your printer for desired paper size

First Set up Printer to be used: Go to Devices and Printers in Printers select the Printer you are going to use and click - right click Printer Properties Click Preferences... Button. Under Main Tab - Change Document Size to User Defined a 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 Save then OK

enter image description here 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...