5 package net.sf.openrocket.gui.print;
7 import com.itextpdf.text.Document;
8 import com.itextpdf.text.DocumentException;
9 import com.itextpdf.text.ExceptionConverter;
10 import com.itextpdf.text.Rectangle;
11 import com.itextpdf.text.pdf.PdfBoolean;
12 import com.itextpdf.text.pdf.PdfName;
13 import com.itextpdf.text.pdf.PdfWriter;
14 import net.sf.openrocket.document.OpenRocketDocument;
15 import net.sf.openrocket.gui.print.visitor.FinSetVisitorStrategy;
16 import net.sf.openrocket.gui.print.visitor.PartsDetailVisitorStrategy;
18 import javax.print.attribute.standard.MediaSizeName;
19 import java.io.IOException;
20 import java.io.OutputStream;
21 import java.util.Iterator;
25 * This is the main active object for printing. It performs all actions necessary to create and populate the print
28 public class PrintController {
31 * Print the selected components to a PDF document.
33 * @param doc the OR document
34 * @param toBePrinted the user chosen items to print
35 * @param outputFile the file being written to
36 * @param msn the paper size
38 public void print(OpenRocketDocument doc, Iterator<PrintableContext> toBePrinted, OutputStream outputFile,
41 Document idoc = new Document(convertWithDefault(msn));
42 PdfWriter writer = null;
44 writer = PdfWriter.getInstance(idoc, outputFile);
45 writer.setStrictImageSequence(true);
47 writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
48 writer.addViewerPreference(PdfName.PICKTRAYBYPDFSIZE, PdfBoolean.PDFTRUE);
53 catch (InterruptedException e) {
55 while (toBePrinted.hasNext()) {
56 PrintableContext printableContext = toBePrinted.next();
58 Set<Integer> stages = printableContext.getStageNumber();
60 switch (printableContext.getPrintable()) {
62 DesignReport dp = new DesignReport(doc, idoc);
63 dp.writeToDocument(writer);
67 final FinSetVisitorStrategy finWriter = new FinSetVisitorStrategy(idoc,
70 finWriter.writeToDocument(doc.getRocket());
73 final PartsDetailVisitorStrategy detailVisitor = new PartsDetailVisitorStrategy(idoc,
76 detailVisitor.writeToDocument(doc.getRocket());
77 detailVisitor.close();
82 //Stupid iText throws a really nasty exception if there is no data when close is called.
83 if (writer.getCurrentDocumentSize() <= 140) {
84 writer.setPageEmpty(false);
89 catch (DocumentException e) {
91 catch (ExceptionConverter ec) {
94 if (outputFile != null) {
98 catch (IOException e) {
105 * Convert a media size name to a rectangle that defines the bounds of the corresponding paper size.
107 * @param msn the MediaSizeName to convert
108 * @return the corresponding Rectangle
110 private Rectangle convertWithDefault(final MediaSizeName msn) {
111 Rectangle result = PaperSize.convert(msn);
112 if (result == null) {
113 result = PaperSize.convert(PrintUtilities.getDefaultMedia().getMediaSizeName());