Example usage for com.itextpdf.awt PdfGraphics2D PdfGraphics2D

List of usage examples for com.itextpdf.awt PdfGraphics2D PdfGraphics2D

Introduction

In this page you can find the example usage for com.itextpdf.awt PdfGraphics2D PdfGraphics2D.

Prototype

public PdfGraphics2D(PdfContentByte cb, float width, float height, FontMapper fontMapper, boolean onlyShapes,
        boolean convertImagesToJPEG, float quality) 

Source Link

Document

Constructor for PDFGraphics2D.

Usage

From source file:groove.io.external.util.GraphToPDF.java

License:Apache License

@Override
public void renderGraph(JGraph<?> graph, File file) throws PortException {
    // Get graph bounds. If not available, do nothing (probably empty graph)
    Rectangle2D bounds = graph.getGraphBounds();
    if (bounds == null) {
        return;// w  w  w .  jav a 2  s .  c  o  m
    }
    Rectangle bound = new Rectangle((float) bounds.getWidth(), (float) bounds.getHeight());

    try (FileOutputStream fos = new FileOutputStream(file)) {
        Document document = new Document(bound);
        // Open file, create PDF document
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        // Set some metadata
        document.addCreator(Version.getAbout());

        // Open document, get graphics
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        boolean onlyShapes = true;
        //The embedded fonts most likely do not contain all necessary glyphs, so using outlines instead
        // onlyShapes makes PDF considerably bigger, but no alternative at the moment
        PdfGraphics2D pdf2d = new PdfGraphics2D(cb, (float) bounds.getWidth(), (float) bounds.getHeight(),
                new DefaultFontMapper(), onlyShapes, false, (float) 100.0);

        // Render
        toGraphics(graph, pdf2d);

        // Cleanup
        pdf2d.dispose();
        document.close();
    } catch (DocumentException | IOException e) {
        throw new PortException(e);
    }
}