Search in sources :

Example 6 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project polyGembler by c-zhou.

the class JfreeChart method print.

public void print(String plot_pdf) {
    try {
        float width = jframe.getSize().width, height = jframe.getSize().height;
        Document document = new Document(new Rectangle(width, height));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(plot_pdf));
        document.open();
        PdfContentByte canvas = writer.getDirectContent();
        PdfTemplate template = canvas.createTemplate(width, height);
        Graphics2D g2d = new PdfGraphics2D(template, width, height);
        jframe.paint(g2d);
        g2d.dispose();
        canvas.addTemplate(template, 0, 0);
        document.close();
    } catch (FileNotFoundException | DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) FileOutputStream(java.io.FileOutputStream) DocumentException(com.itextpdf.text.DocumentException) Rectangle(com.itextpdf.text.Rectangle) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D) FileNotFoundException(java.io.FileNotFoundException) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) Document(com.itextpdf.text.Document) PdfTemplate(com.itextpdf.text.pdf.PdfTemplate) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D) Graphics2D(java.awt.Graphics2D)

Example 7 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte 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 8 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project gephi by gephi.

the class NodeLabelRenderer method renderPDF.

public void renderPDF(PDFTarget target, Node node, String label, float x, float y, int fontSize, Color color, float outlineSize, Color outlineColor, boolean showBox, Color boxColor) {
    Font font = fontCache.get(fontSize);
    PdfContentByte cb = target.getContentByte();
    BaseFont bf = target.getBaseFont(font);
    // Box
    if (showBox) {
        cb.setRGBColorFill(boxColor.getRed(), boxColor.getGreen(), boxColor.getBlue());
        if (boxColor.getAlpha() < 255) {
            cb.saveState();
            float alpha = boxColor.getAlpha() / 255f;
            PdfGState gState = new PdfGState();
            gState.setFillOpacity(alpha);
            cb.setGState(gState);
        }
        float textWidth = getTextWidth(bf, fontSize, label);
        float textHeight = getTextHeight(bf, fontSize, label);
        // A height of just textHeight seems to be half the text height sometimes
        // BaseFont getAscentPoint and getDescentPoint may be not very precise
        cb.rectangle(x - textWidth / 2f - outlineSize / 2f, -y - outlineSize / 2f - textHeight, textWidth + outlineSize, textHeight * 2f + outlineSize);
        cb.fill();
        if (boxColor.getAlpha() < 255) {
            cb.restoreState();
        }
    }
    cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
    float textHeight = getTextHeight(bf, fontSize, label);
    if (outlineSize > 0) {
        cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
        cb.setRGBColorStroke(outlineColor.getRed(), outlineColor.getGreen(), outlineColor.getBlue());
        cb.setLineWidth(outlineSize);
        cb.setLineJoin(PdfContentByte.LINE_JOIN_ROUND);
        cb.setLineCap(PdfContentByte.LINE_CAP_ROUND);
        if (outlineColor.getAlpha() < 255) {
            cb.saveState();
            float alpha = outlineColor.getAlpha() / 255f;
            PdfGState gState = new PdfGState();
            gState.setStrokeOpacity(alpha);
            cb.setGState(gState);
        }
        cb.beginText();
        cb.setFontAndSize(bf, font.getSize());
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f);
        cb.endText();
        if (outlineColor.getAlpha() < 255) {
            cb.restoreState();
        }
    }
    cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
    cb.beginText();
    cb.setFontAndSize(bf, font.getSize());
    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f);
    cb.endText();
}
Also used : BaseFont(com.itextpdf.text.pdf.BaseFont) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) BaseFont(com.itextpdf.text.pdf.BaseFont) Font(java.awt.Font) PdfGState(com.itextpdf.text.pdf.PdfGState)

Example 9 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte 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 PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte 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

PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)29 IOException (java.io.IOException)13 DocumentException (com.itextpdf.text.DocumentException)10 Document (com.itextpdf.text.Document)9 PdfWriter (com.itextpdf.text.pdf.PdfWriter)9 BaseFont (com.itextpdf.text.pdf.BaseFont)8 PdfReader (com.itextpdf.text.pdf.PdfReader)7 FileOutputStream (java.io.FileOutputStream)7 Font (com.itextpdf.text.Font)6 Rectangle (com.itextpdf.text.Rectangle)6 PdfImportedPage (com.itextpdf.text.pdf.PdfImportedPage)5 PdfTemplate (com.itextpdf.text.pdf.PdfTemplate)5 Graphics2D (java.awt.Graphics2D)5 Paragraph (com.itextpdf.text.Paragraph)4 PdfGState (com.itextpdf.text.pdf.PdfGState)4 PdfPTable (com.itextpdf.text.pdf.PdfPTable)4 PdfGraphics2D (com.itextpdf.awt.PdfGraphics2D)3 Image (com.itextpdf.text.Image)3 Phrase (com.itextpdf.text.Phrase)3 Color (java.awt.Color)3