Java實現(xiàn)合并多個PDF的示例代碼
這里合并用到了一個itext的包。使用maven直接導入依賴即可。
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
這個是我寫的一個utl工具類,里面還寫了一個main方法,如果你有兩個pdf,可以直接用main方法跑一下。
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import java.io.FileOutputStream;
public class PdfUtil {
/**
* 合并pdf
* @param files 需要合并的pdf路徑
* @param newfile 合并成新的文件的路徑
* @return
*/
public static boolean mergePdfFiles(String[] files, String newfile) {
boolean retValue = false;
Document document = null;
PdfCopy copy = null;
PdfReader reader = null;
try {
document = new Document(new PdfReader(files[0]).getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(newfile));
document.open();
for (int i = 0; i < files.length; i++) {
reader = new PdfReader(files[i]);
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
retValue = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (copy != null) {
copy.close();
}
if (document != null) {
document.close();
}
}
return retValue;
}
public static void main(String[] args) {
String[] files = { "D:\\Case\\0000001\\00001\\ABIStatistic.pdf", "D:\\Case\\0000001\\00001\\ABITable.pdf",
"D:\\Case\\0000001\\00001\\CVRR.pdf" };
String savepath = "D:\\Case\\0000001\\00001\\temp.pdf";
boolean b = mergePdfFiles(files, savepath);
System.out.println(b);
}
}
補充
通過java還能實現(xiàn)pdf的拆分
1.按每頁單獨拆分
import com.spire.pdf.*;
public class SplitPDF1 {
public static void main(String[] args)
{
//加載需要拆分的PDF文檔
PdfDocument doc = new PdfDocument();
doc.loadFromFile("test.pdf");
//調用方法split()將PDF文檔按每一頁拆分為單獨的文檔
doc.split("output/splitDocument-{0}.pdf", 0);
doc.close();
}
}2.按指定頁數(shù)范圍拆分
import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfMargins;
import java.awt.geom.Point2D;
public class SplitPDF2 {
public static void main(String[] args)
{
//加載需要拆分的PDF文檔
PdfDocument doc = new PdfDocument();
doc.loadFromFile("test.pdf");
//新建第1個PDF文檔1
PdfDocument newpdf1 = new PdfDocument();
PdfPageBase page;
//將原PDF文檔的第1、2頁拆分,并保存到newpdf1
for(int i = 0;i<2;i++)
{
page = newpdf1.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
}
newpdf1.saveToFile("split/result1.pdf");
//新建第2個PDF文檔
PdfDocument newpdf2 = new PdfDocument();
//將原PDF文檔的第3、4頁拆分,并保存到newpdf2
for(int i = 2;i<4;i++)
{
page = newpdf2.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
}
newpdf2.saveToFile("split/result2.pdf");
}
} 到此這篇關于Java實現(xiàn)合并多個PDF的示例代碼的文章就介紹到這了,更多相關Java合并多個PDF內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
快速解決SpringMVC @RequestBody 用map接收請求參數(shù)的問題
今天小編就為大家分享快速解決SpringMVC @RequestBody 用map接收請求參數(shù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Spring Task定時任務每天零點執(zhí)行一次的操作
這篇文章主要介紹了Spring Task定時任務每天零點執(zhí)行一次的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java基本數(shù)據(jù)類型與封裝類型詳解(int和Integer區(qū)別)
這篇文章主要介紹了Java基本數(shù)據(jù)類型與封裝類型詳解(int和Integer區(qū)別) ,需要的朋友可以參考下2017-02-02
Spring 中的 @PathVariable 注解及應用場景分析
@PathVariable注解是 Spring 框架中一個非常實用的注解,它可以幫助我們輕松地從 URL 中提取參數(shù),從而實現(xiàn) RESTful API 的開發(fā),通過本文的介紹,我們了解了@PathVariable注解的基本使用方法和高級用法,以及它的應用場景,感興趣的朋友跟隨小編一起看看吧2025-05-05
淺析SpringBoot中使用thymeleaf找不到.HTML文件的原因
這篇文章主要介紹了SpringBoot中使用thymeleaf找不到.HTML文件的原因分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Java生成日期時間存入Mysql數(shù)據(jù)庫的實現(xiàn)方法
本文主要介紹了Java生成日期時間存入Mysql數(shù)據(jù)庫的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

