java合并PDF,itext.jar
2024-10-20 00:48:53
远原文:http://illy.iteye.com/blog/856479
我们有时候需要将多个PDF文件合并成一个.GUI工具的方式就不说了,
这里只讨论使用java程式如何合并PDF.我们只需要使用到itext.jar就可以完成任务.
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader; public class MergeFile {
public static void main(String[] args) {
String[] files = { "e:\\1.pdf", "e:\\2.pdf", "e:\\3.pdf" };
String savepath = "e:\\temp.pdf";
mergePdfFiles(files, savepath);
} /*
* * 合並pdf文件 * * @param files 要合並文件數組(絕對路徑如{ "e:\\1.pdf", "e:\\2.pdf" ,
* "e:\\3.pdf"}) * @param newfile
* 合並後新產生的文件絕對路徑如e:\\temp.pdf,請自己刪除用過後不再用的文件請 * @return boolean
* 產生成功返回true, 否則返回false
*/ public static boolean mergePdfFiles(String[] files, String newfile) {
boolean retValue = false;
Document document = null;
try {
document = new Document(new PdfReader(files[0]).getPageSize(1));
PdfCopy copy = new PdfCopy(document, new FileOutputStream(newfile));
document.open();
for (int i = 0; i < files.length; i++) {
PdfReader 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 {
document.close();
}
return retValue;
}
}
最新文章
- docker 1.8+之后ubuntu安装指定版本docker-engine
- Bootstrap整体架构
- Ajax请求SpringMVC
- Cannot Change Opencv Webcam Setting
- 二模 06day2
- FastDFS之java客户端使用
- 提问:";~";运算符
- 《如何将windows上的软件包或文件上传到linux服务上》
- java实现合并两个已经排序的列表
- iOS方法类:CGAffineTransform的使用
- K:二叉树
- python入门(4)第一个python程序
- Python爬虫入门教程 63-100 Python字体反爬之一,没办法,这个必须写,反爬第3篇
- Android APK 签名文件MANIFEST.MF、CERT.SF、CERT.RSA分析
- WC.exe【C】
- [原创]标记下Kendo使用中的问题, 持续更新中ing.....
- spring的摘录
- UC浏览器中Ajax请求中传递数据的一个坑
- Nutch1.7学习笔记:基本环境搭建及使用
- hibernate连接oracle12c数据库报:java.sql.SQLException: ORA-01017: 用户名/口令无效; 登录被拒绝。(用户名/口令在oracle客户端以及cmd命令都能登入)
热门文章
- 一个带重试次数的curl 函数
- IP地址及子网--四种IP广播地址
- BeanUtils封装对象时一直提示ClassNotFoundException:org.apache.commons.beanutils.BeanUtils
- Codeforces 351D Jeff and Removing Periods(莫队+区间等差数列更新)
- Perl 连接Oracle 出现OCI missing的问题及解决
- python基础(9)--递归、二叉算法、多维数组、正则表达式
- Java学习(运算符,引用数据类型)
- Git &; GitHub 学习
- list 往指定的下标插入元素
- 查找无序数组中第K大的数