4 package net.sf.openrocket.gui.print;
6 import java.awt.Graphics2D;
7 import java.awt.image.BufferedImage;
9 import com.itextpdf.text.Chunk;
10 import com.itextpdf.text.Document;
11 import com.itextpdf.text.DocumentException;
12 import com.itextpdf.text.Font;
13 import com.itextpdf.text.Paragraph;
14 import com.itextpdf.text.Phrase;
15 import com.itextpdf.text.Rectangle;
16 import com.itextpdf.text.pdf.BaseFont;
17 import com.itextpdf.text.pdf.PdfContentByte;
18 import com.itextpdf.text.pdf.PdfPCell;
19 import com.itextpdf.text.pdf.PdfPTable;
20 import com.itextpdf.text.pdf.PdfWriter;
23 * A bunch of helper methods for creating iText components.
25 public final class ITextHelper {
27 public static BaseFont getBaseFont(){
29 return BaseFont.createFont("/dejavu-font/DejaVuSerif.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
30 } catch (Exception ex ) {
31 throw new RuntimeException(ex);
35 * Create a cell for an iText table.
37 * @return a cell with bottom border
39 public static PdfPCell createCell () {
40 return createCell(Rectangle.BOTTOM);
44 * Create a cell for an iText table with the given border location.
46 * @param border the border location
48 * @return a cell with given border
50 public static PdfPCell createCell (int border) {
51 PdfPCell result = new PdfPCell();
52 result.setBorder(border);
58 * Create a cell whose contents are a table. No border.
60 * @param table the table to insert into the cell
62 * @return the cell containing a table
64 public static PdfPCell createCell (PdfPTable table) {
65 PdfPCell result = new PdfPCell();
66 result.setBorder(PdfPCell.NO_BORDER);
67 result.addElement(table);
73 * Create a cell whose contents are the given string. No border. Standard PrintUtilities.NORMAL font.
75 * @param v the text of the cell.
77 * @return the cell containing the text
79 public static PdfPCell createCell (String v) {
80 return createCell(v, Rectangle.NO_BORDER, PrintUtilities.NORMAL);
84 * Create a cell whose contents are the given string , rendered with the given font. No border.
86 * @param v the text of the cell
87 * @param font the font
89 * @return the cell containing the text
91 public static PdfPCell createCell (String v, Font font) {
92 return createCell(v, Rectangle.NO_BORDER, font);
96 * Create a cell whose contents are the given string with specified left and right padding (spacing).
98 * @param v the text of the cell
99 * @param leftPad the number of points to precede the text
100 * @param rightPad the number of points to follow the text
102 * @return the cell containing the text
104 public static PdfPCell createCell (String v, int leftPad, int rightPad) {
105 PdfPCell c = createCell(v, Rectangle.NO_BORDER, PrintUtilities.NORMAL);
106 c.setPaddingLeft(leftPad);
107 c.setPaddingRight(rightPad);
112 * Create a cell whose contents are the given string with the given border. Uses NORMAL font.
114 * @param v the text of the cell
115 * @param border the border type
117 * @return the cell containing the text
119 public static PdfPCell createCell (String v, int border) {
120 return createCell(v, border, PrintUtilities.NORMAL);
124 * Complete create cell - fully qualified. Create a cell whose contents are the given string with the given border
127 * @param v the text of the cell
128 * @param border the border type
129 * @param font the font
131 * @return the cell containing the text
133 public static PdfPCell createCell (String v, int border, Font font) {
134 PdfPCell result = new PdfPCell();
135 result.setBorder(border);
136 Chunk c = new Chunk();
139 result.addElement(c);
144 * Create a phrase with the given text and font.
146 * @param text the text
147 * @param font the font
149 * @return an iText phrase
151 public static Phrase createPhrase (String text, Font font) {
152 Phrase p = new Phrase();
153 final Chunk chunk = new Chunk(text);
160 * Create a phrase with the given text.
162 * @param text the text
164 * @return an iText phrase
166 public static Phrase createPhrase (String text) {
167 return createPhrase(text, PrintUtilities.NORMAL);
171 * Create a paragraph with the given text and font.
173 * @param text the text
174 * @param font the font
176 * @return an iText paragraph
178 public static Paragraph createParagraph (String text, Font font) {
179 Paragraph p = new Paragraph();
180 final Chunk chunk = new Chunk(text);
187 * Create a paragraph with the given text and using NORMAL font.
189 * @param text the text
191 * @return an iText paragraph
193 public static Paragraph createParagraph (String text) {
194 return createParagraph(text, PrintUtilities.NORMAL);
198 * Break a large image up into page-size pieces and output each page in order to an iText document. The image is
199 * overlayed with an matrix of pages running from left to right until the right side of the image is reached. Then
200 * the next 'row' of pages is output from left to right, and so on.
202 * @param pageSize a rectangle that defines the bounds of the page size
203 * @param doc the iText document
204 * @param writer the underlying content writer
205 * @param image the source image
207 * @throws DocumentException thrown if the document could not be written
209 public static void renderImageAcrossPages (Rectangle pageSize, Document doc, PdfWriter writer, java.awt.Image image)
210 throws DocumentException {
211 final int margin = (int)Math.min(doc.topMargin(), PrintUnit.POINTS_PER_INCH * 0.3f);
212 float wPage = pageSize.getWidth() - 2 * margin;
213 float hPage = pageSize.getHeight() - 2 * margin;
215 float wImage = image.getWidth(null);
216 float hImage = image.getHeight(null);
217 java.awt.Rectangle crop = new java.awt.Rectangle(0, 0, (int) Math.min(wPage, wImage), (int) Math.min(hPage,
219 PdfContentByte content = writer.getDirectContent();
224 BufferedImage subImage = ((BufferedImage) image).getSubimage((int) crop.getX(), (int) crop.getY(),
225 (int) crop.getWidth(), (int) crop.getHeight());
227 Graphics2D g2 = content.createGraphics(pageSize.getWidth(), pageSize.getHeight());
228 g2.drawImage(subImage, margin, ymargin, null);
231 // After the first page, the y-margin needs to be set.
234 final int newX = (int) (crop.getWidth() + crop.getX());
236 double adjust = Math.min(wImage - newX, wPage);
237 crop = new java.awt.Rectangle(newX, (int) crop.getY(), (int) adjust,
238 (int) crop.getHeight());
241 final int newY = (int) (crop.getHeight() + crop.getY());
243 double adjust = Math.min(hImage - newY, hPage);
244 crop = new java.awt.Rectangle(0, newY, (int) Math.min(wPage, wImage), (int) adjust);