远原文: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;
}
}

iTextAsian.jar   itext-1.3.jar

最新文章

  1. docker 1.8+之后ubuntu安装指定版本docker-engine
  2. Bootstrap整体架构
  3. Ajax请求SpringMVC
  4. Cannot Change Opencv Webcam Setting
  5. 二模 06day2
  6. FastDFS之java客户端使用
  7. 提问:&quot;~&quot;运算符
  8. 《如何将windows上的软件包或文件上传到linux服务上》
  9. java实现合并两个已经排序的列表
  10. iOS方法类:CGAffineTransform的使用
  11. K:二叉树
  12. python入门(4)第一个python程序
  13. Python爬虫入门教程 63-100 Python字体反爬之一,没办法,这个必须写,反爬第3篇
  14. Android APK 签名文件MANIFEST.MF、CERT.SF、CERT.RSA分析
  15. WC.exe【C】
  16. [原创]标记下Kendo使用中的问题, 持续更新中ing.....
  17. spring的摘录
  18. UC浏览器中Ajax请求中传递数据的一个坑
  19. Nutch1.7学习笔记:基本环境搭建及使用
  20. hibernate连接oracle12c数据库报:java.sql.SQLException: ORA-01017: 用户名/口令无效; 登录被拒绝。(用户名/口令在oracle客户端以及cmd命令都能登入)

热门文章

  1. 一个带重试次数的curl 函数
  2. IP地址及子网--四种IP广播地址
  3. BeanUtils封装对象时一直提示ClassNotFoundException:org.apache.commons.beanutils.BeanUtils
  4. Codeforces 351D Jeff and Removing Periods(莫队+区间等差数列更新)
  5. Perl 连接Oracle 出现OCI missing的问题及解决
  6. python基础(9)--递归、二叉算法、多维数组、正则表达式
  7. Java学习(运算符,引用数据类型)
  8. Git &amp; GitHub 学习
  9. list 往指定的下标插入元素
  10. 查找无序数组中第K大的数