1 /* 2 * HomePDFPrinter.java 7 sept. 07 3 * 4 * Sweet Home 3D, Copyright (c) 2007 Emmanuel PUYBARET / eTeks <info@eteks.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 package com.eteks.sweethome3d.swing; 21 22 import java.awt.Font; 23 import java.awt.Graphics; 24 import java.awt.print.PageFormat; 25 import java.awt.print.PrinterException; 26 import java.io.IOException; 27 import java.io.InterruptedIOException; 28 import java.io.OutputStream; 29 30 import com.eteks.sweethome3d.model.Home; 31 import com.eteks.sweethome3d.model.UserPreferences; 32 import com.eteks.sweethome3d.viewcontroller.ContentManager; 33 import com.eteks.sweethome3d.viewcontroller.HomeController; 34 import com.lowagie.text.Document; 35 import com.lowagie.text.DocumentException; 36 import com.lowagie.text.Rectangle; 37 import com.lowagie.text.pdf.PdfContentByte; 38 import com.lowagie.text.pdf.PdfTemplate; 39 import com.lowagie.text.pdf.PdfWriter; 40 41 /** 42 * Home PDF printer. PDF creation is implemented with iText. 43 * @author Emmanuel Puybaret 44 */ 45 public class HomePDFPrinter { 46 private final Home home; 47 private final UserPreferences preferences; 48 private final HomeController controller; 49 private final Font defaultFont; 50 51 /** 52 * Creates a PDF printer able to write to an output stream. 53 */ HomePDFPrinter(Home home, UserPreferences preferences, HomeController controller, Font defaultFont)54 public HomePDFPrinter(Home home, 55 UserPreferences preferences, 56 HomeController controller, 57 Font defaultFont) { 58 this.home = home; 59 this.preferences = preferences; 60 this.controller = controller; 61 this.defaultFont = defaultFont; 62 } 63 64 /** 65 * Writes to <code>outputStream</code> the print of a home in PDF format. 66 */ write(OutputStream outputStream)67 public void write(OutputStream outputStream) throws IOException { 68 PageFormat pageFormat = HomePrintableComponent.getPageFormat(this.home.getPrint()); 69 Document pdfDocument = new Document(new Rectangle((float)pageFormat.getWidth(), (float)pageFormat.getHeight())); 70 try { 71 // Get a PDF writer that will write to the given PDF output stream 72 PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, outputStream); 73 pdfDocument.open(); 74 75 // Set PDF document description 76 pdfDocument.addAuthor(System.getProperty("user.name", "")); 77 String pdfDocumentCreator = this.preferences.getLocalizedString( 78 HomePDFPrinter.class, "pdfDocument.creator"); 79 pdfDocument.addCreator(pdfDocumentCreator); 80 pdfDocument.addCreationDate(); 81 String homeName = this.home.getName(); 82 if (homeName != null) { 83 pdfDocument.addTitle(this.controller.getContentManager().getPresentationName( 84 homeName, ContentManager.ContentType.PDF)); 85 } 86 87 PdfContentByte pdfContent = pdfWriter.getDirectContent(); 88 HomePrintableComponent printableComponent = 89 new HomePrintableComponent(this.home, this.controller, this.defaultFont); 90 // Print each page 91 for (int page = 0, pageCount = printableComponent.getPageCount(); page < pageCount; page++) { 92 // Check current thread isn't interrupted 93 if (Thread.interrupted()) { 94 throw new InterruptedIOException(); 95 } 96 PdfTemplate pdfTemplate = pdfContent.createTemplate((float)pageFormat.getWidth(), 97 (float)pageFormat.getHeight()); 98 Graphics g = pdfTemplate.createGraphicsShapes((float)pageFormat.getWidth(), 99 (float)pageFormat.getHeight()); 100 101 printableComponent.print(g, pageFormat, page); 102 103 pdfContent.addTemplate(pdfTemplate, 0, 0); 104 g.dispose(); 105 106 if (page != pageCount - 1) { 107 pdfDocument.newPage(); 108 } 109 } 110 pdfDocument.close(); 111 } catch (DocumentException ex) { 112 IOException exception = new IOException("Couldn't print to PDF"); 113 exception.initCause(ex); 114 throw exception; 115 } catch (InterruptedPrinterException ex) { 116 throw new InterruptedIOException("Print to PDF interrupted"); 117 } catch (PrinterException ex) { 118 IOException exception = new IOException("Couldn't print to PDF"); 119 exception.initCause(ex); 120 throw exception; 121 } 122 } 123 } 124