pdf转svg java,将pdf转换成svg

I want to convert PDF to SVG please suggest some libraries/executable that will be able to do this efficiently. I have written my own java program using the apache PDFBox and Batik libraries -

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

}

This solution works great but the size of the resulting svg files in huge.(many times greater than the pdf). I have figured out where the problem is by looking at the svg in a text editor. it encloses every character in the original document in its own block even if the font properties of the characters is the same. For example the word hello will appear as 6 different text blocks. Is there a way to fix the above code? or please suggest another solution that will work more efficiently.

解决方案

Inkscape can also be used to convert PDF to SVG. It's actually remarkably good at this, and although the code that it generates is a bit bloated, at the very least, it doesn't seem to have the particular issue that you are encountering in your program. I think it would be challenging to integrate it directly into Java, but inkscape provides a convenient command-line interface to this functionality, so probably the easiest way to access it would be via a system call.

To use Inkscape's command-line interface to convert a PDF to an SVG, use:

inkscape -l out.svg in.pdf

Which you can then probably call using:

Runtime.getRuntime().exec("inkscape -l out.svg in.pdf")

I think exec() is synchronous and only returns after the process completes (although I'm not 100% sure on that), so you shoudl be able to just read "out.svg" after that. In any case, Googling "java system call" will yield more info on how to do that part correctly.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值