PDFBox - 将页面保存为 SVG 格式

PDFBox - Saving a page as SVG format

我正在尝试将给定 PDF 文档的每一页保存为 SVG 文件。与相当流行的 SVG 到 PDF 相比,关于该主题的信息似乎很少。

是否有使用 PDFBox、Batik 或标准库功能的任意组合来执行此操作的简单方法?

这是一些保存 PDF 所有页面的代码,我可能从这里或 from batik itself

try (PDDocument pdfboxDocument = PDDocument.load(new File(dir, PDFFILE)))
{
    PDFRenderer r = new PDFRenderer(pdfboxDocument);
    for (int i = 0; i < pdfboxDocument.getNumberOfPages(); ++i)
    {
        String svgNS = "http://www.w3.org/2000/svg";
        DOMImplementation impl = GenericDOMImplementation.getDOMImplementation();
        Document myFactory = impl.createDocument(svgNS, "svg", null);
        SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(myFactory);
        ctx.setEmbeddedFontsOn(true);
        SVGGraphics2D g2d = new SVGGraphics2D(ctx, true);
        r.renderPageToGraphics(i, g2d);
        String filename = "test-" + (i + 1) + ".svg";
        try (Writer out = new OutputStreamWriter(new FileOutputStream(new File(dir, filename)), "UTF-8"))
        {
            g2d.stream(out, true);
        }
    }
}

pom.xml 摘录:

<dependencies>
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>batik-svggen</artifactId>
        <version>${batik.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>batik-codec</artifactId>
        <version>${batik.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>batik-dom</artifactId>
        <version>${batik.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox-app</artifactId>
        <version>2.0.22</version>
    </dependency>
    <dependency>
        <groupId>com.github.jai-imageio</groupId>
        <artifactId>jai-imageio-core</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.jai-imageio</groupId>
        <artifactId>jai-imageio-jpeg2000</artifactId>
        <version>1.4.0</version>
    </dependency>
</dependencies>