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.