itextpdf 放入SVG矢量图

为什么80%的码农都做不了架构师?>>>   hot3.png

maven pom.xml

     
        1.8
        2.0.1
        5.5.8
    

    
        
            org.apache.xmlgraphics
            xmlgraphics-commons
            ${xmlgraphics.version}
        
        
            org.apache.xmlgraphics
            batik-svggen
            ${batik.version}
        
        
            org.apache.xmlgraphics
            batik-xml
            ${batik.version}
        
        
            org.apache.xmlgraphics
            batik-rasterizer
            ${batik.version}
        

   

接入代码

        float width=200;
        float height=200;
        //构建svgDocument
        final String parser = XMLResourceDescriptor.getXMLParserClassName();
        
        SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
        UserAgent userAgent = new UserAgentAdapter();
        DocumentLoader loader = new DocumentLoader(userAgent);
        BridgeContext ctx = new BridgeContext(userAgent, loader);
        ctx.setDynamicState(BridgeContext.STATIC);
        GVTBuilder builder = new GVTBuilder();
        
        PdfTemplate template = pcb.createTemplate(width, height);
        //生成awt Graphics2D
        Graphics2D g2d = new PdfGraphics2D(template, width, height);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        SVGDocument svgDocument = factory.createSVGDocument("http://xxx/xx.svg");
        GraphicsNode graphNode = builder.build(ctx, svgDocument);
        //画svg到画布
        graphNode.paint(g2d);
        g2d.dispose();
        //生成Img
        ImgTemplate img = new ImgTemplate(template);
        //添加图片到pdf Document
        document.add(img);





转载于:https://my.oschina.net/osgit/blog/551410

你可能感兴趣的:(itextpdf 放入SVG矢量图)