正经学徒,佛系记录,不搞事情
itextpdf 是java用于操作pdf文件的工具,速度极快,目前测试上千页的pdf文件都不在话下
下面编写的工具类,可看作是将pdf文件转成类似list的集合,通过对list的CURD操作,最终生成一个新的list集合,即生成一个新的pdf文件
提供的方法:
- pdf追加pdf
- pdf插入pdf 指定序列号下
- 删除pdf某页
- 交换pdf指定的两页
第一步:添加 pom 依赖
<!--添加操作pdf依赖-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
第二步:创建工具类 PDFUtil.java
第三步:定义最终生成的pdf文件
这里定义一个temp.pdf文件,无需创建可直接生成,实际项目中,可使用时间戳+业务规则作为最终生成的文件名。
public static String PDF_PATH = "D:\\pdf\\temp.pdf";
第四步:编写工具类
/**
* @description 追加pdf 在headReader后面追加tailReader
* @param headReader PdfReader
* @param tailReader PdfReader
* @return out
*/
public static FileOutputStream pdfFollowPdf(PdfReader headReader, PdfReader tailReader){
//创建文档
Document document = new Document();
PdfWriter writer;
FileOutputStream out = null;
try {
//最终文件输出
out = new FileOutputStream(PDF_PATH);
writer = PdfWriter.getInstance(document, out);
//打开文档
document.open();
PdfContentByte cb = writer.getDirectContent();
int currentPage=1;
while(currentPage<=headReader.getNumberOfPages()){
document.newPage();
//获取某一页
PdfImportedPage page = writer.getImportedPage(headReader, currentPage);
cb.addTemplate(page, 0, 0);
currentPage++;
}
currentPage=1;
while(currentPage<=tailReader.getNumberOfPages()){
document.newPage();
//获取某一页
PdfImportedPage page = writer.getImportedPage(tailReader, currentPage);
cb.addTemplate(page, 0, 0);
currentPage++;
}
document.close();
out.flush();
out.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return out;
}
/**
* @description 插入pdf 指定序列号下
* @param headReader PdfReader
* @param tailReader PdfReader
* @param int insertPage
* @return out FileOutputStream
*/
public static FileOutputStream pdfIntoPdf(PdfReader headReader,PdfReader tailReader,int insertPage){
if(insertPage>headReader.getNumberOfPages()+1){
System.out.println("需插入的页号超过总页数");
return null;
}
FileOutputStream out = null;
Document document = new Document();
PdfWriter writer;
//最终文件输出
try {
out = new FileOutputStream(PDF_PATH);
writer = PdfWriter.getInstance(document, out);
document.open();
PdfContentByte cb = writer.getDirectContent();
int headCurrentPage=1;
int tailCurrentPage=1;
int currentPage=1;
while(currentPage<=headReader.getNumberOfPages()+tailReader.getNumberOfPages()){
document.newPage();
if(currentPage==insertPage){
while(tailCurrentPage<=tailReader.getNumberOfPages()){
//获取某一页
PdfImportedPage page = writer.getImportedPage(tailReader, tailCurrentPage);
cb.addTemplate(page, 0, 0);
tailCurrentPage++;
currentPage++;
}
}else{
//获取某一页
PdfImportedPage page = writer.getImportedPage(headReader, headCurrentPage);
cb.addTemplate(page, 0, 0);
headCurrentPage++;
currentPage++;
}
}
document.close();
out.flush();
out.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return out;
}
/**
* @description 删除指定页的pdf
* @param headReader PdfReader
* @param deletePage int
* @return out FileOutputStream
*/
public static FileOutputStream delPdfByIndex(PdfReader headReader,int deletePage){
if(deletePage>headReader.getNumberOfPages()){
System.out.println("需删除的页号超过总页数");
return null;
}
//处理需要的页数
StringBuffer showPages=new StringBuffer("");
for(int i=1;i<=headReader.getNumberOfPages();i++){
if(i!=deletePage){
showPages.append(",").append(i);
}
}
//去除第一个逗号
showPages.delete(0, 1);
FileOutputStream out = null;
try {
out = new FileOutputStream(PDF_PATH);
headReader.selectPages(showPages.toString());
PdfStamper stamp;
stamp = new PdfStamper(headReader,out);
stamp.close();
out.flush();
out.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return out;
}
/**
* @description 交换pdf(headIndex页交换tailIndex页)
* @param headReader PdfReader
* @param headIndex int
* @param tailIndex int
* @return out FileOutputStream
*/
public static FileOutputStream pdfChangePage(PdfReader headReader,int headIndex,int tailIndex){
if(headIndex>headReader.getNumberOfPages()||tailIndex>headReader.getNumberOfPages()){
System.out.println("需交换的页号超过总页数");
return null;
}
Document document = new Document();
PdfWriter writer;
FileOutputStream out = null;
try {
out = new FileOutputStream(PDF_PATH);
writer = PdfWriter.getInstance(document, out);
document.open();
PdfContentByte cb = writer.getDirectContent();
int currentPage=1;
while(currentPage<=headReader.getNumberOfPages()){
document.newPage();
if(currentPage==headIndex){
//获取某一页
PdfImportedPage page = writer.getImportedPage(headReader, tailIndex);
cb.addTemplate(page, 0, 0);
currentPage++;
}else if(currentPage==tailIndex){
//获取某一页
PdfImportedPage page = writer.getImportedPage(headReader, headIndex);
cb.addTemplate(page, 0, 0);
currentPage++;
}else{
//获取某一页
PdfImportedPage page = writer.getImportedPage(headReader, currentPage);
cb.addTemplate(page, 0, 0);
currentPage++;
}
}
document.close();
out.flush();
out.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return out;
}
第五步:测试
public static void main(String[] args) {
InputStream headStream = null;
InputStream tailStream = null;
PdfReader headReader;
PdfReader tailReader;
try {
headStream = new FileInputStream("D:\\pdf\\Spring Cloud微服务实战.pdf");
tailStream = new FileInputStream("D:\\pdf\\test1.pdf");
headReader = new PdfReader(headStream);
tailReader = new PdfReader(tailStream);
//PDFUtil.pdfFollowPdf(headReader,tailReader);
//PDFUtil.pdfIntoPdf(headReader,tailReader,2);
//PDFUtil.delPdfByIndex(headReader,2);
//PDFUtil.pdfChangePage(headReader,1,2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
工具类地址:
https://pan.baidu.com/s/1_7rApHY5dxX044SDadQUGQ 提取码: tgg3