-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathConvertSvgToLayoutImage.java
53 lines (38 loc) · 1.71 KB
/
ConvertSvgToLayoutImage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.itextpdf.samples.sandbox.svg;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.svg.converter.SvgConverter;
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
public class ConvertSvgToLayoutImage {
private static final String SRC = "./src/main/resources/svg/";
public static final String DEST = "./target/sandbox/svg/ConvertSvgToLayoutImage.pdf";
public static void main(String[] args) throws IOException {
String svgImage = SRC + "cauldron.svg";
File file = new File(DEST);
file.getParentFile().mkdirs();
new ConvertSvgToLayoutImage().manipulatePdf(svgImage, DEST);
}
public void manipulatePdf(String svgSource, String pdfDest) throws IOException {
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(pdfDest))) {
Document doc = new Document(pdfDocument);
//Create new page
pdfDocument.addNewPage(PageSize.A4);
doc.add(new Paragraph("This is some text added before the SVG image."));
//SVG image
FileInputStream svgPath = new FileInputStream(svgSource);
//Convert to image
Image image = SvgConverter.convertToImage(svgPath, pdfDocument);
//Set scale
image.scaleToFit(PageSize.A4.getWidth()/2, PageSize.A4.getHeight()/2);
//Add to the document
doc.add(image);
doc.add(new Paragraph("This is some text added after the SVG image."));
}
}
}