Print Without Viewing
In Windows Forms applications, you can print reports without previewing them using the Print method that is implemented as an extension method for the SectionDocument and PageDocument classes. Because it is an extension of the PrintExtension class, Print is not listed as a method on either of the document classes. You can find it in the GrapeCity.ActiveReports.Viewer.Win.v10 assembly under the GrapeCity.ActiveReports namespace. There are two ways to call this extended Print method. You can call it on the document object, or call it on the PrintExtension object and pass in the document. I've provided both ways in the code samples below, and commented out one of them. Also, you can see that one of them uses a Section report and the other uses a Page report.
' Visual Basic
Imports GrapeCity.ActiveReports
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sectionReport As New GrapeCity.ActiveReports.SectionReport()
Dim xtr As New System.Xml.XmlTextReader("..\\..\\PurchaseOrder.rpx")
sectionReport.LoadLayout(xtr)
xtr.Close()
sectionReport.Run()
Dim sectionDocument = sectionReport.Document
' sectionDocument.Print ' OR
PrintExtension.Print(sectionDocument, True, True)
End Sub
End Class
// C#
using System;
using System.Windows.Forms;
using GrapeCity.ActiveReports;
namespace winFormsPrintNoViewer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string file_name = @"..\\..\\Catalog.rdlx";
GrapeCity.ActiveReports.PageReport pageReport = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(file_name));
GrapeCity.ActiveReports.Document.PageDocument pageDocument = new GrapeCity.ActiveReports.Document.PageDocument(pageReport);
// pageDocument.Print(); // OR
PrintExtension.Print(pageDocument, true, true);
}
}
}
Troubleshooting
If you see error messages like these, the tips below will help.
VB.NET
- 'Print' is not a member of 'PageDocument'.
- 'Print' is not a member of 'SectionDocument'.
- 'PrintExtension' is not declared. It may be inaccessible due to its protection level.
C#
- 'PageDocument' does not contain a definition for 'Print' and no extension method 'Print' accepting a first argument of type 'PageDocument' could be found (are you missing a using directive or an assembly reference?)
- 'SectionDocument' does not contain a definition for 'Print' and no extension method 'Print' accepting a first argument of type 'SectionDocument' could be found (are you missing a using directive or an assembly reference?)
- The name 'PrintExtension' does not exist in the current context.
Reference the Windows Viewer Assembly
To use the Print method, add a reference to the GrapeCity.ActiveReports.Viewer.Win.v10 assembly in your application.
Explicitly Import the Namespace
To enable the Print method, you also need to explicitly import the GrapeCity.ActiveReports namespace. Add code like the following at the beginning of the code file that calls the Print method.
'VB.NET
Imports GrapeCity.ActiveReports
//C#
using GrapeCity.ActiveReports;
Important: When you upgrade old ActiveReports projects, especially those created using ActiveReports version 6 or lower, the reference and code to import isn't added automatically with the ActiveReports 10 Upgrade tool. You can find more information on printing in Print Methods In ActiveReports.