JClass PageLayout

PreviousNextIndex

1

JClass PageLayout Basics

Overview of JClass PageLayout  Basic Steps for Creating a JClass PageLayout Document

A Simple JClass PageLayout Program  JClass PageLayout Objects  Creating Flow  Printing 


1.1 Overview of JClass PageLayout

JClass PageLayout uses a flow-markup approach for creating multipage documents to be displayed or converted for printing. The major components of JClass PageLayout are:

Other important top-level components are JCTextStyle (stores character- and paragraph-level attributes controlling the text appearance), JCDrawStyle (contains attributes of drawn geometric objects), and JCPageTable (describes general tables whose cells can contain any JClass PageLayout drawable including JCPageTable).


1.2 Basic Steps for Creating a JClass PageLayout Document

At a minimum, you need to perform the following steps to format and flow a JClass PageLayout document. For more information on any step, refer to the corresponding section of this guide.

Step...

See...

1. Select or create a page template.

Building Page Templates, in Chapter 2, and line 20, A Simple JClass PageLayout Program

2. Instantiate the printer.

Creating a Printer, in Chapter 2, and line 16, A Simple JClass PageLayout Program

3. Instantiate the document.

Creating a Document, in Chapter 2, and line 20, A Simple JClass PageLayout Program

4. Instantiate the flow.

Controlling Flow, in Chapter 2, and line 23, A Simple JClass PageLayout Program

5. Send the document to the specified printer.

Printing Options, in Chapter 7, and line 29, A Simple JClass PageLayout Program


1.3 A Simple JClass PageLayout Program

If you compile and run the following program, it generates a one-page PDF document containing the text "Hello, World."

Note: For information on setting up your environment, please refer to the JClass DesktopViews Installation Guide.

1   package examples.pagelayout;
2
3   import java.io.StringReader;
4   import java.io.BufferedReader;
5
6   import com.klg.jclass.page.JCFlow;
7   import com.klg.jclass.page.JCPrinter;
8   import com.klg.jclass.page.JCDocument;
9   import com.klg.jclass.page.adobe.pdf.JCPDFPrinter;
10   
11   public class PdfPrinter {
12   
13   public static void main(String[] args)   
14   
15   // Create a PDF printer, output to stdout
16   JCPrinter printer = new JCPDFPrinter(System.out);
17
18   // Create a document using the PDF printer for formatting,
19   // setting the page template to be a simple 8.5 x 11 Letter page
20   JCDocument document = new JCDocument(printer,JCDocument.BLANK_8p5X11);
21

22   // Instantiate a flow object on the document
23   JCFlow flow = new JCFlow(document);
24
25   // Print some text to the document
26   flow.print("Hello, World.");
27
28   // Print the document to the PDF printer
29   document.print();
30   }
31 }

Line 16 of the program instantiates the PDF printer to which the output of this file is sent. On line 20, we construct the document, at the same time specifying that the printer is the one created in line 16, and that the page template to use is the standard 8.5 x 11 that comes with JClass PageLayout. We then create the flow (line 23), render the text "Hello, World" to the flow (line 26), and send the document to the PDF printer (line 29).


1.4 JClass PageLayout Objects

JClass PageLayout uses a series of nested objects. JCDocument is the top-level object. JCPage, JCFrame, and JCPageTable objects are created, defined, and added to the JCDocument object. The JCFlow object controls how text and images are rendered into the JCFrame objects. When all text has been flowed into the JCDocument object, it is passed to a JCPrinter object which handles document output.

1.4.1 Class Overview

To use JClass PageLayout, you need to understand how its classes work together to create a document and output it. The following table provides a brief overview of the classes used in JClass PageLayout.

JClass PageLayout Class

Description

JCDocument

The JCDocument holds the JCPage and JCFrame objects into which you "flow" or "render" your text and images. Attributes specify how the flow should advance through the rendering process.

Note that there can be only one JCDocument object per JCFlow; also, the JCDocument object must be the one passed to the JCFlow in the constructor.

JCPage

JCPage methods and attributes specify how an actual page should be laid out, what frames should exist on the page, and the frame order of the flow. JCPage objects are based on XML templates. Complex documents may need to define many different page templates. For example, separate templates would be necessary to describe a title page, a table of contents page, the first page in a chapter, and subsequent right and left body pages.

JCFlow

The JCFlow class provides methods and attributes that control how text and images are rendered to the document. There is only one flow in a document. To render text or images apart from the main flow, use JCFrame methods.

JCFrame

The frame is the basic unit of the flow. Frames contain all graphics (text, shapes, and images) rendered to the document. Complex documents may need to define many different JCFrame objects on a page. For example, most pages require a header frame, a body frame, and a footer frame.

JCTextStyle &
JCDrawStyle

The style classes describe and control the appearance of text and figures in the document. A style object's attributes identify how the rendered text or images should appear, for example JCTextStyle.LINEMODE_UNDERLINE is used to underline text.

JCPageTable

The JCPageTable class provides methods and attributes for creating tables. Using the JCPageTable object's subclasses (Row, Column, and Cell), you can customize the table's rows, columns, and cells. Additional methods allow you to convert other types of Java tables, including JClass LiveTables, Swing JTables, and JDBC data.

JCTab

JCTab works with JCTextStyle and provides methods for modifying tab alignment, horizontal position, and appearance (tab leader).

JCPrinter

The JCPrinter class provides attributes and methods that control how the rendered document is printed on an output device: the number of pages to output, the number of copies, whether copies should be collated, and so on. JCPrinter also generates a list of Font Family and Font objects based on your printer drivers.

A JCPrinter object needs to be created before the JCDocument object and passed to it. The JCDocument object uses the JCPrinter object's font information to properly render text.

formulas

You can add formulas to pages and to tables by employing the classes in com.klg.jclass.util.formulae. You build an expression that references page variables or table cells and performs one of a large number of mathematical operations on them, for example, summing a range of table cells. See Adding Formulas to JClass PageLayout, in Chapter 5, for details.

1.4.2 Objects on a Page

The following illustration demonstrates the output of our Hello, World program, found in Section 1.3, A Simple JClass PageLayout Program.

Figure 1 :  Example of how objects are used to build a page.

Figure 1 displays a JCPage object, which can only exist in a JCDocument object. The JCPage object contains the JCFrame object (body) that holds the text and the JCFlow object that renders the text.


1.5 Creating Flow

While the objects and template define where and how the text gets rendered, methods from the JCFrame and JCFlow classes control the flow of text from one frame to the next and from one page to the next.

When you want to control the flow of text throughout the document, use the JCFlow class. JCFlow manages layout from frame to frame and from page to page. When you want to control a frame layout or render text apart from the main flow of the document, use the JCFrame class. For more information on JCFlow and JCFrame, refer to Controlling Flow, in Chapter 2.


1.6 Printing

JClass PageLayout prints using JCDocument.print(). The print command can be as simple as:

  doc.print();

JClass PageLayout can also print a page range. For example, to print pages 17 through 39, use:

  doc.print(17, 39)

Before you can print the document, you must have set up a printer object, as described in Creating a Printer, in Chapter 2. The JCHTMLPrinter, JCPCLPrinter, JCPDFPrinter, and JCPostScriptPrinter objects print directly to a file, while the JCAWTPrinter object prints to your system printer.

When you use JCAWTPrinter, you may notice that the process of printing to your system printer is much slower than printing to a file. The Java 2 Printing API used by JCAWTPrinter causes large amounts of data to be sent to the system printer, resulting in the drastic slowdown. For more information on print output options and printing efficiency, refer to Printing Options, in Chapter 7.

Once a page is created, you can designate whether it will be held until the entire document is finished before it is printed. This is set via the outputPolicy property; please refer to OutputPolicy, in Chapter 7, for complete details.

As well, you can designate whether pages are to be discarded once printed. You can do this by setting the flushPolicy property; for information, please see FlushPolicy, in Chapter 7.

Printing Large Documents

When printing documents of any size, but especially for larger documents, better performance is achieved by wrapping the output stream in a BufferedOutputStream:

  BufferedOutputStream bos = new BufferedOutputStream(os, 2048);
  JCPrinter printer = new JC<type>Printer(bos);

This may be especially important in application server or web server environments where exceptions may be thrown if multiple writes are not buffered.

Printing Components

Components embedded into a JClass PageLayout document or encoded using the JClass PageLayout encoder types should not be double- buffered. If such components are double-buffered, then the components will draw to an image and the image will be drawn to the printer/file, thus resulting in a loss of resolution.


PreviousNextIndex