天天看点

Printing multipage output

Printing known-length multipage output

Using the PrintDataGrid control for multipage grids

Example: Printing with multipage PrintDataGrid controls

Multipage print application file

Print output component

Header and footer files

Using the PrintAdvancedDataGrid control

You can print well-formatted multipage output under the following conditions:

When each control fits on a print page or less. You often encounter such jobs when printing a form with fixed-length fields.

When the printed output includes one or more PrintDataGrid controls that are too long to print on a single page, particularly if the control height might vary, depending on the data. A good example of this type of output is a customer order receipt, which starts with the customer information, has an indeterminate number of order line items, and ends with total information.

If you know the length of each component in a multipage document, you can create a separate print layout component for each page you print, and specify each layout page in a separateaddObject()method, as follows:

When a DataGrid control with many rows does not fit on a single screen in your application, you typically have scroll bars that let users view all the data. When you print the DataGrid control, the output is the same as the screen display. Therefore, if your DataGrid control has rows or columns that are not immediately visible, they do not print. If you replace the DataGrid control with a PrintDataGrid control that does not have a height specified (or has a large height), you print all the rows, but some rows could be partially printed on the bottom of one page and partially printed at the top of another, as you often see with HTML printout.

You can solve these problems by using the following features of the PrintDataGrid control. These features let you correctly print grids that contain multiple pages of data without splitting rows across pages:

<dl></dl>

<dt>sizeToPage property</dt>

<dd>Makes the printed data grid contain only full rows.</dd>

<dt>nextPage() method</dt>

<dd>Gets the next printable page of data.</dd>

<dt>validNextPage property</dt>

<dd>Istrueif printing the data requires an additional page.</dd>

A PrintDataGrid page consists of the rows that are visible in the control’s current view. Suppose, for example, that a PrintDataGrid control has a height of 130 pixels. The total height of each row and header is 30 pixels, and the control’s data provider has 10 rows. In this situation, the printed PrintDataGrid page contains only three complete data rows, plus the header. ThesizeToPageproperty specifies whether to include a fourth and partial data row.

ThesizeToPageproperty, which istrueby default, causes the PrintDataGrid control to remove any partially visible or empty rows and to resize itself to include only complete rows in the current view. For the data grid described in the preceding paragraph, when this property istrue, the DataGrid shrinks to show three complete data rows, and no incomplete rows; if the attribute isfalse, the grid includes a partial row at the bottom.

The following properties provide information on page sizing that are affected by thesizeToPageproperty:

Property

Description

Contains the height of the grid, in pixels, that results if thesizeToPageproperty istrue. If thesizeToPageproperty istrue, thecurrentPageHeightproperty equals theheightproperty.

Contains the grid height that results if thesizeToPageproperty isfalse. If thesizeToPageproperty isfalse, theoriginalHeightproperty equals theheightproperty.

In most applications, you leave thesizeToPageattribute at its default value (true), and use theheightproperty to determine the grid height.

ThesizeToPageproperty does not affect the way the page breaks when a single PrintDataGrid control page is longer than a print page. To print multipage data grids without splitting rows, you must divide the grid items into multiple views by using thenextPage()method, as described in the following section.

ThevalidNextPageproperty istrueif the PrintDataGrid control has data beyond the rows that fit on the current print page. You use it to determine whether you need to format and print an additional page.

ThenextPage()method lets you page through the data provider contents by setting the first row of the PrintDataGrid control to be the data provider row that follows the last row of the previous PrintDataGrid page. In other words, thenextPage()method increases the grid’sverticalScrollPositionproperty by the value of the grid’srowCountproperty.

The following code shows a loop that prints a grid using multiple pages, without having rows that span pages:

The section Example: Printing with multipage PrintDataGrid controls shows how to use thenextPage()method to print a report with a multipage data grid.

When you use a PrintDataGrid control to print a single data grid across multiple pages, you queue each page of the grid individually. If your application customizes each page beyond simply using thenextPage()method to page through the PrintDataGrid, you must call thevalidateNow()method to update the page layout before you print each page, as shown in Print output component.

The following example prints a data grid in which you can specify the number of items in the data provider. You can, therefore, set the DataGrid control contents to print on one, two, or more pages, so that you can see the effects of different-sized data sets on the printed result.

The example also shows how you can put header information before the grid and footer information after the grid, as in a shipping list or receipt. It uses the technique of selectively showing and hiding the header and footer, depending on the page being printed. To keep the code as short as possible, the example uses simple placeholder information only.

The application consists of the following files:

The application file displays the form to the user, including TextArea and Button controls to set the number of lines and a Print button. The file includes the code to initialize the view, get the data, and handle the user’s print request. It uses the FormPrintView MXML component as a template for the printed output.

The FormPrintView.mxml file formats the printed output. It has two major elements:

The print output template includes the PrintDataGrid control and uses two MXML components to format the header and footer contents.

TheshowPage()function determines which sections of the template to include in a particular page of the output, based on the page type: first, middle, last, or single. On the first page of multipage output, theshowPage()function hides the footer; on the middle and last pages, it hides the header. On a single page, it shows both header and footer.

The FormPrintHeader.mxml and formPrintFooter.mxml files specify the contents of the start and the end of the output. To keep the application simple, the header has a single, static Label control. The footer displays a total of the numbers in the Quantity column. In a more complete application, the header page could have, for example, a shipping address, and the footer page could show more detail about the shipment totals.

The files include detailed comments explaining the purpose of the code.

The following code shows the multipage print application file:

The executing SWF file for the previous example is shown below:

The following lines show the FormPrintView.mxml custom component file:

The following lines show the FormPrintHeader.mxml file.

The following lines show the FormPrintFooter.mxml file:

The PrintAdvancedDataGrid and PrintOLAPDataGridcontrols provide the same functionality for the AdvancedDataGrid and OLAPDataGrid controls as the PrintDataGrid control does for the DataGrid control. For more information, see Using the PrintDataGrid control for multipage grids.

The following example uses the PrintAdvancedDataGrid control to print an instance of the AdvancedDataGrid control.

The contents of the included SimpleHierarchicalData.as file are as follows:

This example uses thePrintAdvancedDataGrid.sourceproperty to initialize the PrintAdvancedDataGrid control from the AdvancedDataGrid control.

To support the AdvancedDataGrid control, the PrintAdvancedDataGrid control adds the following properties not available in the PrintDataGrid control:

Iftrue, allow some interactions with the control, such as column resizing, column reordering, and expanding or collapsing nodes. The default value isfalse.

Iftrue, display the folder and leaf icons in the navigation tree. The default value istrue.

Initialize the PrintAdvancedDataGrid control and all of its properties from the specified AdvancedDataGrid control.

Indicates that the data provider contains data rows that precede the rows that the PrintAdvancedDataGrid control currently displays.