Output an ActiveReports report as a PDF file

Page update date :
Page creation date :

Operating environment

Windows
  • Windows 10 Pro 22H2
Visual Studio
  • Visual Studio 2022 Community Edition
ActiveReports for .NET
  • ActiveReports for .NET 16.0J

Prerequisites

Windows
  • Windows 8.1
  • Windows 10
  • Windows 11
  • Windows Server 2012
  • Windows Server 2012 R2
  • Windows Server 2016
  • Windows Server 2019
  • Windows Server 2022
Visual Studio
  • Visual Studio 2017
  • Visual Studio 2019
  • Visual Studio 2022
.NET Framework
  • NET Framework 4.6.2
  • NET Framework 4.7
  • NET Framework 4.8
.NET
  • .NET Core 3.1
  • .NET 5
  • .NET 6
ActiveReports for .NET
  • ActiveReports for .NET 16.0J

How to output to a PDF file

If a user wants a report as a PDF file, the quickest way to provide it is to have the report viewed in the viewer. In addition to displaying reports, the viewer has a function to print and output files such as PDF, so if you display the viewer, you can ask the user to output the PDF file.

However, in some cases, you may not want to bother the user, or you may want to systematically direct the PDF file. This section describes how to programmatically output a PDF file directly from a report.

PDF File Output Methods and Limitations

There are several ways to programmatically output PDF files, but not all of them are acceptable. This varies depending on the version of ActiveReports, the platform, the framework, and the type of report it performs. For example, in ActiveReports 16, section and page reports are output differently programmatically. An example of this can also be found on the official page.

For this reason, here are some examples of programmatic PDF output that you can use as a reference to find the one that suits your project.

About the sample report

The report file included in the template for the new project is empty, so we will use the official sample report. Here are some suggestions, so please download them from one of the following sites.

In this case, we will use the following sample report.

  • Page Report : Estimate_page_ipa.rdlx
  • Section report: Invoice.rpx (included in the WebSample16 sample project)

Render and generate PDFs (Page Reports/RDL Reports)

There is an "export filter" for outputting PDFs, but in version 16 it can only be used for section reports.

Therefore, page reports/RDL reports need to be generated with the "Drawing Extension" as described on the above page.

First of all, I will create a project, but this time I will make it with a simple console application. It's just as easy to apply to the desktop and web versions as it is to the web version.

When you create a project, select Console App.

After you create the project, add the required packages.

Here's what you'll add: Please do not get the latest version, but use the version described below. It seems that some package combinations do not work properly with the latest version. A warning mark is displayed, but ignore it because the dependencies do not match. However, there is a possibility that it will change with version upgrades in the future, so please adjust it each time. GrapeCity.Documents.Imaging GrapeCity.Documents.Pdf and are required to render to PDF.

  • GrapeCity.ActiveReports.Export.Pdf (16.4.0)
  • GrapeCity.Documents.Imaging (6.0.3)
  • GrapeCity.Documents.Pdf (6.0.3)

Add a report file to your project. The location can be anywhere, but this time I'm Reports creating a folder and putting it in there. This placement location affects the path that you specify later.

Next, open the properties of the report you added.

Set the build action to Embedded Resource. This means embedding the resource inside an EXE (DLL). The report is uniquely loaded on the desktop as it is on the web.

Next, write the code to load the report and print it to PDF. Since it is a console application, it should be described in Program.cs as it is, but if it is a desktop, it should be described in a button event, etc., and if it is ASP.NET, it should be described in the place where it is processed when it is posted.

// レポートファイルがあるパス
var reportPath = "ConsoleApp1.Reports.Estimate_page_ipa.rdlx";

// 埋め込みリソースを Stream をして取り出す
var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(reportPath);
if (stream == null) return;

// ページレポートに変換します
using var sr = new StreamReader(stream);
var pageReport = new GrapeCity.ActiveReports.PageReport(sr);

// PDF 描画拡張を定義します
var pdfRenderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();

// メモリストリームとして出力する定義です
var outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();

// レポートを PDF にレンダリングします
pageReport.Document.Render(pdfRenderingExtension, outputProvider);

// Stream に変換します
var ps = outputProvider.GetPrimaryStream();
var outputStream = ps.OpenStream();

I won't explain in detail what kind of processing it is because it is written in the comments of the code. The key is to specify the location of the report file in the first line. The format is as <アセンブリ名>.<レポートファイルのパス> follows, and all path separators are periods.

The last one is Stream, so please convert it to PDF according to the processing content.

For example, if you want to save it as a local file, add the following.

// ファイルにストリームを書き出します
using var fs = new FileStream("Output.pdf", FileMode.Create);
outputStream.CopyTo(fs);

If you want to download it with ASP.NET, add the following.

return File(outputStream, "application/pdf", "Output.pdf");

Try opening the output PDF file to see if it's okay.

Output PDF using export filters (section reports)

Use the PDF export filter available for section reports.

It is created in a console project in the same way as for page reports. The code and settings you create here can be applied to other projects.

After you create the project, add the required packages. This time, we will output with an export filter, so we will only add one.

  • GrapeCity.ActiveReports.Export.Pdf (16.4.0)

Add a report file to your project. The location can be anywhere, but this time I'm Reports creating a folder and putting it in there. This placement location affects the path that you specify later.

Next, open the properties of the report you added.

Set the build action to Embedded Resource. This means embedding the resource inside an EXE (DLL). The report is uniquely loaded on the desktop as it is on the web.

Next, write the code to load the report and print it to PDF. Since it is a console application, it should be described in Program.cs as it is, but if it is a desktop, it should be described in a button event, etc., and if it is ASP.NET, it should be described in the place where it is processed when it is posted.

// レポートファイルがあるパス
var reportPath = "ConsoleApp1.Reports.Invoice.rpx";

// 埋め込みリソースを Stream をして取り出す
var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(reportPath);
if (stream == null) return;

// Stream を XML として読み込めるようにします
var xr = System.Xml.XmlReader.Create(stream);

// セクションレポートとして読み込みます
var sectionReport = new GrapeCity.ActiveReports.SectionReport();
sectionReport.LoadLayout(xr);
sectionReport.Run();

// PDF のエクスポートフィルターを作成します
var pdfExport = new GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport();

// メモリストリームに PDF の情報を書き出します
var outputStream = new System.IO.MemoryStream();
pdfExport.Export(sectionReport.Document, outputStream);
outputStream.Seek(0, SeekOrigin.Begin);

I won't explain in detail what kind of processing it is because it is written in the comments of the code. The key is to specify the location of the report file in the first line. The format is as <アセンブリ名>.<レポートファイルのパス> follows, and all path separators are periods.

The last one is Stream, so please convert it to PDF according to the processing content.

For example, if you want to save it as a local file, add the following.

// ファイルにストリームを書き出します
using var fs = new FileStream("Output.pdf", FileMode.Create);
outputStream.CopyTo(fs);

If you want to download it with ASP.NET, add the following.

return File(outputStream, "application/pdf", "Output.pdf");

Try opening the output PDF file to see if it's okay.