4 package net.sf.openrocket.gui.print;
6 import com.itextpdf.text.Chunk;
7 import com.itextpdf.text.Document;
8 import com.itextpdf.text.DocumentException;
9 import com.itextpdf.text.Font;
10 import com.itextpdf.text.Paragraph;
11 import com.itextpdf.text.Phrase;
12 import com.itextpdf.text.Rectangle;
13 import com.itextpdf.text.pdf.PdfContentByte;
14 import com.itextpdf.text.pdf.PdfPCell;
15 import com.itextpdf.text.pdf.PdfPTable;
16 import com.itextpdf.text.pdf.PdfWriter;
19 import java.awt.image.BufferedImage;
22 * A bunch of helper methods for creating iText components.
24 public final class ITextHelper {
27 * Create a cell for an iText table.
29 * @return a cell with bottom border
31 public static PdfPCell createCell () {
32 return createCell(Rectangle.BOTTOM);
36 * Create a cell for an iText table with the given border location.
38 * @param border the border location
40 * @return a cell with given border
42 public static PdfPCell createCell (int border) {
43 PdfPCell result = new PdfPCell();
44 result.setBorder(border);
50 * Create a cell whose contents are a table. No border.
52 * @param table the table to insert into the cell
54 * @return the cell containing a table
56 public static PdfPCell createCell (PdfPTable table) {
57 PdfPCell result = new PdfPCell();
58 result.setBorder(PdfPCell.NO_BORDER);
59 result.addElement(table);
65 * Create a cell whose contents are the given string. No border. Standard PrintUtilities.NORMAL font.
67 * @param v the text of the cell.
69 * @return the cell containing the text
71 public static PdfPCell createCell (String v) {
72 return createCell(v, Rectangle.NO_BORDER, PrintUtilities.NORMAL);
76 * Create a cell whose contents are the given string , rendered with the given font. No border.
78 * @param v the text of the cell
79 * @param font the font
81 * @return the cell containing the text
83 public static PdfPCell createCell (String v, Font font) {
84 return createCell(v, Rectangle.NO_BORDER, font);
88 * Create a cell whose contents are the given string with specified left and right padding (spacing).
90 * @param v the text of the cell
91 * @param leftPad the number of points to precede the text
92 * @param rightPad the number of points to follow the text
94 * @return the cell containing the text
96 public static PdfPCell createCell (String v, int leftPad, int rightPad) {
97 PdfPCell c = createCell(v, Rectangle.NO_BORDER, PrintUtilities.NORMAL);
98 c.setPaddingLeft(leftPad);
99 c.setPaddingRight(rightPad);
104 * Create a cell whose contents are the given string with the given border. Uses NORMAL font.
106 * @param v the text of the cell
107 * @param border the border type
109 * @return the cell containing the text
111 public static PdfPCell createCell (String v, int border) {
112 return createCell(v, border, PrintUtilities.NORMAL);
116 * Complete create cell - fully qualified. Create a cell whose contents are the given string with the given border
119 * @param v the text of the cell
120 * @param border the border type
121 * @param font the font
123 * @return the cell containing the text
125 public static PdfPCell createCell (String v, int border, Font font) {
126 PdfPCell result = new PdfPCell();
127 result.setBorder(border);
128 Chunk c = new Chunk();
131 result.addElement(c);
136 * Create a phrase with the given text and font.
138 * @param text the text
139 * @param font the font
141 * @return an iText phrase
143 public static Phrase createPhrase (String text, Font font) {
144 Phrase p = new Phrase();
145 final Chunk chunk = new Chunk(text);
152 * Create a phrase with the given text.
154 * @param text the text
156 * @return an iText phrase
158 public static Phrase createPhrase (String text) {
159 return createPhrase(text, PrintUtilities.NORMAL);
163 * Create a paragraph with the given text and font.
165 * @param text the text
166 * @param font the font
168 * @return an iText paragraph
170 public static Paragraph createParagraph (String text, Font font) {
171 Paragraph p = new Paragraph();
172 final Chunk chunk = new Chunk(text);
179 * Create a paragraph with the given text and using NORMAL font.
181 * @param text the text
183 * @return an iText paragraph
185 public static Paragraph createParagraph (String text) {
186 return createParagraph(text, PrintUtilities.NORMAL);
190 * Break a large image up into page-size pieces and output each page in order to an iText document. The image is
191 * overlayed with an matrix of pages running from left to right until the right side of the image is reached. Then
192 * the next 'row' of pages is output from left to right, and so on.
194 * @param pageSize a rectangle that defines the bounds of the page size
195 * @param doc the iText document
196 * @param writer the underlying content writer
197 * @param image the source image
199 * @throws DocumentException thrown if the document could not be written
201 public static void renderImageAcrossPages (Rectangle pageSize, Document doc, PdfWriter writer, java.awt.Image image)
202 throws DocumentException {
203 final int margin = (int)Math.min(doc.topMargin(), PrintUnit.POINTS_PER_INCH * 0.3f);
204 float wPage = pageSize.getWidth() - 2 * margin;
205 float hPage = pageSize.getHeight() - 2 * margin;
207 float wImage = image.getWidth(null);
208 float hImage = image.getHeight(null);
209 java.awt.Rectangle crop = new java.awt.Rectangle(0, 0, (int) Math.min(wPage, wImage), (int) Math.min(hPage,
211 PdfContentByte content = writer.getDirectContent();
216 BufferedImage subImage = ((BufferedImage) image).getSubimage((int) crop.getX(), (int) crop.getY(),
217 (int) crop.getWidth(), (int) crop.getHeight());
219 Graphics2D g2 = content.createGraphics(pageSize.getWidth(), pageSize.getHeight());
220 g2.drawImage(subImage, margin, ymargin, null);
223 // After the first page, the y-margin needs to be set.
226 final int newX = (int) (crop.getWidth() + crop.getX());
228 double adjust = Math.min(wImage - newX, wPage);
229 crop = new java.awt.Rectangle(newX, (int) crop.getY(), (int) adjust,
230 (int) crop.getHeight());
233 final int newY = (int) (crop.getHeight() + crop.getY());
235 double adjust = Math.min(hImage - newY, hPage);
236 crop = new java.awt.Rectangle(0, newY, (int) Math.min(wPage, wImage), (int) adjust);