使用 IText7 将 SVG 图像添加到 PDF
Adding an SVG image to PDF using IText7
我正在使用 iText7 生成 PDF 文件。我需要帮助将 svg 文件添加到 PDF 文档。需要使用 URL.
从远程位置获取 svg 文件
我能够使用 apache batik 库来完成这项工作。这是我的做法。
Maven 依赖
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.9</version>
</dependency>
为 SvgImage 自定义 ITagWorker
import com.itextpdf.html2pdf.attach.ITagWorker;
import com.itextpdf.html2pdf.attach.ProcessorContext;
import com.itextpdf.html2pdf.html.node.IElementNode;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.layout.IPropertyContainer;
import com.itextpdf.layout.element.Image;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import java.io.ByteArrayOutputStream;
public class SvgImageWorker implements ITagWorker {
private Image img;
@Override
public void processEnd(IElementNode iElementNode, ProcessorContext processorContext) {
String url = iElementNode.getAttribute("src");
JPEGTranscoder jpegTranscoder = new JPEGTranscoder();
// Set the transcoding hints.
jpegTranscoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));
// Create the transcoder input.
try {
TranscoderInput input = new TranscoderInput(url);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(10000);
TranscoderOutput output = new TranscoderOutput(byteArrayOutputStream);
// Save the image.
jpegTranscoder.transcode(input, output);
img = new Image(ImageDataFactory.create(byteArrayOutputStream.toByteArray()));
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean processContent(String s, ProcessorContext processorContext) {
return false;
}
@Override
public boolean processTagChild(ITagWorker iTagWorker, ProcessorContext processorContext) {
return false;
}
@Override
public IPropertyContainer getElementResult() {
return img;
}
}
我正在使用 iText7 生成 PDF 文件。我需要帮助将 svg 文件添加到 PDF 文档。需要使用 URL.
从远程位置获取 svg 文件我能够使用 apache batik 库来完成这项工作。这是我的做法。
Maven 依赖
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.9</version>
</dependency>
为 SvgImage 自定义 ITagWorker
import com.itextpdf.html2pdf.attach.ITagWorker;
import com.itextpdf.html2pdf.attach.ProcessorContext;
import com.itextpdf.html2pdf.html.node.IElementNode;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.layout.IPropertyContainer;
import com.itextpdf.layout.element.Image;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import java.io.ByteArrayOutputStream;
public class SvgImageWorker implements ITagWorker {
private Image img;
@Override
public void processEnd(IElementNode iElementNode, ProcessorContext processorContext) {
String url = iElementNode.getAttribute("src");
JPEGTranscoder jpegTranscoder = new JPEGTranscoder();
// Set the transcoding hints.
jpegTranscoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));
// Create the transcoder input.
try {
TranscoderInput input = new TranscoderInput(url);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(10000);
TranscoderOutput output = new TranscoderOutput(byteArrayOutputStream);
// Save the image.
jpegTranscoder.transcode(input, output);
img = new Image(ImageDataFactory.create(byteArrayOutputStream.toByteArray()));
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean processContent(String s, ProcessorContext processorContext) {
return false;
}
@Override
public boolean processTagChild(ITagWorker iTagWorker, ProcessorContext processorContext) {
return false;
}
@Override
public IPropertyContainer getElementResult() {
return img;
}
}