我想将PDF转换为SVG。我已经使用Apache PDFBox和Batik库编写了自己的Java程序。
PDDocument document = PDDocument.load( pdfFile );
DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document svgDocument = domImpl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(svgDocument);
ctx.setEmbeddedFontsOn(true);
// Ask the test to render into the SVG Graphics2D implementation.
for(int i = 0 ; i < document.getNumberOfPages() ; i++){
String svgFName = svgDir+"page"+i+".svg";
(new File(svgFName)).createNewFile();
// Create an instance of the SVG Generator.
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx,false);
Printable page = document.getPrintable(i);
page.print(svgGenerator, document.getPageFormat(i), i);
svgGenerator.stream(svgFName);
}
这个解决方案是可行的,但生成的SVG文件大小巨大(比原始PDF文件大多倍)。通过在文本编辑器中查看SVG,我已经找到了问题所在:即使字符的字体属性相同,它也会将原始文档中的每个字符都封闭在自己的<text> </text>
块中。
例如,单词“hello”将显示为6个不同的文本块。
有没有办法修复上述代码?或者是否有其他更有效的解决方案?