OK、私は簡単な解決策を見つけるために少し時間を費やしました、そして私は以下を得ました:
まず、Crystal Reportsではなく、Windowsレポート(rdlcファイル)を使用しました。Windowsレポートはよりシンプルで簡単で、背景として画像を追加し、この画像の上に文字列パラメーター(まさに私が必要としていたもの)を参照するいくつかのTextBoxを追加することができます。これらは既定でVisualStudioにあり、Visual Studioでレポートをデザインします(ソリューションエクスプローラーで右クリック->レポートの追加)
次に、レポートをPDFファイルに変換するコードサンプルを見つけ、それを使用して次のクラスを記述しました。
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();
}
}
できます。それを試してみてください。2つのことに注意してください:
reportFilePathとpdfFilePathには正しいパスを使用してください。(pdfFilePathは、私にとっては非相対パスでのみ機能しました)
rdlcレポートにすべてのパラメーターを正しい名前で追加したことを確認してください。これらは、[表示]->[レポートデータ]->[新しいパラメーターの追加]([パラメーター]を右クリック)で追加できます。こちらもご覧ください:rdlcレポートにパラメータを作成する
私が助けてくれたことを願っています。それは私にとって素晴らしい働きをしました。