【发布时间】:2019-06-13 02:44:15
【问题描述】:
我可以使用下面的代码在 PDF 中添加 SVG 图像,但图像的对齐方式需要折腾。我想将图像保存在一个狭窄的区域(假设总是 300 x 300 大小)。如果图像更大,它应该缩小/压缩并适合这个尺寸。我们如何才能做到这一点。
PdfDocument doc = null;
try {
doc = new PdfDocument(new PdfWriter(new FileOutputStream(new File("D:\\test.pdf")),
new WriterProperties().setCompressionLevel(0)));
doc.addNewPage();
URL svgUrl = null;
String svgPath = "...svgPathHere";
try {
svgUrl = new URL(svgPath);
} catch(MalformedURLException mue) {
System.out.println("Exception caught" + mue.getMessage() );
}
if (svgUrl == null){
try {
svgUrl = new File(svgPath).toURI().toURL();
} catch(Throwable th) {
System.out.println("Exception caught" + th.getMessage());
}
}
SvgConverter.drawOnDocument(svgUrl.openStream(), doc, 1, 100, 200); // 100 and 200 are x and y coordinate of the location to draw at
doc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
除了上述问题,SvgConverter 的 drawOnDocument() 方法为我们提供了通过 x 和 y 坐标定位 svg 的控件。有没有更好的方法来处理定位? (如左上、右上)
【问题讨论】:
标签: image pdf svg pdf-generation itext7