JTextPrinter 1.0
Sample Code
WILDCREST ASSOCIATES

[Main Page] [Sample Code] [User Manual] [Javadoc Documentation]
[Installation & Compatibility] [License & Contact Information]

JTextPrinter comes with two sample programs including source code in both Swing 1.0 (com.sun.java.swing) and Swing 1.1  (javax.swing)  versions.  The source code for these applications may be freely used, modified, incorporated, and distributed without restriction as part of any software that uses JTextPrinter itself (see License information).
 

JTextPrinterSimpleTest

JTextPrinterSimpleTest is a small Java application that will display a JTextPane containing a small rich text document, which the user can optionally edit, and a single button which will cause the JTextPane to print.  It is intended to show the modest program that forms a rich text document (including an embedded graphic) and allows minimal user interaction for editing and printing.  The source code for the Swing 1.0 version of JTextPrinterSimpleTest.java and the Swing 1.1 version of JTextPrinterSimpleTest.java are provided with this distribution.  See below for instructions on how to install and run JTextPrinterSimpleTest.
 

JTextPrinterTestApplication

JTextPrinterTestApplication is a more substantial Java application that demonstrates the use of JTextPane to compose and edit  rich text documents, read and write them from files, and print them with JTextPrinter, including a Page Setup Dialog giving full control of all the properties that control the features of JTextPrinter.

JTextPrinterTestApplication basically implements a simple word processor for entering a rich text document including font selection, size, style, color, text alignment, and insertion of graphics and page breaks.  The resulting document is an instance of the Java class StyledDocument which the Print command (under the File menu) will then print.  The Save or Save As commands will save the document in a file as a "serialized StyledDocument" using the extension .SSD.  The Open command will reopen files with the extension .SSD.  In addition, the Open command will open files with extension .RTF (or .rtf) as RTF files and files with the extension .HTM or .HTML (or .htm or .html) as HTML files.  Files with any other extensions will be opened as plain text documents which can then be stylized as desired using the text formatting commands.

JTextPrinterTestApplication's Page Setup command brings up a dialog box that lets you control all the page formatting parameters supported by JTextPrinter.  JTextPrinterTestApplication also comes with a set of sample files which can be used to test JTextPrinter functionality.

One of the Page Setup options lets you print documents with multiple columns per page.  In conjunction with this option, you can use the Print With Title Section command under the File menu.  This will prompt for an additional document file (which you will not see on the screen).  JTextPrinterTestApplication will then print this additional document file as single column title section (which can itself span multiple pages) followed immediately by a multiple column version of the document in the document window.  This feature is useful for titles and abstracts, tables of contents, cover pages, etc.

Source code for JTextPrinterTestApplication is provided with this distribution.  It consists of three files.   The Swing 1.0 versions of these are: JTextPrinterTestApplication.java, JPageSetupDialog.java, and JAboutDialog.java.  The Swing 1.1 versions of these are: JTextPrinterTestApplication.java, JPageSetupDialog.java, and JAboutDialog.java.
 

Installation

Because JTextPrinter and the two sample applications are Swing-based programs, you must have Swing (JFC) installed on your system (i.e., swingall.jar must appear your classpath).  Information is available at the Sun Java site for how to download (free) and install Swing, along with information on how you can test if your system is already set up for Swing.

Two versions of JTextPrinter (and the sample applications) are provided with this release.  One version is built assuming the older Swing package names com.sun.java.swing.* and is intended for use with Swing 1.0.  The other is built assuming the new Swing package names javax.swing.*  and is intended for use with Swing 1.1.  JTextPrinter 1.0 requires that you use the correct release depending on which release of Swing you have installed.  Swing works with most JDK 1.1 releases, requiring JDK 1.1.2 or later, with JDK 1.1.5 or later preferred.  JTextPrinter was developed with  Swing 1.0.3, Swing 1.1, Swing 1.1.1 beta 1, and Swing 1.1.1 beta 2 using the JDK 1.1.7A release.

JTextPrinter uses the JDK 1.1 printing model and is intended for use with with JDK 1.1.  Even though the JDK 1.1 printing model is supported for compatibility under JDK 1.2, the newer printing model introduced with JDK 1.2 provides a new architecture for printing that is intended as the primary printing solution under JDK 1.2.  Moreover, there are a variety bugs in the JDK 1.1 printing model support for rich text, and JTextPane in particular, under JDK 1.2.  As a result, JTextPrinter does not work under the current release of JDK 1.2.
 

Limits to RTF and HTML support (JTextPrinterTestApplication)

JTextPrinter is built upon the Swing JTextPane print and paint methods, and unfortunately JTextPane has significant problems with the RTF and HTML formats in the different versions of Swing.  JTextPane only supports a limited subset of the RTF and HTML formats.  Depending on what version of Swing you are using, you will discover that many features (such as images, tables, frames, special characters, font selection, ruler settings, indentation, scaling, etc.) of your RTF or HTML document may display incorrectly or be omitted, and certain documents may not read in at all.  The JTextPane printing methods have further problems.  There are features of RTF and HTML documents that JTextPane can display but can't print or prints incorrectly, and there are documents that will only print part way or won't print at all.

In practice, only basic RTF and HTML documents will display and print correctly.  However, the native Java StyledDocument format (DefaultStyledDocument) is well supported for both display and printing by JTextPane and thus JTextPrinter.  See the Installation and Compatibility section for more details on problems that may arise using JTextPrinter on different versions of Swing.
 

Document serialization and Swing versions

Sun does not provide serialization of documents that is compatible across Swing releases prior to the JDK 1.2 release.  This means that the documents JTextPrinterTestApplication saves in .SSD format under one version of Swing will only read back into the same release of Swing.  The SSD test documents provided with this distribution come in Swing 1.0, Swing 1.1, and Swing 1.1.1 versions, and you must use the appropriate version depending upon the Swing release you have installed (Swing 1.1.1 beta 1 and beta 2 share the same serialization format).
 

Using Stylepad with JTextPrinter

The Stylepad sample application that comes with Swing is similar to JTextPrinterTestApplication in that it is built around JTextPane and uses the same native Java StyledDocument format as is supported by JTextPrinter.  Therefore, you can use Stylepad to create more rich text documents, save these documents in files (be sure to use the .SSD extension), and read and print them using JTextPrinterTestApplication.  Because of the serialization issues discussed above, you need to use the Stylepad and JTextPrinterTestApplication from the same release of Swing (1.0, 1.1, or 1.1.1), as the serialized StyledDocument format does not migrated across releases of Swing.
 

How to run JTextPrinterTestApplication

1) Make sure your system is set up for either Swing 1.0 or Swing 1.1 on a JDK 1.1.x environment (see first section above).

2) Download the file JTextPrinter100free.zip (or JTextPrinter100product.zip if you have purchased the Product Version of JTextPrinter) and unzip. This will create a JTextPrinter folder containing both code and documentation.

3) Open a command window (shell window in Unix or MS-DOS window in Windows) and change directory ("cd") to the JTextPrinter installation folder.

4) In the JTextPrinter installation folder you will find two subfolders:
    VersionForSwing10
and
    VersionForSwing11
Use "cd" again to go into the appropriate subfolder for your Swing installation.

5) Type:
    java -classpath JTextPrinterTestApplication JTextPrinterTestApplication
or on some systems you may need to type:
    java -classpath JTextPrinterTestApplication;%classpath% JTextPrinterTestApplication
NOTE: On Unix systems, the class path file separator is generally colon instead of semi-colon, and the class path is designated by  $CLASSPATH instead of %classpath%

6) Or on Windows systems, instead of steps 3-5, you can simply navigate to the appropriate VersionForSwing folder and double-click on the file:
    runJTextPrinterTestApplication.bat
 

How to run JTextPrinterSimpleTestApplication

Follow the instructions for JTextPrinterTestApplication above, except for steps 5 and 6 substitute:

5) Type:
    java -classpath JTextPrinterSimpleTest JTextPrinterSimpleTest
or on some systems you may need to type:
    java -classpath JTextPrinterSimpleTest;%classpath% JTextPrinterSimpleTest
NOTE: On Unix systems, the class path file separator is generally colon instead of semi-colon, and the class path is designated by  $CLASSPATH instead of %classpath%

6) Or on Windows systems, instead of steps 3-5, you can simply navigate to the appropriate VersionForSwing folder and double-click on the file:
    runJTextPrinterSimpleTest.bat



© Copyright 1999, Wildcrest Associates (http://www.wildcrest.com)