Search in sources :

Example 6 with Document

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

the class DCIDeckSheetExport method export.

@Override
public void export(MagicDeck deck, File dest) throws IOException {
    PdfReader reader = new PdfReader(new URL(getString("PDF_URL")));
    Document document = new Document(reader.getPageSize(1));
    PdfWriter writer;
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    } catch (DocumentException e) {
        throw new IOException(e.getMessage());
    }
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    // copy first page to new pdf file
    PdfImportedPage page = writer.getImportedPage(reader, 1);
    document.newPage();
    cb.addTemplate(page, 0, 0);
    Font helvetica = new Font(FontFamily.HELVETICA, 12);
    BaseFont bfHelv = helvetica.getCalculatedBaseFont(false);
    cb.beginText();
    cb.setFontAndSize(bfHelv, 11);
    // HEADER
    cb.setTextMatrix(page.getWidth() - 51f, page.getHeight() - 49);
    cb.showText(getString("LAST_NAME").substring(0, 1).toUpperCase());
    cb.setTextMatrix(page.getWidth() / 3.2f, page.getHeight() - 73);
    if (!getString("FORCED_DATE").equalsIgnoreCase(""))
        cb.showText(getString("FORCED_DATE"));
    else
        cb.showText(new SimpleDateFormat(getString("DATE_FORMAT")).format(new Date()));
    cb.setTextMatrix(page.getWidth() / 1.48f, page.getHeight() - 73);
    cb.showText(getString("EVENT_NAME"));
    cb.setTextMatrix(page.getWidth() / 3.2f, page.getHeight() - 96);
    cb.showText(getString("LOCATION"));
    cb.setTextMatrix(page.getWidth() / 1.48f, page.getHeight() - 96);
    cb.showText(deck.getName());
    cb.setTextMatrix(page.getWidth() / 1.48f, page.getHeight() - 119);
    if (getString("DECK_DESIGNER").equals(""))
        cb.showText(getString("LAST_NAME") + " " + getString("FIRST_NAME"));
    else
        cb.showText(getString("DECK_DESIGNER"));
    // MAIN DECK
    int count = 0;
    for (MagicCard mc : deck.getMap().keySet()) {
        cb.setTextMatrix(page.getWidth() / 6.4f, page.getHeight() - 185 - count);
        cb.showText(deck.getMap().get(mc) + space + mc.getName());
        count += 18;
    }
    // CONTINUED and BASIC LAND
    if (getString("FILL_CONTINUED_LANDS").equalsIgnoreCase("true")) {
        count = 0;
        for (MagicCard mc : deck.getMap().keySet()) {
            if (mc.getTypes().contains("Land")) {
                cb.setTextMatrix(page.getWidth() / 1.7f, page.getHeight() - 185 - count);
                cb.showText(deck.getMap().get(mc) + space + mc.getName());
                count += 18;
            }
        }
    }
    // SIDEBOARD
    count = 0;
    for (MagicCard mc : deck.getMapSideBoard().keySet()) {
        cb.setTextMatrix(page.getWidth() / 1.7f, page.getHeight() - 418 - count);
        cb.showText(deck.getMapSideBoard().get(mc) + space + mc.getName());
        count += 18;
    }
    // BOTTOM card count
    cb.setTextMatrix((page.getWidth() / 2f) - 30, 45);
    cb.showText(String.valueOf(deck.getAsList().size()));
    cb.setTextMatrix(page.getWidth() - 70, 100);
    cb.showText(String.valueOf(deck.getSideAsList().size()));
    // LEFT TEXT
    cb.showTextAligned(PdfContentByte.ALIGN_LEFT, getString("LAST_NAME"), 52, 90, 90);
    cb.showTextAligned(PdfContentByte.ALIGN_LEFT, getString("FIRST_NAME"), 52, 295, 90);
    String dci = getString("DCI_NUMBER");
    count = 0;
    for (int i = 0; i < dci.length(); i++) {
        char c = dci.charAt(i);
        cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(c), 52, (428 + count), 90);
        count += 22;
    }
    cb.endText();
    document.close();
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) PdfReader(com.itextpdf.text.pdf.PdfReader) IOException(java.io.IOException) Document(com.itextpdf.text.Document) URL(java.net.URL) Font(com.itextpdf.text.Font) BaseFont(com.itextpdf.text.pdf.BaseFont) Date(java.util.Date) PdfImportedPage(com.itextpdf.text.pdf.PdfImportedPage) MagicCard(org.magic.api.beans.MagicCard) FileOutputStream(java.io.FileOutputStream) DocumentException(com.itextpdf.text.DocumentException) BaseFont(com.itextpdf.text.pdf.BaseFont) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with Document

use of com.itextpdf.text.Document in project ASCIIGenome by dariober.

the class Pdf method appendPdf.

/**
 * Append pdf file `in` to pdf file `dest`
 * @throws IOException
 * @throws DocumentException
 */
private void appendPdf(File in, File dest) throws IOException, DocumentException {
    if (!dest.exists()) {
        Files.move(Paths.get(in.getAbsolutePath()), Paths.get(dest.getAbsolutePath()));
        return;
    }
    File template = Utils.createTempFile(".template.", ".pdf");
    ;
    template.deleteOnExit();
    Files.copy(Paths.get(dest.getAbsolutePath()), Paths.get(template.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
    Document document = new Document();
    FileOutputStream outputStream = new FileOutputStream(template);
    PdfCopy copy = new PdfSmartCopy(document, outputStream);
    document.open();
    PdfReader reader0 = new PdfReader(dest.getAbsolutePath());
    copy.addDocument(reader0);
    reader0.close();
    PdfReader reader = new PdfReader(in.getAbsolutePath());
    copy.addDocument(reader);
    reader.close();
    document.close();
    Files.move(Paths.get(template.getAbsolutePath()), Paths.get(dest.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
}
Also used : PdfCopy(com.itextpdf.text.pdf.PdfCopy) FileOutputStream(java.io.FileOutputStream) PdfSmartCopy(com.itextpdf.text.pdf.PdfSmartCopy) PdfReader(com.itextpdf.text.pdf.PdfReader) Document(com.itextpdf.text.Document) File(java.io.File)

Example 8 with Document

use of com.itextpdf.text.Document in project ASCIIGenome by dariober.

the class Pdf method convert.

/* M e t h o d s */
public void convert(File pdfOut, float fontSize, boolean append) throws IOException, DocumentException, InvalidColourException {
    // First we write to tmp file then we copy to given destination, possibly appending
    File tmpPdf = Utils.createTempFile(pdfOut.getName(), ".pdf");
    tmpPdf.deleteOnExit();
    List<Paragraph> pdfLines = this.ansiFileToPdfParagraphs(fontSize);
    Rectangle pageSize = new Rectangle((float) (this.getMaxWidth() * 1.01), (float) (this.getMaxHeight()));
    int background256 = Config.get256Color(ConfigKey.background);
    Color pageColor = Xterm256.xterm256ToColor(background256);
    pageSize.setBackgroundColor(new BaseColor(pageColor.getRed(), pageColor.getGreen(), pageColor.getBlue()));
    Document document = new Document(pageSize, 5f, 0f, 0f, 0f);
    // Document document = new Document(new Rectangle((float) (this.getMaxWidth() * 1.01), (float) (this.getMaxHeight())), 5f, 0f, 0f, 0f);
    PdfWriter.getInstance(document, new FileOutputStream(tmpPdf));
    document.open();
    for (Paragraph line : pdfLines) {
        document.add(line);
    }
    document.close();
    if (append) {
        this.appendPdf(tmpPdf, pdfOut);
    } else {
        Files.move(Paths.get(tmpPdf.getAbsolutePath()), Paths.get(pdfOut.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
    }
}
Also used : BaseColor(com.itextpdf.text.BaseColor) Color(java.awt.Color) BaseColor(com.itextpdf.text.BaseColor) FileOutputStream(java.io.FileOutputStream) Rectangle(com.itextpdf.text.Rectangle) Document(com.itextpdf.text.Document) File(java.io.File) Paragraph(com.itextpdf.text.Paragraph)

Example 9 with Document

use of com.itextpdf.text.Document in project Java-Matrix-Benchmark by lessthanoptimal.

the class UtilPlotPdf method saveAsPdf.

public static void saveAsPdf(JFreeChart chart, String FILENAME, int width, int height) {
    File parent = new File(new File(FILENAME).getParent());
    if (!parent.exists()) {
        if (!parent.mkdirs())
            throw new RuntimeException("Can't make directory path");
    }
    Document document = new Document(new Rectangle(width, height));
    try {
        FileOutputStream file = new FileOutputStream(FILENAME);
        PdfWriter writer = PdfWriter.getInstance(document, file);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2d, r2d);
        g2d.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
        g2d.dispose();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) Rectangle(com.itextpdf.text.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) DefaultFontMapper(com.itextpdf.text.pdf.DefaultFontMapper) Document(com.itextpdf.text.Document) PdfTemplate(com.itextpdf.text.pdf.PdfTemplate) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) File(java.io.File)

Example 10 with Document

use of com.itextpdf.text.Document in project Zong by Xenoage.

the class PdfPrinter method print.

/**
 * Prints the given {@link Layout} into the given PDF output stream.
 */
public static void print(Layout layout, OutputStream out) {
    Document document = new Document();
    PdfWriter writer = null;
    try {
        writer = PdfWriter.getInstance(document, out);
    } catch (Exception e) {
        handle(warning(Voc.ErrorWhilePrinting));
    }
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    It<Page> pages = it(layout.getPages());
    for (Page page : pages) {
        // create PDF page
        Size2f pageSize = page.getFormat().getSize();
        float width = Units.mmToPx(pageSize.width, 1);
        float height = Units.mmToPx(pageSize.height, 1);
        document.newPage();
        PdfTemplate tp = cb.createTemplate(width, height);
        // fill PDF page
        Graphics2D g2d = new PdfGraphics2D(cb, width, height);
        INSTANCE.log(Companion.remark("Printing page " + pages.getIndex() + "..."));
        LayoutRenderer.paintToCanvas(layout, pages.getIndex(), new AwtCanvas(g2d, pageSize, CanvasFormat.Vector, CanvasDecoration.None, CanvasIntegrity.Perfect));
        // finish page
        g2d.dispose();
        cb.addTemplate(tp, 0, 0);
    }
    document.close();
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) Size2f(com.xenoage.utils.math.geom.Size2f) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D) AwtCanvas(com.xenoage.zong.renderer.awt.canvas.AwtCanvas) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) Page(com.xenoage.zong.layout.Page) Document(com.itextpdf.text.Document) PdfTemplate(com.itextpdf.text.pdf.PdfTemplate) Graphics2D(java.awt.Graphics2D) 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