Batch of CRYSTAL REPORTS into PDF with Parameters

Posted on 2004-11-01
Last Modified: 2008-02-01
I have a batch process that gets Crystal reports out of a certain folder that turns them into PDF's without parameters and now I have CR that need to have parameters fed into them.  

They could be all different types of parameters, including ones with multiple parameters.  I need to be able to feed the parameters into different reports doing a batch process that runs.

I am guessing I would use a table to feed these into CR?  I am using VB.NET  This is an extention of another question which here is the code.  Thanks ahead of time vikingg97

Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO
Imports Microsoft.VisualBasic

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button1.Location = New System.Drawing.Point(256, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(144, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Generate PDF from CR"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(616, 461)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Place the following name spaces at the top of the code page
        Dim fileList() As String = System.IO.Directory.GetFiles("c:\inetpub\wwwroot\MCNETWebSite\IT_Work\")

        For Each fileSource As String In fileList


            If Strings.Right(fileSource, 4) = ".rpt" Then
                'Crystal Export Code Here
                Dim CrReport As New ReportDocument
                CrReport.Load(fileSource, OpenReportMethod.OpenReportByDefault)
                Dim CrExportOptions As ExportOptions
                Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions
                Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions

                ' Set the destination path and file name
                ' 41 is the spot where I need to get file name 41 (34 old)
                Dim mid As String
                mid = Strings.Mid(fileSource, 41)

                ' Gets the length of the filename
                Dim LengthObjName As String
                LengthObjName = Strings.Len(mid)

                ' Gets charactor to start at for taking out of the .rpt
                Dim LengthFrom_rpt As String
                LengthObjName = CInt(LengthObjName - 4)

                ' Gets the actual file name minus the .rpt extension
                Dim strVarName As String
                strVarName = Strings.Left(mid, LengthObjName)

                'Getobj = Microsoft.VisualBasic.GetObject(fileSource)
                Dim pdf_path As String
                pdf_path = "c:\CR_Convert_to_PDF\" & strVarName & ".pdf"
                CrDiskFileDestinationOptions.DiskFileName = pdf_path

                'Specify a page range (optional)
                CrFormatTypeOptions.FirstPageNumber = 1
                CrFormatTypeOptions.LastPageNumber = 3
                CrFormatTypeOptions.UsePageRange = True

                'Set export options
                CrExportOptions = CrReport.ExportOptions

                With CrExportOptions
                    'Set the destination to a disk file
                    .ExportDestinationType = ExportDestinationType.DiskFile
                    'Set the format to PDF
                    .ExportFormatType = ExportFormatType.PortableDocFormat
                    'Set the destination options to DiskFileDestinationOptions object
                    .DestinationOptions = CrDiskFileDestinationOptions
                    .FormatOptions = CrFormatTypeOptions
                End With

                ' Trap any errors that occur on export
                    'Export the report
                    MessageBox.Show("PDF File Generated from " & fileSource & " into location: " & pdf_path)
                Catch err As Exception
                End Try

            End If
        MessageBox.Show("All pdf files created successfully.")

    End Sub
End Class
Question by:vikingg97
    LVL 10

    Accepted Solution

    You can do this with an application

    1. Set the parameters to the report document runtime
    2. Get the full report
    3. Export the final report into available format

    This is pretty long code so I hope you know the object model
    I am pasting the section to set the parameetrs in runtime

    internal static void CreateParameter(ref ReportDocument report, string ParameterName, object value)
    ParameterDiscreteValue  ParameterDiscreteValue;
    ParameterValues  ParameterValues;
    ParameterFieldDefinition param = report.DataDefinition.ParameterFields[ParameterName];
    ParameterValues = new CrystalDecisions.Shared.ParameterValues();
    ParameterDiscreteValue = new CrystalDecisions.Shared.ParameterDiscreteValue();      
    ParameterDiscreteValue.Value = value;      

    After this you set the report document as the viewers datasource
    crystalviewer1.reportsource = reportdocument;

    Then you do the exporting in code

    LVL 1

    Author Comment

    This is my first VB.NEt application dealing with CR, so I am going to ask if you could give it to me more basically.  

    Unsure what you mean on
    --1. Set the parameters to the report document runtime
    --2. Get the full report

    Also I don't know what you mean about what you mean about knowing the object model and how long it is, and setting the report document as the viewers datasource crystalviewer1.reportsource = reportdocument;  Does that mean in the actual report?  

    I don't want to even see the reports while its processing, I just want them to all to export from in a certain folder feeding parameters making a PDF.  

    once again I appreciate ahead of time.  vikingg97
    LVL 10

    Expert Comment

    The things you are asking has to be done by the application. You have to write and deploy an application. The application has to set the parameters runtime to the report , then display the report to the user. If you dont want the user t osee the report then you can directly send it to the exporting module.

    It is not so easy to write this application. First you have to decide what language is available t oyou. What version of crystal do you have and how much does it let you make ru time customizations. Maybe after answering these questions, you will see that you wont be able to do it with your current version and need to uprade
    LVL 1

    Author Comment

    I have all the latest technologies available, using CR 10 and VB.NET through VS.NET.  I have already written this application to export the CR to PDF.  This works with a basic crystal report with no parameters pushed in.  I am just wanting to pass parameters into reports then have it convert to a pdf.  

    If you can just help me get this done, for example with say 2 reports with parameters, then that is for the most part what I need.  I posted the code above with the working process converting CR into pdf.  Lets say I want a parameter of date and plancode on one report and name and version on the second report.  

    Thanks  vikingg97
    LVL 10

    Expert Comment

    That is perfect hope you are using c#. The code I posted is C# for parameters. Use that you will be good to go. If you dont undrstand anythind ask me. It works because I have the same technologies and doing it right now

    Good luck brother

    LVL 1

    Author Comment

    Like I said I am not using C#, I am using VB.NET.  So the C# code you have posted isn't helping this beginner.  

    I said before that if someone could maybe show me an example in VB.NET and just help me get this started, for example with say 2 reports with parameters, then that is for the most part what I need.  I posted the code above with the working process converting simnple CR into pdf.  Lets say I want a parameter of date and plancode on one report and name and version on the second report.  

    If someone could help with that part of it just going through 2 reports with those parameters with the code I have already posted, I would appreciate it VERY MUCH. This is kinda urgent.
    LVL 1

    Author Comment

    ANYONE? Help please?
    LVL 10

    Expert Comment

    I dont have time to convert the code to It is very similar though. And that code works because I am using in 100 reports.
    LVL 1

    Author Comment

    So that snippet you added works for converting CR to PDF with any multitude of parameters?  
    LVL 10

    Expert Comment

    that code sets the parameters of the report in runtime

    You have to write little bit more to export t opdf as well.
    LVL 10

    Expert Comment

    Glad I could help


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Hot fix for .Net Crystal Reports 10.2.3600.0 to fix problems with sub reports running on 64 bit operating systems ISSUE: Reports which contain subreports fail with error "Missing Parameter Value" DEPLOYMENT SERVER OS: Windows 2008 with 64 bi…
    Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now