Example usage for com.itextpdf.text.pdf PdfContentByte createGraphics

List of usage examples for com.itextpdf.text.pdf PdfContentByte createGraphics

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfContentByte createGraphics.

Prototype

public java.awt.Graphics2D createGraphics(final float width, final float height) 

Source Link

Document

Gets a Graphics2D to write on.

Usage

From source file:ca.sqlpower.wabit.report.LayoutToPDF.java

License:Open Source License

public void writePDF() throws DocumentException, FileNotFoundException, PrinterException {
    monitorableHelper.setStarted(true);//from w  ww  .j  a va  2s  . c o m
    int pageNum = 0;

    int numPages = layout.getNumberOfPages();
    monitorableHelper.setJobSize(numPages);
    Page page = layout.getPage();
    OutputStream out = fileOS;
    Rectangle pageSize;
    pageSize = new Rectangle(page.getWidth(), page.getHeight());

    Document pdfDoc = new Document(pageSize, 0f, 0f, 0f, 0f);

    PdfWriter pdfOut = PdfWriter.getInstance(pdfDoc, out);
    pdfDoc.open();
    pdfDoc.addCreator("Wabit " + WabitVersion.VERSION);
    PdfContentByte pdfContent = pdfOut.getDirectContent();
    Graphics2D pdfGraphics = null;
    try {
        while (pageNum < numPages) {
            monitorableHelper.checkCancelled();
            monitorableHelper.setProgress(pageNum);
            pdfGraphics = pdfContent.createGraphics(pageSize.getWidth(), pageSize.getHeight());
            int flag = layout.print(pdfGraphics, layout.getPageFormat(pageNum), pageNum);

            if (watermarker != null) {
                java.awt.Rectangle watermarkSize = new java.awt.Rectangle();
                watermarkSize.setSize(Math.round(pageSize.getWidth()), Math.round(pageSize.getHeight()));
                watermarker.watermark(pdfGraphics, watermarkSize);
            }

            pdfGraphics.dispose();
            pdfGraphics = null;

            if (flag == Printable.NO_SUCH_PAGE)
                break;

            pdfDoc.newPage();

            pageNum++;
        }
    } finally {
        if (pdfGraphics != null)
            pdfGraphics.dispose();
        if (pdfDoc != null)
            pdfDoc.close();
        monitorableHelper.setFinished(true);
    }
}

From source file:net.algem.billing.InvoiceView.java

License:Open Source License

private void drawPdf(Document document, PdfWriter writer) {

    PdfContentByte cb = writer.getDirectContent();
    Graphics2D g = cb.createGraphics(document.getPageSize().getWidth(), document.getPageSize().getHeight());
    Quote quote = get();/*w w w.  j  av a 2  s .  c o  m*/
    String genericLabel = invoice.getClass() == Quote.class ? BundleUtil.getLabel("Quotation.label")
            : BundleUtil.getLabel("Invoice.label");
    int left = ImageUtil.mmToPoints(110);
    int top = ImageUtil.mmToPoints(50);
    int bottom = ImageUtil.mmToPoints(297 - 20);// hauteur de page - 20 mm de marge
    int margin = ImageUtil.mmToPoints(15);

    Font defaultFont = g.getFont();
    g.setFont(serif);
    int itemsHeight = ((g.getFontMetrics().getHeight() * 2) * (quote.getItems().size() - 1)); // average
    int footerHeight = InvoiceFooterEditor.getFooter().size() * 10;
    int maxHeight = top + 160 + 20 + itemsHeight + ImageUtil.mmToPoints(50);

    drawHeader(g, defaultFont, quote, genericLabel, top, left, margin);
    int tablebottom = drawContent(g, quote, top, margin);

    if (maxHeight > (bottom - footerHeight)) {
        g.setFont(serif.deriveFont(Font.ITALIC));
        g.drawString(MessageUtil.getMessage("see.back.page.label"), margin, tablebottom + 10);
        drawFooter(g, margin, bottom);
        //IMPORTANT
        g.dispose();
        document.newPage();
        g = cb.createGraphics(document.getPageSize().getWidth(), document.getPageSize().getHeight());

        g.setFont(serif.deriveFont(Font.ITALIC));
        g.drawString("(page 2)", margin, top + 90);
        g.setFont(serif);
        g.drawString(genericLabel + " : " + quote.getNumber(), margin, top + 100);
        // description
        g.drawString(BundleUtil.getLabel("Invoice.description.label") + " : " + quote.getDescription(), margin,
                top + 140);
        // pied tableau
        new InvoiceFooterElement(margin, top + 160, quote).draw(g);
        // infos lgales
        drawFooter(g, margin, bottom);
        g.dispose();
    } else {
        // pied tableau
        new InvoiceFooterElement(margin, tablebottom + 20, quote).draw(g);
        // infos lgales
        drawFooter(g, margin, bottom);
        g.dispose();
    }

}

From source file:omr.score.ui.SheetPdfOutput.java

License:Open Source License

public void write() throws Exception {
    FileOutputStream fos = new FileOutputStream(file);
    Document document = null;/*  ww  w  . ja  v  a  2 s  . c  o m*/
    PdfWriter writer = null;

    try {
        for (TreeNode pn : score.getPages()) {
            Page page = (Page) pn;
            Dimension dim = page.getDimension();

            if (document == null) {
                document = new Document(new Rectangle(dim.width, dim.height));
                writer = PdfWriter.getInstance(document, fos);
                document.open();
            } else {
                document.setPageSize(new Rectangle(dim.width, dim.height));
                document.newPage();
            }

            PdfContentByte cb = writer.getDirectContent();
            Graphics2D g2 = cb.createGraphics(dim.width, dim.height);
            g2.scale(1, 1);

            // Painting
            PagePhysicalPainter painter = new PagePhysicalPainter(g2, Color.BLACK, // Foreground color
                    false, // No voice painting
                    true, // Paint staff lines
                    false); // No annotations
            page.accept(painter);

            // This is the end...
            g2.dispose();
        }
    } catch (Exception ex) {
        logger.warn("Error printing " + score.getRadix(), ex);
        throw ex;
    } finally {
        if (document != null) {
            document.close();
        }
    }

    fos.close();
}

From source file:org.audiveris.omr.score.ui.BookPdfOutput.java

License:Open Source License

/**
 * Write the PDF output for the provided sheet if any, otherwise for the whole book.
 *
 * @param sheet desired sheet or null/* ww  w.java2 s .com*/
 * @throws Exception if printing goes wrong
 */
public void write(Sheet sheet) throws Exception {
    FileOutputStream fos = null;
    Document document = null;
    PdfWriter writer = null;

    try {
        final List<SheetStub> stubs = (sheet != null) ? Arrays.asList(sheet.getStub()) : book.getValidStubs();
        fos = new FileOutputStream(file);

        for (SheetStub stub : stubs) {
            final int width = stub.getSheet().getWidth();
            final int height = stub.getSheet().getHeight();

            if (document == null) {
                document = new Document(new Rectangle(width, height));
                writer = PdfWriter.getInstance(document, fos);
                document.open();
            } else {
                document.setPageSize(new Rectangle(width, height));
                document.newPage();
            }

            PdfContentByte cb = writer.getDirectContent();
            Graphics2D g2 = cb.createGraphics(width, height);

            // Scale: 1
            g2.scale(1, 1);

            // Anti-aliasing ON
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // Painting
            SheetResultPainter painter = new SheetResultPainter(stub.getSheet(), g2, false, // No voice painting
                    true, // Paint staff lines
                    false); // No annotations
            g2.setColor(Color.BLACK);

            painter.process();

            // This is the end...
            g2.dispose();
        }

        logger.info("Book printed to {}", file);
    } finally {
        if (document != null) {
            document.close();
        }

        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ignored) {
            }
        }
    }
}

From source file:org.saiku.web.export.PdfReport.java

License:Apache License

public byte[] pdf(QueryResult qr, String svg) throws Exception {

    int resultWidth = qr != null && qr.getCellset() != null && qr.getCellset().size() > 0
            ? qr.getCellset().get(0).length
            : 0;/*from  w  w w  .ja  v  a  2s .  co  m*/
    if (resultWidth == 0) {
        throw new SaikuServiceException("Cannot convert empty result to PDF");
    }
    Rectangle size = PageSize.A4.rotate();
    if (resultWidth > 8) {
        size = PageSize.A3.rotate();
    }
    if (resultWidth > 16) {
        size = PageSize.A2.rotate();
    }
    if (resultWidth > 32) {
        size = PageSize.A1.rotate();
    }
    if (resultWidth > 64) {
        size = PageSize.A0.rotate();
    }

    Document document = new Document(size, 15, 15, 10, 10);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, baos);
    document.open();
    populatePdf(document, writer, qr);

    // do we want to add a svg image?
    if (StringUtils.isNotBlank(svg)) {
        document.newPage();
        StringBuffer s1 = new StringBuffer(svg);
        if (!svg.startsWith("<svg xmlns=\"http://www.w3.org/2000/svg\" ")) {
            s1.insert(s1.indexOf("<svg") + 4, " xmlns='http://www.w3.org/2000/svg'");
        }

        String t = "<?xml version='1.0' encoding='ISO-8859-1'" + " standalone='no'?>" + s1.toString();
        PdfContentByte cb = writer.getDirectContent();
        cb.saveState();
        cb.concatCTM(1.0f, 0, 0, 1.0f, 36, 0);
        float width = document.getPageSize().getWidth() - 20;
        float height = document.getPageSize().getHeight() - 20;
        Graphics2D g2 = cb.createGraphics(width, height);
        //g2.rotate(Math.toRadians(-90), 100, 100);
        PrintTranscoder prm = new PrintTranscoder();
        TranscoderInput ti = new TranscoderInput(new StringReader(t));
        prm.transcode(ti, null);
        PageFormat pg = new PageFormat();
        Paper pp = new Paper();
        pp.setSize(width, height);
        pp.setImageableArea(5, 5, width, height);
        pg.setPaper(pp);
        prm.print(g2, pg, 0);
        g2.dispose();
        cb.restoreState();
    }

    document.close();
    return baos.toByteArray();
}