我想将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), …Run Code Online (Sandbox Code Playgroud) 我是JavaFX 2.2的新手,到目前为止,我找不到在JavaFX 2.2应用程序中显示SVG图像的方法.我看了看Batik,但它没有为我做伎俩,因为它转换为BufferedImages而不是javafx.ImageView.
有没有办法在JavaFX应用程序中显示SVG图像?或者你至少可以从JavaFX导出SVG图像?该功能是否Node.snapshot()以任何方式帮助那里?
什么是实际工作的最小的Java SVG引擎(最小/最小的罐子)?如果你的答案是Batik,那么最小的dep是什么.让它在一个简单的Java应用程序中工作的图表?
我查看了Batik网站上的依赖图,但它看起来像是典型的Apache混乱.还有更好的选择吗?
简单地说,我正在寻找一种使用batik库从SVG文件制作ImageIcon的方法.我不想首先将SVG光栅转换为磁盘,我只是希望能够从jar文件中提取svg并将其作为UI元素.
我觉得这应该相当容易,但蜡染javadocs并没有告诉我我需要知道什么.
(为什么选择蜡染?好吧,我们已经在使用它了,所以我们不必经营另一个合法的图书馆.)
我正在使用Batik来处理SVG图像.有没有办法从SVG文件中获取java.awt.image.BufferedImage?
我知道有转码器,我可以将SVG转码为例如PNG,然后用ImageIO.read()加载PNG但我不想拥有临时文件.
我有SVG文件,其中包含以下图片:
每个箭头都由如下代码表示:
<g
transform="matrix(-1,0,0,-1,149.82549,457.2455)"
id="signS"
inkscape:label="#sign">
<title
id="title4249">South, 180</title>
<path
sodipodi:nodetypes="ccc"
inkscape:connector-curvature="0"
id="path4251"
d="m 30.022973,250.04026 4.965804,-2.91109 4.988905,2.91109"
style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:0.6855976px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" />
<rect
y="250.11305"
x="29.768578"
height="2.6057031"
width="10.105703"
id="rect4253"
style="fill:#008000;fill-opacity:1;stroke:#000000;stroke-width:0.53715414;stroke-opacity:1" />
</g>
Run Code Online (Sandbox Code Playgroud)
我想计算矩形(节点)的绝对位置rect.为了做到这一点,我需要评估transform标签标签内的表达式g(matrix(-1,0,0,-1,149.82549,457.2455)在上面的例子中).
我该怎么做?
我假设第一步是SVGDocument使用Apache Batik读取文件:
import java.io.IOException;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
try {
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
String uri = "http://www.example.org/diagram.svg";
Document doc = f.createDocument(uri);
} catch (IOException ex) {
// ...
} …Run Code Online (Sandbox Code Playgroud) 我尝试将svg转换为PNG.svg文件来自服务器Inputstream.
首先,我将svg流转换为字节数组:
byte[] streamBytes = IOUtils.toByteArray(svgStream);
Run Code Online (Sandbox Code Playgroud)
然后我OutputStream用以下代码将字节转换为(PNG).
private ByteArrayOutputStream svgToPng(byte[] streamBytes)
throws TranscoderException, IOException {
PNGTranscoder t = new PNGTranscoder();
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(streamBytes));
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(ostream);
t.transcode(input, output);
ostream.flush();
// ostream.close();
return ostream;
}
Run Code Online (Sandbox Code Playgroud)
但我通过" t.transcode(input, output);" 得到空指针异常
org.apache.batik.transcoder.TranscoderException: null
Enclosed Exception:
Premature end of file.
graphdata : null
at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:136)
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
Run Code Online (Sandbox Code Playgroud)
注意:如果我将svgstream保存在磁盘上并使用以下transcoderinput和uri构造函数,那么它可以工作.但在我的情况下,我不想保存在磁盘上.
TranscoderInput input = new TranscoderInput(new File("c:/a.svg").toURI().toString());
Run Code Online (Sandbox Code Playgroud) 有许多与阅读和解析SVG路径相关的未解答的问题:
这个问题和答案旨在解决所有这些问题.
SVG path元素包含数据属性(d).有时需要从SVG文件中加载,解析和提取路径信息.
如何使用Java从SVG文件加载,解析和提取SVG路径信息?
我正在尝试缩放图像,修改它,然后输出到另一种图像格式.到目前为止,我一直在使用apache蜡染库.对于简单的转换,这很容易.为了削减svg,这很容易.
但是,我似乎无法弄清楚如何缩放到svg创建的完整图像.也就是说,我可以将感兴趣的区域指定为边界矩形,然后缩放在边界矩形上工作,但我不知道如何缩放svg的图像.
这是我到目前为止:
...
//set the output width and height
transcoder.addTranscodingHint( PNGTranscoder.KEY_WIDTH, new Float( newSize.width ) );
transcoder.addTranscodingHint( PNGTranscoder.KEY_HEIGHT, new Float( newSize.height ) );
//set the aoi for scaling. Unsure what to do here.
transcoder.addTranscodingHint( PNGTranscoder.KEY_AOI, new Rectangle( 0, 0, 100, 100 ) );
...
Run Code Online (Sandbox Code Playgroud) 在batik的文档中,它展示了如何从类org.apache.batik.dom.svg.SVGDOMImplementation获取DOM实现的实例.
但是,从同一网站下载Batik 1.8后,我无法在任何地方找到这个课程.
我下载了1.7版本,并在batik-svg-dom.jar中找到它,但它不存在于1.8内的同一个jar中(或者据我所知,在该软件包的任何一个jar中).
此类是否已重命名/重构/替换?如果是这样,你如何在蜡染1.8中获得DOM实现的实例?