Search in sources :

Example 1 with ImgTemplate

use of com.lowagie.text.ImgTemplate in project charts by vaadin.

the class PdfExportDemo method drawUnscaledSvg.

private Image drawUnscaledSvg(PdfContentByte contentByte) throws IOException {
    // First, lets create a graphics node for the SVG image.
    GraphicsNode imageGraphics = buildBatikGraphicsNode(svgStr);
    // SVG's width and height
    float width = (float) imageGraphics.getBounds().getWidth();
    float height = (float) imageGraphics.getBounds().getHeight();
    // Create a PDF template for the SVG image
    PdfTemplate template = contentByte.createTemplate(width, height);
    // Create Graphics2D rendered object from the template
    Graphics2D graphics = template.createGraphics(width, height);
    try {
        // SVGs can have their corner at coordinates other than (0,0).
        Rectangle2D bounds = imageGraphics.getBounds();
        graphics.translate(-bounds.getX(), -bounds.getY());
        // Paint SVG GraphicsNode with the 2d-renderer.
        imageGraphics.paint(graphics);
        // image.
        return new ImgTemplate(template);
    } catch (BadElementException e) {
        throw new RuntimeException("Couldn't generate PDF from SVG", e);
    } finally {
        // Manual cleaning (optional)
        graphics.dispose();
    }
}
Also used : ImgTemplate(com.lowagie.text.ImgTemplate) BadElementException(com.lowagie.text.BadElementException) Rectangle2D(java.awt.geom.Rectangle2D) PdfTemplate(com.lowagie.text.pdf.PdfTemplate) GraphicsNode(org.apache.batik.gvt.GraphicsNode) Graphics2D(java.awt.Graphics2D)

Aggregations

BadElementException (com.lowagie.text.BadElementException)1 ImgTemplate (com.lowagie.text.ImgTemplate)1 PdfTemplate (com.lowagie.text.pdf.PdfTemplate)1 Graphics2D (java.awt.Graphics2D)1 Rectangle2D (java.awt.geom.Rectangle2D)1 GraphicsNode (org.apache.batik.gvt.GraphicsNode)1