itextpdf 放入SVG矢量图

maven pom.xml

     <properties>
        <batik.version>1.8</batik.version>
        <xmlgraphics.version>2.0.1</xmlgraphics.version>
        <itextpdf.version>5.5.8</itextpdf.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>xmlgraphics-commons</artifactId>
            <version>${xmlgraphics.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-svggen</artifactId>
            <version>${batik.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-xml</artifactId>
            <version>${batik.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-rasterizer</artifactId>
            <version>${batik.version}</version>
        </dependency>

   </dependencies>

接入代码

        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);





你可能感兴趣的:(svg,Batik,Itextpdf)