iText是一个强大的PDF处理库,可以用于创建、读取和操作PDF文件。它支持PDF表单、加密和签署等操作,同时支持多种字体和编码。maven的中央仓库中的最新版本是5.X,且iText5不是完全免费的,但是基础能力是免费使用的,不过需要遵循AGPLv3协议。iText7是iText5的后继版本,相比之下有更好的性能和更全面的PDF支持。在实际开发中,可以根据需求选择不同的iText版本。
iText是一个用于在Java中创建和操作PDF文件的开源库。以下是iText的一些主要功能:
<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>
1.创建一个Document对象实例。
2.创建一个PdfWriter对象,并将其与Document对象关联,以便将文档写入硬盘。
3.打开Document对象,以便可以向其中添加内容。
4.向Document对象添加文本内容。这可以通过创建Paragraph对象并使用Document对象的add()方法来完成。
5.关闭Document对象,以完成PDF文档的创建。
@Test
public void test0() {try {Document document = new Document();Instance(document, new FileOutputStream("d:/test/hello.pdf"));document.open();Element element = new Paragraph("hello, baby!");document.add(element);document.close();} catch (DocumentException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();}
}
与添加文本内容的过程比较类似,唯一不同的是,这里添加到文档中的是图片。在实际的业务开发过程中,如果默认使用图片原始的心都,可能会导致在文档中的图片过大或过小,都不好看,最好根据页面的宽度进行一定比例的自适应,这里介绍一种方法:
1.先获取图片的原始宽度和高度
2.再获取PDF页面的宽度和高度
3.然后根据页面宽度计算图片的缩放比例
4.最后根据缩放比例计算图片的新宽度和高度,并重新设置图片的宽度和高度;
@Test
public void test2() {try {Document document = new Document();PdfWriter pdfWriter = Instance(document, new FileOutputStream("d:/test/hello2.pdf"));document.open();Image image = Instance("d:/test/dog.jpg");// 获取图片的原始宽度和高度float originalWidth = Width();float originalHeight = Height();// 获取PDF页面的宽度和高度Rectangle pageSize = PageSize();float pageWidth = Width();// 根据页面宽度计算图片的缩放比例float scaleRatio = pageWidth / originalWidth;// 根据缩放比例计算图片的新宽度和高度float newWidth = originalWidth * scaleRatio;float newHeight = originalHeight * scaleRatio;// 设置图片的新宽度和高度,并保持纵横比不变image.scaleToFit(newWidth, newHeight);image.setAlignment(Element.ALIGN_CENTER);document.add(image);document.close();} catch (DocumentException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
}
1.创建PDF文档对象。使用Document类创建一个新的PDF文档对象,并打开它。
2.创建表格对象。使用PdfPTable类创建一个新的表格对象,并设置表格的列数和其他属性。
3.向表格中添加内容。使用PdfPCell类创建单元格对象,并将它们添加到表格中。可以设置单元格的文本、样式和布局等属性。这里需要注意的是:在iText中并没有行的概念,设置完表格的列数后,就是从左至右开始添加单元格内的内容,一般第一行是表头。
4.将表格添加到文档中。使用Document类的add()方法将表格添加到文档中。
5.关闭文档。使用Document类的close()方法关闭文档,并保存到指定的文件路径。
@Test
public void test3() {try {Document document = new Document();Instance(document, new FileOutputStream("d:/test/hello3.pdf"));document.open();// 创建PdfPTable对象并设置列宽度和间距PdfPTable table = new PdfPTable(3);float[] columnWidths = {1f, 1f, 1f};table.setWidths(columnWidths);table.setSpacingAfter(10f);table.setWidthPercentage(100); // 设置表格宽度占页面宽度的百分比为100%// 添加表头行PdfPCell headerCell1 = new PdfPCell(new Paragraph("realName"));headerCell1.setBackgroundColor(BaseColor.LIGHT_GRAY);table.addCell(headerCell1);PdfPCell headerCell2 = new PdfPCell(new Paragraph("age"));headerCell2.setBackgroundColor(BaseColor.LIGHT_GRAY);table.addCell(headerCell2);PdfPCell headerCell3 = new PdfPCell(new Paragraph("sex"));headerCell3.setBackgroundColor(BaseColor.LIGHT_GRAY);table.addCell(headerCell3);// 添加数据行table.addCell("zhangsan");table.addCell("18");table.addCell("boy");// 将表格添加到文档中document.add(table);// 关闭文档对象document.close();} catch (Exception e) {e.printStackTrace();}
}
1.创建一个PDF读取器对象(PdfReader):
2.创建一个PdfTextExtractor对象:
3.逐页提取文本:
4.关闭PDF读取器:
@Test
public void test6() {try {PdfReader pdfReader = new PdfReader(new FileInputStream("d:/test/hello5.pdf"));int numberOfPages = NumberOfPages();for (int i = 0; i < numberOfPages; i++) {String textFromPage = TextFromPage(pdfReader, i + 1);System.out.println(textFromPage);}} catch (Exception e) {e.printStackTrace();}
}
下一篇:掌握iText:轻松处理PDF文档-进阶篇
本文发布于:2024-01-29 08:11:53,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170648711513900.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |