Search in sources :

Example 31 with Document

use of com.itextpdf.text.Document in project MtgDesktopCompanion by nicho92.

the class PDFExport method export.

@Override
public void export(MagicDeck deck, File f) throws IOException {
    PdfPTable table = new PdfPTable(3);
    table.setHorizontalAlignment(Element.ALIGN_CENTER);
    try {
        document = new Document(PageSize.A4, 5, 5, 10, 5);
        document.addAuthor(getString("AUTHOR"));
        document.addCreationDate();
        document.addCreator("Magic Desktop Companion");
        document.addTitle(deck.getName());
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f));
        document.open();
        int i = 0;
        for (MagicCard card : deck.getAsList()) {
            table.addCell(getCells(card));
            setChanged();
            notifyObservers(i++);
        }
        document.add(table);
        document.close();
        writer.close();
    } catch (Exception e) {
        logger.error("Error in pdf creation " + f, e);
    }
}
Also used : PdfPTable(com.itextpdf.text.pdf.PdfPTable) PdfWriter(com.itextpdf.text.pdf.PdfWriter) MagicCard(org.magic.api.beans.MagicCard) FileOutputStream(java.io.FileOutputStream) Document(com.itextpdf.text.Document) NotImplementedException(org.apache.commons.lang3.NotImplementedException) BadElementException(com.itextpdf.text.BadElementException) IOException(java.io.IOException)

Example 32 with Document

use of com.itextpdf.text.Document in project mzmine2 by mzmine.

the class SwingExportUtil method writeToPDF.

/**
 * Writes swing to pdf
 *
 * @param panel
 * @param fileName
 * @throws DocumentException
 * @throws Exception
 */
public static void writeToPDF(JComponent panel, File fileName) throws IOException, DocumentException {
    // print the panel to pdf
    int width = panel.getWidth();
    int height = panel.getHeight();
    logger.info(() -> MessageFormat.format("Exporting panel to PDF file (width x height; {0} x {1}): {2}", width, height, fileName.getAbsolutePath()));
    Document document = new Document(new Rectangle(width, height));
    PdfWriter writer = null;
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D g2 = new PdfGraphics2D(contentByte, width, height, new DefaultFontMapper());
        panel.print(g2);
        g2.dispose();
        contentByte.addTemplate(template, 0, 0);
        document.close();
        writer.close();
    } finally {
        if (document.isOpen()) {
            document.close();
        }
    }
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) FileOutputStream(java.io.FileOutputStream) Rectangle(com.itextpdf.text.Rectangle) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) DefaultFontMapper(com.itextpdf.awt.DefaultFontMapper) Document(com.itextpdf.text.Document) PdfTemplate(com.itextpdf.text.pdf.PdfTemplate) EMFGraphics2D(org.freehep.graphicsio.emf.EMFGraphics2D) Graphics2D(java.awt.Graphics2D) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D)

Aggregations

Document (com.itextpdf.text.Document)32 FileOutputStream (java.io.FileOutputStream)25 PdfWriter (com.itextpdf.text.pdf.PdfWriter)22 IOException (java.io.IOException)12 DocumentException (com.itextpdf.text.DocumentException)10 Rectangle (com.itextpdf.text.Rectangle)10 PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)10 File (java.io.File)10 Font (com.itextpdf.text.Font)8 Paragraph (com.itextpdf.text.Paragraph)8 PdfTemplate (com.itextpdf.text.pdf.PdfTemplate)6 FileNotFoundException (java.io.FileNotFoundException)6 Graphics2D (java.awt.Graphics2D)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 PdfGraphics2D (com.itextpdf.awt.PdfGraphics2D)4 Chapter (com.itextpdf.text.Chapter)4 PdfPTable (com.itextpdf.text.pdf.PdfPTable)4 OutputStream (java.io.OutputStream)4 SimpleDateFormat (java.text.SimpleDateFormat)4 Date (java.util.Date)4