Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Batch of CRYSTAL REPORTS into PDF with Parameters

Posted on 2004-11-01
11
Medium Priority
?
891 Views
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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'Button1
        '
        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"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(616, 461)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    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

            'Microsoft.VisualBasic.GetObject("C:\WindowsApp\WindowsApplication1\error.rpt")
            'MessageBox.Show(uu)

            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
                Try
                    'Export the report
                    CrReport.Export()
                    MessageBox.Show("PDF File Generated from " & fileSource & " into location: " & pdf_path)
                Catch err As Exception
                    MessageBox.Show(err.ToString())
                End Try

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

    End Sub
End Class
0
Comment
Question by:vikingg97
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 10

Accepted Solution

by:
ebolek earned 2000 total points
ID: 12463767
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;      
ParameterValues.Add(ParameterDiscreteValue);
param.CurrentValues.Clear();
param.DefaultValues.Clear();  
param.ApplyCurrentValues(ParameterValues);
}

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

Then you do the exporting in code

Regards
Emre
0
 
LVL 1

Author Comment

by:vikingg97
ID: 12467541
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
0
 
LVL 10

Expert Comment

by:ebolek
ID: 12467572
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
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 1

Author Comment

by:vikingg97
ID: 12467868
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
0
 
LVL 10

Expert Comment

by:ebolek
ID: 12467946
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

Emre
0
 
LVL 1

Author Comment

by:vikingg97
ID: 12473321
Like I said I am not using C#, I am using VB.NET.  So the C# code you have posted isn't helping this vb.net 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.
0
 
LVL 1

Author Comment

by:vikingg97
ID: 12474367
ANYONE? Help please?
0
 
LVL 10

Expert Comment

by:ebolek
ID: 12475513
I dont have time to convert the code to vb.net. It is very similar though. And that code works because I am using in 100 reports.
0
 
LVL 1

Author Comment

by:vikingg97
ID: 12475723
So that snippet you added works for converting CR to PDF with any multitude of parameters?  
0
 
LVL 10

Expert Comment

by:ebolek
ID: 12475741
that code sets the parameters of the report in runtime

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

Expert Comment

by:ebolek
ID: 12523456
Glad I could help

Regards
Emre
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hate sub reports and always consider them the last resort in any reporting solution.  The negative effect on performance and maintainability is just not worth the easy ride they give the report writer.  Nine times out of ten reporting requirements…
There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 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