Itext将SVG文字转图片导出FDP中
引入依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>
<!-- 支持中文 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--插入SVG-->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-codec</artifactId>
<version>1.14</version>
</dependency>
将svg文字转为png图片的测试方法测试方法
@Test
public void test5(){
String svgStream="<svg width='24px' height=\"24px\" viewBox=\"0 0 24 24\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n" +
" <title>'图片测试'</title>\n" +
" <g id='图片测试' stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\">\n" +
" <g id='图片测试' transform=\"translate(-532.000000, -348.000000)\" fill-rule=\"nonzero\">\n" +
" <g id='图片测试' transform=\"translate(532.000000, 348.000000)\">\n" +
" <rect id='矩形' fill=\"#000000\" opacity=\"0\" x=\"0\" y=\"0\" width=\"24\" height=\"24\"></rect>\n" +
" <path d=\"M12,8.74999999 C10.208,8.74999999 8.74999999,10.208 8.74999999,12 C8.74999999,13.792 10.208,15.25 12,15.25 C13.792,15.25 15.25,13.792 15.25,12 C15.25,10.208 13.792,8.74999999 12,8.74999999 Z M12,13.75 C11.035,13.75 10.25,12.965 10.25,12 C10.25,11.035 11.035,10.25 12,10.25 C12.965,10.25 13.75,11.035 13.75,12 C13.75,12.965 12.965,13.75 12,13.75 L12,13.75 Z M22.25,11.8 C22.25,11.7445 22.243,11.691 22.2315,11.639 C22.039,6.15499999 17.53,1.75000001 12,1.75000001 C6.46999999,1.75000001 1.96099999,6.15499999 1.7685,11.639 C1.75647757,11.6918309 1.75027397,11.7458191 1.75000001,11.8 C1.75000001,11.823 1.75450001,11.8445 1.7565,11.867 C1.75600001,11.9115 1.75000001,11.955 1.75000001,12 C1.75000001,17.652 6.348,22.25 12,22.25 C17.652,22.25 22.25,17.652 22.25,12 C22.25,11.955 22.244,11.9115 22.2435,11.867 C22.2455,11.8445 22.25,11.823 22.25,11.8 Z M12,3.25000001 C16.5035,3.25000001 20.2205,6.6705 20.696,11.05 L18.7085,11.05 C18.333,7.66600001 15.4325,5.05000001 12,5.05000001 C8.56750001,5.05000001 5.667,7.66600001 5.29150001,11.05 L3.30400001,11.05 C3.77950001,6.6705 7.49650001,3.25000001 12,3.25000001 Z M12,20.75 C7.36099999,20.75 3.564,17.118 3.27799999,12.55 L5.52049999,12.55 C6.17049998,12.55 6.717,12.0475 6.765,11.406 C6.96649999,8.68249999 9.26599999,6.55000001 12,6.55000001 C14.734,6.55000001 17.0335,8.68300001 17.2355,11.406 C17.2835,12.0475 17.83,12.55 18.48,12.55 L20.7225,12.55 C20.436,17.118 16.639,20.75 12,20.75 Z\" id=\"形状\" fill=\"#0088FF\"></path>\n" +
" </g>\n" +
" </g>\n" +
" </g>\n" +
"</svg>";
String pngFilePath="test.png";
convertToPng(svgStream,pngFilePath);
}
private void convertToPng(String str, String pngFilePath) {
File file=new File(pngFilePath);
FileOutputStream fileOutputStream =null;
try{
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
convertToPng(str,fileOutputStream);
}catch (Exception e){
e.printStackTrace();
}finally {
if(fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
测试结果
将svg文字转为png图片导入pdf中
private void convertToPng(String svgCode,OutputStream os) throws IOException, TranscoderException {
byte[] bytes = svgCode.getBytes("utf-8");
PNGTranscoder t = new PNGTranscoder();
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
TranscoderInput input = new TranscoderInput(stream);
TranscoderOutput output = new TranscoderOutput(os);
t.transcode(input, output);
os.flush();
}
@Test
public void test6(){
try{
Document document = new Document(PageSize.A4);
//第二步,创建Writer实例
PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
//创建中文字体
BaseFont bfchinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfchinese, 12, Font.NORMAL);
//第三步,打开文档
document.open();
//svg文本
String svgStream="<svg width='24px' height=\"24px\" viewBox=\"0 0 24 24\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n" +
" <title>'图片测试'</title>\n" +
" <g id='图片测试' stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\">\n" +
" <g id='图片测试' transform=\"translate(-532.000000, -348.000000)\" fill-rule=\"nonzero\">\n" +
" <g id='图片测试' transform=\"translate(532.000000, 348.000000)\">\n" +
" <rect id='矩形' fill=\"#000000\" opacity=\"0\" x=\"0\" y=\"0\" width=\"24\" height=\"24\"></rect>\n" +
" <path d=\"M12,8.74999999 C10.208,8.74999999 8.74999999,10.208 8.74999999,12 C8.74999999,13.792 10.208,15.25 12,15.25 C13.792,15.25 15.25,13.792 15.25,12 C15.25,10.208 13.792,8.74999999 12,8.74999999 Z M12,13.75 C11.035,13.75 10.25,12.965 10.25,12 C10.25,11.035 11.035,10.25 12,10.25 C12.965,10.25 13.75,11.035 13.75,12 C13.75,12.965 12.965,13.75 12,13.75 L12,13.75 Z M22.25,11.8 C22.25,11.7445 22.243,11.691 22.2315,11.639 C22.039,6.15499999 17.53,1.75000001 12,1.75000001 C6.46999999,1.75000001 1.96099999,6.15499999 1.7685,11.639 C1.75647757,11.6918309 1.75027397,11.7458191 1.75000001,11.8 C1.75000001,11.823 1.75450001,11.8445 1.7565,11.867 C1.75600001,11.9115 1.75000001,11.955 1.75000001,12 C1.75000001,17.652 6.348,22.25 12,22.25 C17.652,22.25 22.25,17.652 22.25,12 C22.25,11.955 22.244,11.9115 22.2435,11.867 C22.2455,11.8445 22.25,11.823 22.25,11.8 Z M12,3.25000001 C16.5035,3.25000001 20.2205,6.6705 20.696,11.05 L18.7085,11.05 C18.333,7.66600001 15.4325,5.05000001 12,5.05000001 C8.56750001,5.05000001 5.667,7.66600001 5.29150001,11.05 L3.30400001,11.05 C3.77950001,6.6705 7.49650001,3.25000001 12,3.25000001 Z M12,20.75 C7.36099999,20.75 3.564,17.118 3.27799999,12.55 L5.52049999,12.55 C6.17049998,12.55 6.717,12.0475 6.765,11.406 C6.96649999,8.68249999 9.26599999,6.55000001 12,6.55000001 C14.734,6.55000001 17.0335,8.68300001 17.2355,11.406 C17.2835,12.0475 17.83,12.55 18.48,12.55 L20.7225,12.55 C20.436,17.118 16.639,20.75 12,20.75 Z\" id=\"形状\" fill=\"#0088FF\"></path>\n" +
" </g>\n" +
" </g>\n" +
" </g>\n" +
"</svg>";
ByteArrayOutputStream out = new ByteArrayOutputStream();
convertToPng(svgStream,out);
final byte[] bytes=out.toByteArray();
//创建一个表格
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
Image image = Image.getInstance(bytes);
//设置图片居中
image.setAlignment(Element.ALIGN_CENTER);
//创建一个cell
PdfPCell cell = new PdfPCell(image, true);
// cell.setBorderColor(BaseColor.GRAY);
// 设置cell高度
cell.setFixedHeight(20);
//设置cell上边距
cell.setPaddingTop(2);
//设置cell低边据
cell.setPaddingBottom(2);
cell.setBorder(0);//去除边框
//垂直水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//将cell添加到表格当中
table.addCell(cell);
//document写入表格
document.add(table);
//关闭document
document.close();
}catch (Exception e){
e.printStackTrace();
}
}
结果如下
这里不创建表格也可以
@Test
public void test10(){
try{
Document document = new Document(PageSize.A4);
//第二步,创建Writer实例
PdfWriter.getInstance(document, new FileOutputStream("test10.pdf"));
//第三步,打开文档
document.open();
String svgStream="<svg width='240px' height=\"240px\" viewBox=\"0 0 24 24\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n" +
" <title>'图片测试'</title>\n" +
" <g id='图片测试' stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\">\n" +
" <g id='图片测试' transform=\"translate(-532.000000, -348.000000)\" fill-rule=\"nonzero\">\n" +
" <g id='图片测试' transform=\"translate(532.000000, 348.000000)\">\n" +
" <rect id='矩形' fill=\"#000000\" opacity=\"0\" x=\"0\" y=\"0\" width=\"24\" height=\"24\"></rect>\n" +
" <path d=\"M12,8.74999999 C10.208,8.74999999 8.74999999,10.208 8.74999999,12 C8.74999999,13.792 10.208,15.25 12,15.25 C13.792,15.25 15.25,13.792 15.25,12 C15.25,10.208 13.792,8.74999999 12,8.74999999 Z M12,13.75 C11.035,13.75 10.25,12.965 10.25,12 C10.25,11.035 11.035,10.25 12,10.25 C12.965,10.25 13.75,11.035 13.75,12 C13.75,12.965 12.965,13.75 12,13.75 L12,13.75 Z M22.25,11.8 C22.25,11.7445 22.243,11.691 22.2315,11.639 C22.039,6.15499999 17.53,1.75000001 12,1.75000001 C6.46999999,1.75000001 1.96099999,6.15499999 1.7685,11.639 C1.75647757,11.6918309 1.75027397,11.7458191 1.75000001,11.8 C1.75000001,11.823 1.75450001,11.8445 1.7565,11.867 C1.75600001,11.9115 1.75000001,11.955 1.75000001,12 C1.75000001,17.652 6.348,22.25 12,22.25 C17.652,22.25 22.25,17.652 22.25,12 C22.25,11.955 22.244,11.9115 22.2435,11.867 C22.2455,11.8445 22.25,11.823 22.25,11.8 Z M12,3.25000001 C16.5035,3.25000001 20.2205,6.6705 20.696,11.05 L18.7085,11.05 C18.333,7.66600001 15.4325,5.05000001 12,5.05000001 C8.56750001,5.05000001 5.667,7.66600001 5.29150001,11.05 L3.30400001,11.05 C3.77950001,6.6705 7.49650001,3.25000001 12,3.25000001 Z M12,20.75 C7.36099999,20.75 3.564,17.118 3.27799999,12.55 L5.52049999,12.55 C6.17049998,12.55 6.717,12.0475 6.765,11.406 C6.96649999,8.68249999 9.26599999,6.55000001 12,6.55000001 C14.734,6.55000001 17.0335,8.68300001 17.2355,11.406 C17.2835,12.0475 17.83,12.55 18.48,12.55 L20.7225,12.55 C20.436,17.118 16.639,20.75 12,20.75 Z\" id=\"形状\" fill=\"#0088FF\"></path>\n" +
" </g>\n" +
" </g>\n" +
" </g>\n" +
"</svg>";
ByteArrayOutputStream out = new ByteArrayOutputStream();
convertToPng(svgStream,out);
final byte[] bytes=out.toByteArray();
Image image = Image.getInstance(bytes);
//设置图片居中
image.setAlignment(Element.ALIGN_CENTER);
//重置图片大小
image.scaleAbsolute(150,150);
document.add(image);
//关闭document
document.close();
}catch (Exception e){
e.printStackTrace();
}
}
结果如下: