spring boot itext7的生成一个pdf(hello,world),并且相关一些简单的使用方法及相关说明

1、我们经常会碰到生成Pdf的场景,比如说有很多题目,又比如说来个质检的报告,我们都需要导出为pdf,那这种情况有二种方法,一种是通过报表来实现,一种就是通过itext来生成。一般我们会通过报表来直接导出pdf。

2、下面我们通过itext7来生成一个pdf:

复制代码
<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext7-core</artifactId>
			<version>7.2.5</version>
			<type>pom</type>
		</dependency>

3、创建表:

复制代码
float[] colWidths = new float[] { 50, 50 };

			Table table = new Table(UnitValue.createPercentArray(colWidths), true);
			doc.add(table);

4、使用canvas

复制代码
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle[] columns = { new Rectangle(30, 130, 15, 100),
new Rectangle(60, 130, 15, 100), }; // 几个Rectangle对应几个位置
String[] textList = { "第一行", "第二行"};
for (int i = 0; i < columns.length; i++) {
				pdfCanvas.rectangle(columns[i]);
				pdfCanvas.stroke();
				Canvas canvas = new Canvas(pdfCanvas, columns[i]);
				Paragraph p = new Paragraph(textList[i]).setFont(font).setBold().setFontSize(8)
						.setVerticalAlignment(VerticalAlignment.TOP).setHorizontalAlignment(HorizontalAlignment.CENTER)
						.setTextAlignment(TextAlignment.CENTER); // Bold为设置粗体
				p.setBorder(Border.NO_BORDER);
				canvas.add(p);
				canvas.flush();
				canvas.close();
			}

5、背景图的使用及图片的创建:

复制代码
File bgFile = ResourceUtils.getFile("classpath:img/bg.png");
			PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.create(bgFile.getAbsolutePath()));
		
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.create(bgFile.getAbsolutePath())).put(
					PdfName.BBox, new PdfArray(new Rectangle(0, 0, rect.getWidth() / 2, rect.getHeight())));
			BackgroundImage backgroundImage = new BackgroundImage.Builder().setImage(xObject)
					.setBackgroundRepeat(new BackgroundRepeat(BackgroundRepeatValue.NO_REPEAT)).build();


//图片的创建
			Image image = new Image(ImageDataFactory.create(bgFile.getAbsolutePath()));	

图片的目录结构见下面:

6、生成一个helloworld

复制代码
@SuppressWarnings("resource")
	@RequestMapping(path = "/test", method = RequestMethod.GET)
	public void TestHello(HttpServletRequest req) {
		String rootPath = req.getSession().getServletContext().getRealPath("/");
		File logDir = new File(rootPath);
		if (!logDir.exists()) {
			logDir.mkdirs();
		}
		String outFileName = rootPath + "test.pdf";
		System.out.println(outFileName);
		PdfDocument pdfDocument;
		try {
			pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)));
			new Document(pdfDocument).add(new Paragraph("hello,world!"));
			pdfDocument.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

7、简要的说明:

从以上的概念我们就知道了,为什么还要new Document(pdfDocument),只有这样才可以来使用API,进行一些操作,比如增加Table,Image等。

8、还有一点比较特别的:

x,y,width,height,这里面的y是跟html中css不一样的。要从下面bottom开始。

9、官方example: Examples

相关推荐
工业3D_大熊9 天前
3D模式格式转换工具HOOPS Exchange如何将3D PDF转换为STEP格式?
3d·pdf·3d格式转换·3d模型格式转换·cad格式转换·cad数据格式转换·3d模型可视化
IDRSolutions_CN10 天前
在 Java 中生成 PDF 缩略图(教程)
java·经验分享·pdf·软件工程·团队开发
IDRSolutions_CN10 天前
用Java将PDF转换成GIF
java·经验分享·pdf·软件工程·团队开发
贤和兄10 天前
使用docx4j 实现word转pdf(linux乱码处理)
linux·pdf·word
Eiceblue10 天前
高效打印 PDF 文档:基础操作与自动打印(含C# .NET方案)
pdf·c#·.net
沉到海底去吧Go10 天前
【工具教程】PDF指定区域OCR识别重命名工具使用教程和注意事项
pdf·ocr·图片区域识别改名·仓储物流单据识别·物流单据识别改名·pdf区域识别改名·pdf区域识别重命名
开开心心就好11 天前
高效批量转换Word到PDF的方法
javascript·安全·智能手机·pdf·word·objective-c·lisp
response_L11 天前
麒麟v10、uos系统在线批量生成pdf文件
java·pdf·word·pageoffice·在线编辑
SEO-狼术11 天前
Easily Fill Out PDF Forms Crack
pdf
拓端研究室11 天前
专题:2025游戏科技与市场趋势报告|附130+份报告PDF汇总下载
科技·游戏·pdf