![]() ![]() ![]() |
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:
- The
JCDocument
object, which stores document-level attributes and the list of completed pages (JCPage
objects), and against which theJCFlow
object is created.- The flow mechanism, which is responsible for allocating content to pages and creating new pages as necessary.
- The
JCPrinter
object, which provides an abstraction across the set of supported output types, and whose subclasses provide the required instances of printer-specific implementations of Graphics2D for the conversion of draw actions to page-marking operands.Other important top-level components are
JCTextStyle
(stores character- and paragraph-level attributes controlling the text appearance),JCDrawStyle
(contains attributes of drawn geometric objects), andJCPageTable
(describes general tables whose cells can contain any JClass PageLayout drawable includingJCPageTable
).
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...
Building Page Templates, in Chapter 2, and line 20, A Simple JClass PageLayout Program
Creating a Printer, in Chapter 2, and line 16, A Simple JClass PageLayout Program
Creating a Document, in Chapter 2, and line 20, A Simple JClass PageLayout Program
Controlling Flow, in Chapter 2, and line 23, A Simple JClass PageLayout Program
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
, andJCPageTable
objects are created, defined, and added to theJCDocument
object. TheJCFlow
object controls how text and images are rendered into theJCFrame
objects. When all text has been flowed into theJCDocument
object, it is passed to aJCPrinter
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
The
JCDocument
holds theJCPage
andJCFrame
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 perJCFlow
; also, theJCDocument
object must be the one passed to theJCFlow
in the constructor.
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.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, useJCFrame
methods.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.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.The
JCPageTable
class provides methods and attributes for creating tables. Using theJCPageTable
object's subclasses (Row
,Column
, andCell
), 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
works withJCTextStyle
and provides methods for modifying tab alignment, horizontal position, and appearance (tab leader).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 theJCDocument
object and passed to it. TheJCDocument
object uses theJCPrinter
object's font information to properly render text.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 aJCDocument
object. TheJCPage
object contains theJCFrame
object (body
) that holds the text and theJCFlow
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
andJCFlow
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 theJCFrame
class. For more information onJCFlow
andJCFrame
, refer to Controlling Flow, in Chapter 2.
1.6 Printing
JClass PageLayout prints using
doc.print();JCDocument.print()
. The print command can be as simple as: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
, andJCPostScriptPrinter
objects print directly to a file, while theJCAWTPrinter
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 byJCAWTPrinter
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 bos = new BufferedOutputStream(os, 2048);BufferedOutputStream
:
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.
![]() ![]() ![]() |