|
General properties
Whether J2TextPrinter is used as a Pageable or a Flowable, you can control whether it is left, right, or center justified horizontally on a page, top, bottom, or center justified within the remaing space on a page, whether the JTextPane reflows (rewraps) into the available page width or maintains its current layout ("WYSIWYG"), and how copying (cloning) of your JTextPane is used when printing.
Horizontal and vertical alignment
J2TextPrinter will print your JTextPane either left, center, or right justified horizontally on the page between the left and right margins. The default value for horizontal alignment is CENTER.
Similarly, J2TextPrinter will print your JTextPane either top, center, or bottom justified vertically on the page between the gap below the header and gap above the footer. The default value for vertical alignment is TOP.
setHorizontalAlignment (int horizontalAlignment)
Sets the horizontal alignment (LEFT, CENTER, RIGHT) for printing the JTextPane on the page.
setVerticalAlignment (int verticalAlignment)
Sets the vertical alignment (TOP, CENTER, BOTTOM) for printing the JTextPane on the page.
WYSIWYG support
J2TextPrinter normally reflows (line wraps) your document to fill the printed page. However, J2TextPrinter can be controlled to print your JTextPane with exactly the same flow as it appears on the screen. For this purpose J2TextPrinter has a setWYSIWYG method:
setWYSIWYG (boolean wysiwyg)
Sets whether WYSIWYG (What-You-See-Is-What-You-Get) mode is enabled, thereby preventing relayout of text.
If you call setWYSIWYG(true), then you will get a WYSIWYG representation of your JTextPane with its line layout preserved as is. If the width of your JTextPane is wider than the available printing width of the page (between margins), then J2TextPrinter will perform a shrink-to-fit operation on your JTextPane to make it fit. J2TextPrinter never prints more than one page horizontally.
Cloning support
J2TextPrinter needs to be able to reflow (rewrap) the contents of your JTextPane without affecting its appearance on the screen. To accomplish this, J2TextPrinter prints your JTextPane's contents from its own internal JTextPane rather than from your JTextPane.
If cloningUsed is false, the StyledDocument of J2TextPrinter's internal JTextPane is set to your JTextPane's StyledDocument directly. If cloningUsed is true, the StyledDocument of J2TextPrinter's internal JTextPane is set to a clone (a copy created by Java serialization) of the StyledDocument of your JTextPane.
It has been determined that cloning is necessary if your JTextPane has inserted Components and appears in a Frame, JFrame, or JInternalFrame, or if it has inserted Components and you are printing from a separate thread (setSeparatePrintThread(true), which is the default). This is because Java doesn't support having the same component in multiple simultaneous views.
Since cloning is generally a safe, the default for cloningUsed is true. This method allows cloning to be disabled if it causes any problems for a particular JTextPane and since it is a bit slower. This can occur in particular if the JTextPane contains embedded components that are for whatever reason not serializable. There are also certain known serialization bugs in Java. For example, under JDK 1.4.x certain HTML tags such as the "border" subtag of "table" are lost during serialization, so it turns out cloningUsed needs to be set to false for HTML documents that contain tables with borders, otherwise the borders will be missing. This problem is fixed under JDK 1.5.
setCloningUsed (boolean cloningUsed)
Specify whether cloning is to be used when printing with J2TextPrinter.
clone (javax.swing.JTextPane pane)
Convenience factory method for making a new JTextPane whose StyledDocument is a serialized copy (deep clone) of the specified JTextPane's StyledDocument.
If your JTextPane does not have embedded images or embedded components, then you can clone your JTextPane without resorting to serialization using the following code:
JTextPane text2 = new JTextPane();
text2.setContentType(text1.getContentType());
text2.setText(text1.getText());
text2.setSize(text2.getPreferredSize()); // size required
The same technique can always be used for JEditorPane, which does not support embedded images or embedded components.
« »
|