学习地址https://www.yiibai.com/pdfbox/pdfbox_overview.html
官网下载jarhttps://pdfbox.apache.org/
或使用maven
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jempbox</artifactId>
<version>1.8.11</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>xmpbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>preflight</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
创建空的PDF文档
PDDocument document = new PDDocument();
进行保存
document.save("Path");
关闭文档
document.close();
添加页面到PDF文档
PDPage my_page = new PDPage();
document.addPage(my_page);
加载现有的PDF文档
File file = new File("path of the document")
PDDocument.load(file);
从现有文档中删除页面,传递页面索引
document.removePage(2); // 删除第三页
获取总页数
document.getNumberOfPages();
文档的属性
编号 属性 描述
1 File 该属性保存文件的名称。
2 Title 使用此属性,可以设置文档的标题。
3 Author 使用此属性,可以设置文档的作者姓名。
4 Subject 使用此属性,可以指定PDF文档的主题。
5 Keywords 使用此属性,列出可以搜索文档的关键字。
6 Created 使用此属性,可以设置为文档修改的日期
7 Application 使用此属性,可以设置文档的应用程序。
设置文档的属性
PDDocumentInformation pdd = document.getDocumentInformation();
pdd.setAuthor("Yiibai.com");
// Setting the title of the document
pdd.setTitle("一个简单的文档标题");
//Setting the creator of the document
pdd.setCreator("PDF Examples");
//Setting the subject of the document
pdd.setSubject("文档标题");
//Setting the created date of the document
Calendar date = new GregorianCalendar();
date.set(2017, 11, 5);
pdd.setCreationDate(date);
//Setting the modified date of the document
date.set(2018, 10, 5);
pdd.setModificationDate(date);
//Setting keywords for the document
pdd.setKeywords("pdfbox, first example, my pdf");
将文本添加到现有的PDF文档
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.beginText();
// 其它代码 .....
code to add text content
contentStream.setFont( font_type, font_size );
// 其它代码 .....
contentStream.newLineAtOffset(25, 700);
contentStream.showText(text);
contentStream.endText();
插入多行文本
contentStream.setLeading(14.5f);
contentStream.newLine();
提取文本
File file = new File("F:\\worksp\\pdfbox\\new-mul-doc.pdf");
PDDocument document = PDDocument.load(file);
//Instantiate PDFTextStripper class
PDFTextStripper pdfStripper = new PDFTextStripper();
//Retrieving text from PDF document
String text = pdfStripper.getText(document);
System.out.println(text);
//Closing the document
document.close();
插入图像
File file = new File("F:/worksp/pdfbox/sample.pdf");
PDDocument doc = PDDocument.load(file);
//Retrieving the page
PDPage page = doc.getPage(0);
//Creating PDImageXObject object
PDImageXObject pdImage = PDImageXObject.createFromFile("F:/worksp/pdfbox/logo.png",doc);
//creating the PDPageContentStream object
PDPageContentStream contents = new PDPageContentStream(doc, page);
//Drawing the image in the PDF document
contents.drawImage(pdImage, 70, 250);
System.out.println("Image inserted");
//Closing the PDPageContentStream object
contents.close();
//Saving the document
doc.save("F:/worksp/pdfbox/sample-image.pdf");
//Closing the document
doc.close();
其他待整理,包括加密pdf文档,分隔合并pdf文档,提取图像,添加矩形等。