引入依赖:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
package com.dc.esb.pdf;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* @auther zhanggt
*/
public class CreatePdf {
public static void main(String[] args) throws Exception {
System.out.println("------------------------开始写入pdf内容---------------------------");
Document document = new Document(PageSize.A4);
Font font = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, 12, Font.NORMAL);
PdfWriter.getInstance(document, new FileOutputStream("D:\\pdf\\111.pdf"));
document.open();
Paragraph paragraph = new Paragraph("hello pdf*****我来啦****", font);
document.add(paragraph);
System.out.println("------------------------写入汉字结束---------------------------");
//加入图片
Image image = Image.getInstance("D:\\pdf\\200512112022-1-1200.jpg");
float width = image.getWidth();
float height = image.getHeight();
//获取pdf的高度
Rectangle pageSize = document.getPageSize();
float sizeWidth = pageSize.getWidth();
//计算缩放比例
float scale = sizeWidth / 0.3f;
float newwidth = width * 0.3f;
float newheight = height * scale;
image.scaleToFit(newwidth, newheight);
image.setAlignment(Element.ALIGN_CENTER);
document.add(image);
System.out.println("------------------------写入图片结束---------------------------");
//增加表格
PdfPTable table = new PdfPTable(3);
float[] cloumnWidths = {1f, 1f, 1f};
table.setWidths(cloumnWidths);
table.setSpacingAfter(10f);
table.setWidthPercentage(100);
//添加表头
PdfPCell name = new PdfPCell(new Paragraph("name"));
name.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(name);
PdfPCell age = new PdfPCell(new Paragraph("age"));
age.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(age);
PdfPCell sex = new PdfPCell(new Paragraph("sex"));
sex.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(sex);
//添加数据
table.addCell(new Paragraph("张三",font));
table.addCell("18");
table.addCell("boy");
document.add(table);
document.close();
System.out.println("------------------------写入表格结束---------------------------");
System.out.println("------------------------开始读取pdf内容---------------------------");
PdfReader reader = new PdfReader(new FileInputStream(new File("D:\\pdf\\111.pdf")));
for (int i = 0; i < reader.getNumberOfPages(); i++) {
System.out.println(PdfTextExtractor.getTextFromPage(reader,i+1));
}
}
}
生成的pdf:
读取结果:
------------------------开始写入pdf内容---------------------------
------------------------写入汉字结束---------------------------
------------------------写入图片结束---------------------------
------------------------写入表格结束---------------------------
------------------------开始读取pdf内容---------------------------
hello pdf*****我来啦****
name age sex
张三 18 boy