use of com.itextpdf.kernel.pdf.PdfDocument in project spring-learning by moon-zhou.
the class FillTemplate method main.
public static void main(String[] args) {
try {
Map<String, String> params = new HashMap<>();
params.put("applicant", "张三");
params.put("name", "lisi");
params.put("applicantId", "666");
params.put("email", "1@1.com");
String templatePath = "/Users/xxx/tmp/pdf/PDF 模板测试 1.pdf";
String newPDFPath = "/Users/xxx/tmp/pdf/PDF 模板测试 2.pdf";
String fontPath = "/Users/xxx/tmp/pdf/Alibaba-PuHuiTi-Regular.ttf";
PdfDocument pdf = new PdfDocument(new PdfReader(templatePath), new PdfWriter(newPDFPath));
/*PdfFont font = PdfFontFactory.createFont(this.getClass().getClassLoader().getResource("/").getPath()
+ "font/Alibaba-PuHuiTi-Regular.ttf");*/
PdfFont font = PdfFontFactory.createFont(fontPath);
if (params != null && !params.isEmpty()) {
// 有参数才替换
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
Map<String, PdfFormField> fields = form.getFormFields();
// 获取所有的表单域
for (String param : params.keySet()) {
PdfFormField formField = fields.get(param);
// 获取某个表单域
if (formField != null) {
// 替换值
formField.setFont(font).setValue(params.get(param));
// 替换值
// formField.setValue(params.get(param));
}
}
// 锁定表单,不让修改
form.flattenFields();
}
pdf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.itextpdf.kernel.pdf.PdfDocument in project spring-learning by moon-zhou.
the class Demo001 method main.
public static void main(String[] args) {
try {
String dest = "/Users/xxx/tmp/pdf/demo001.pdf";
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
document.add(new Paragraph("Hello World!"));
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
use of com.itextpdf.kernel.pdf.PdfDocument in project i7js-highlevel by itext.
the class C03E15_MaryReillyV8 method createPdf.
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
document.add(p);
Image img = new Image(ImageDataFactory.create(MARY));
img.setHorizontalAlignment(HorizontalAlignment.CENTER);
img.setWidth(UnitValue.createPercentValue(80));
document.add(img);
document.close();
}
use of com.itextpdf.kernel.pdf.PdfDocument in project i7js-highlevel by itext.
the class C03E19_PagedImages method createPdf.
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
Image img;
// Animated GIF
URL url1 = UrlUtil.toURL(TEST1);
List<ImageData> list = ImageDataFactory.createGifFrames(url1);
for (ImageData data : list) {
img = new Image(data);
document.add(img);
}
// JBIG2
URL url2 = UrlUtil.toURL(TEST2);
IRandomAccessSource ras2 = new RandomAccessSourceFactory().createSource(url2);
RandomAccessFileOrArray raf2 = new RandomAccessFileOrArray(ras2);
int pages2 = Jbig2ImageData.getNumberOfPages(raf2);
for (int i = 1; i <= pages2; i++) {
img = new Image(ImageDataFactory.createJbig2(url2, i));
document.add(img);
}
// TIFF
URL url3 = UrlUtil.toURL(TEST3);
IRandomAccessSource ras3 = new RandomAccessSourceFactory().createSource(url3);
RandomAccessFileOrArray raf3 = new RandomAccessFileOrArray(ras3);
int pages3 = TiffImageData.getNumberOfPages(raf3);
for (int i = 1; i <= pages3; i++) {
img = new Image(ImageDataFactory.createTiff(url3, true, i, true));
document.add(img);
}
document.close();
}
use of com.itextpdf.kernel.pdf.PdfDocument in project i7js-highlevel by itext.
the class C04E01_DivExample1 method createPdf.
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
resultSet.remove(0);
for (List<String> record : resultSet) {
Div div = new Div().setBorderLeft(new SolidBorder(2)).setPaddingLeft(3).setMarginBottom(10);
String url = String.format("http://www.imdb.com/title/tt%s", record.get(0));
Link movie = new Link(record.get(2), PdfAction.createURI(url));
div.add(new Paragraph(movie.setFontSize(14))).add(new Paragraph(String.format("Directed by %s (%s, %s)", record.get(3), record.get(4), record.get(1))));
File file = new File(String.format("src/main/resources/img/%s.jpg", record.get(0)));
if (file.exists()) {
Image img = new Image(ImageDataFactory.create(file.getPath()));
img.scaleToFit(10000, 120);
div.add(img);
}
document.add(div);
}
document.close();
}
Aggregations