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

相关推荐
Geekersoft5 分钟前
Windows 10 磁盘管理恢复 - 如何恢复Windows 10文件
windows·经验分享·pdf·编辑器·电脑·笔记本电脑·社交电子
合合技术团队1 小时前
OCR+PDF解析配套前端工具开源详解!
前端·深度学习·pdf·ocr
棱境2 小时前
一文解决jQuery表格插件DataTable导出PDF中文乱码问题
pdf·jquery·datatable
GEEKVIP7 小时前
选择 PDF 编辑器时要考虑什么?如何选择适用于 Windows 10 的 PDF 编辑器
网络·windows·ios·智能手机·pdf·编辑器·iphone
SEO-狼术8 小时前
Automate PowerPoint to PDF Conversion in .NET
pdf·powerpoint
惠惠软件20 小时前
CleverPDF是一款专业的pdf转换器-强大的PDF表格识别能够将PDF中的表格提取到Excel或者其他格式-供大家学习研究参考
学习·pdf·excel·pdf工具包·pdf超级工具
iQM751 天前
Python自动化办公:批量提取PDF中的表格到Excel
pdf·自动化·excel
AI大模型_学习君1 天前
实验 | 使用本地大模型从论文PDF中提取结构化信息
人工智能·深度学习·机器学习·pdf·llm·ai大模型·计算机技术
2401_856652211 天前
详解 PDF 转 JPG:简单操作,高效转换
图像处理·学习·pdf·电脑·娱乐