0

I just want to put some values from my database on an A4 page (I have a JPG template) and create a PDF book/report with an insert per page. I have easily done it with NetBeans Java Jasper Reports using iReport editor.

It seems so much more difficult in Visual Studio C# Crystal Reports. I've really searched for tutorials for Crystal Reports and none of them is using an A4 image as a template. Please help me if you know any such tutorials.

I prefer a solution which works programmatically and not through a wizard. I already manage my database with my program. I just need the report and some documentation of how to give input values to the report. I don't even need for the report to access the database. I can get all the values in my program. The best solution for me is a template with my JPG file as background and boxes (like textboxes) where I give text from my program through parameters of a function. Like Jasper Reports / iReport.

Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101

2 Answers2

1

OK, I spend some time to find an easy solution and I got the following:

First of all I didn't use Crystal Reports, but Windows Reports (rdlc files). Windows Reports are more simple and easy and it is possible to add an image as a background and above this image some TextBoxes that refer to String parameters (exactly what I needed). They are in Visual Studio by default and you design your report in Visual Studio (right click in Solution Explorer --> Add Report)

Then I found a code sample which converts the report to PDF files and I used it to write the following Class:

public class XReport
{
    private ReportViewer reportViewer = new ReportViewer();

    public XReport()
    {
    }
    public XReport(String reportFilePath)
    {
        setReportFile(reportFilePath);
    }
    // set rdlc file
    public void setReportFile(String reportFilePath)
    {
        reportViewer.Reset();
        reportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
        reportViewer.LocalReport.ReportPath = reportFilePath;
    }
    public void setParameters(List<ReportParameter> parameters)
    {
        reportViewer.LocalReport.SetParameters(parameters.ToArray());
        reportViewer.LocalReport.Refresh();
    }
    public void setParameters(Dictionary<String, String> parameters)
    {
        XList<ReportParameter> parameterList = new List<ReportParameter>();
        XList<String> parameterKeys = parameters.getKeys();
        foreach (String parameterKey in parameterKeys) {
            parameterList.Add(new ReportParameter(parameterKey, parameters.get(parameterKey))); }
        setParameters(parameterList);
    }
    public void exportToPDF(String pdfFilePath)
    {
        Warning[] warnings;
        string[] streamids;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;
        byte[] bytes = null;
        // render PDF file
        try { bytes = reportViewer.LocalReport.Render( "PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings); }
        catch (Exception ex) { System.Console.Write(ex.Message); }
        // write PDF file
        using (FileStream fs = new FileStream(pdfFilePath, FileMode.Create)) { fs.Write(bytes, 0, bytes.Length); fs.Close(); }
        // release reportViewer resources to avoid errors
        reportViewer.LocalReport.ReleaseSandboxAppDomain();
    }
}

It works. Try it. Be careful with 2 things:

  • Use correct paths for reportFilePath and pdfFilePath. (pdfFilePath worked only with non-relative path for me)

  • Make sure that you have added all parameters with their correct names in your rdlc report. You can add them at View --> Report Data --> Add New Parameter (right click at Parameters). Also see this: Create a parameter in rdlc report

Hope I helped. It worked for me great.

Community
  • 1
  • 1
Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101
0

You can set the paper size of the report using the below mentioned option in Crystal itself. Open the report first and then go to File -> Page Setupenter image description here

Amal Dev
  • 1,938
  • 1
  • 14
  • 26