Search in sources :

Example 1 with GraphicContext

use of org.apache.xmlgraphics.java2d.GraphicContext in project grafikon by jub77.

the class DrawOutput method processPdf.

private void processPdf(Collection<Image> images, OutputStream stream, DrawLayout layout) throws OutputException {
    PDFDocumentGraphics2D g2d;
    try {
        g2d = new PDFDocumentGraphics2D();
        g2d.setGraphicContext(new GraphicContext());
        FopFactory fopFactory = PdfTransformer.createFopFactory();
        FOUserAgent userAgent = fopFactory.newFOUserAgent();
        FontConfig fc = userAgent.getRendererConfig(MimeConstants.MIME_PDF, new PDFRendererConfigParser()).getFontInfoConfig();
        FontManager fontManager = fopFactory.getFontManager();
        DefaultFontConfigurator fontInfoConfigurator = new DefaultFontConfigurator(fontManager, null, false);
        List<EmbedFontInfo> fontInfoList = fontInfoConfigurator.configure(fc);
        FontInfo fontInfo = new FontInfo();
        FontSetup.setup(fontInfo, fontInfoList, userAgent.getResourceResolver(), fontManager.isBase14KerningEnabled());
        g2d.setFontInfo(fontInfo);
        g2d.setFont(new Font("SansCondensed", Font.PLAIN, g2d.getFont().getSize()));
        List<Dimension> sizes = this.getSizes(images, g2d);
        Dimension size = this.getTotalSize(sizes, layout);
        g2d.setupDocument(stream, size.width, size.height);
        this.drawImages(sizes, images, g2d, layout);
        g2d.finish();
    } catch (IOException | FOPException e) {
        throw new OutputException(e.getMessage(), e);
    }
}
Also used : EmbedFontInfo(org.apache.fop.fonts.EmbedFontInfo) GraphicContext(org.apache.xmlgraphics.java2d.GraphicContext) PDFRendererConfigParser(org.apache.fop.render.pdf.PDFRendererConfig.PDFRendererConfigParser) FOUserAgent(org.apache.fop.apps.FOUserAgent) FontConfig(org.apache.fop.fonts.FontConfig) PDFDocumentGraphics2D(org.apache.fop.svg.PDFDocumentGraphics2D) FopFactory(org.apache.fop.apps.FopFactory) Dimension(java.awt.Dimension) IOException(java.io.IOException) FontManager(org.apache.fop.fonts.FontManager) Font(java.awt.Font) FOPException(org.apache.fop.apps.FOPException) OutputException(net.parostroj.timetable.output2.OutputException) EmbedFontInfo(org.apache.fop.fonts.EmbedFontInfo) FontInfo(org.apache.fop.fonts.FontInfo) DefaultFontConfigurator(org.apache.fop.fonts.DefaultFontConfigurator)

Example 2 with GraphicContext

use of org.apache.xmlgraphics.java2d.GraphicContext in project dkpro-lab by dkpro.

the class ChartUtil method writeChartAsPDF.

/**
 * Exports a JFreeChart to a scalable PDF file.
 *
 * @param chart JFreeChart to export
 * @param aOS stream to write to.
 * @param aWidth width of the chart in pixels
 * @param aHeight height of the chart in pixels
 * @throws IOException if writing the svgFile fails.
 */
public static void writeChartAsPDF(OutputStream aOS, JFreeChart chart, int aWidth, int aHeight) throws IOException {
    // Create an instance of the SVG Generator
    PDFDocumentGraphics2D pdfGenerator = new PDFDocumentGraphics2D(true, aOS, aWidth, aHeight);
    pdfGenerator.setDeviceDPI(PDFDocumentGraphics2D.NORMAL_PDF_RESOLUTION);
    pdfGenerator.setGraphicContext(new GraphicContext());
    pdfGenerator.setSVGDimension(aWidth, aHeight);
    pdfGenerator.setClip(0, 0, aWidth, aHeight);
    pdfGenerator.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
    pdfGenerator.setRenderingHint(KEY_INTERPOLATION, VALUE_INTERPOLATION_BILINEAR);
    chart.setBackgroundPaint(Color.white);
    chart.getPlot().setBackgroundPaint(Color.white);
    // draw the chart in the SVG generator
    chart.draw(pdfGenerator, new Rectangle(aWidth, aHeight));
    pdfGenerator.finish();
}
Also used : GraphicContext(org.apache.xmlgraphics.java2d.GraphicContext) Rectangle(java.awt.Rectangle) PDFDocumentGraphics2D(org.apache.fop.svg.PDFDocumentGraphics2D)

Aggregations

PDFDocumentGraphics2D (org.apache.fop.svg.PDFDocumentGraphics2D)2 GraphicContext (org.apache.xmlgraphics.java2d.GraphicContext)2 Dimension (java.awt.Dimension)1 Font (java.awt.Font)1 Rectangle (java.awt.Rectangle)1 IOException (java.io.IOException)1 OutputException (net.parostroj.timetable.output2.OutputException)1 FOPException (org.apache.fop.apps.FOPException)1 FOUserAgent (org.apache.fop.apps.FOUserAgent)1 FopFactory (org.apache.fop.apps.FopFactory)1 DefaultFontConfigurator (org.apache.fop.fonts.DefaultFontConfigurator)1 EmbedFontInfo (org.apache.fop.fonts.EmbedFontInfo)1 FontConfig (org.apache.fop.fonts.FontConfig)1 FontInfo (org.apache.fop.fonts.FontInfo)1 FontManager (org.apache.fop.fonts.FontManager)1 PDFRendererConfigParser (org.apache.fop.render.pdf.PDFRendererConfig.PDFRendererConfigParser)1