SVG行未在PDF中正确呈现

时间:2014-09-17 11:49:21

标签: java pdf svg itext batik

我正在使用iText和一些SVG渲染库。

我想将SVG图像渲染为PDF。我使用以下代码来做到这一点。现在问题是我给它一个面积图SVG,它没有正确渲染线。附上截图。

已使用以下课程:

import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.svg.SVGDocument;

这里是警察片段:

    String parser = XMLResourceDescriptor.getXMLParserClassName();
    factory = new SAXSVGDocumentFactory(parser);

    UserAgent userAgent = new UserAgentAdapter();
    DocumentLoader loader = new DocumentLoader(userAgent);
    ctx = new BridgeContext(userAgent, loader);
    ctx.setDynamicState(BridgeContext.DYNAMIC);

    builder = new GVTBuilder();

    PdfContentByte cb = writer.getDirectContent();


    PdfTemplate chartImageTemplate = cb.createTemplate(1000, 1000);


    SVGDocument svgDoc=null;
    try {
        InputStream in = new ByteArrayInputStream(chartImage.getBytes());
        svgDoc = factory.createSVGDocument("", in);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try{
        GraphicsNode mapGraphics = builder.build(ctx, svgDoc);

        DefaultFontMapper mapper = new DefaultFontMapper();
        PdfGraphics2D g2 = (PdfGraphics2D)cb.createGraphics(1000, 500, mapper);
        Graphics2D g2d = g2;
        mapGraphics.paint(g2d);
        g2d.dispose();

        cb.addTemplate(chartImageTemplate, 0, 0);
        return mapGraphics.getGeometryBounds().getHeight();
    }catch(Exception e){

    }

您可以在此处找到SVG: http://speedy.sh/xvDp8/demo.svg

实际SVG:  enter image description here

呈现PDF

enter image description here

0 个答案:

没有答案