PDF拆分与合并

在进行开发的时候,需要在每个页面进行盖章

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import uk.ltd.getahead.dwr.util.Logger;

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 PdfOperateUtil {
	private static Logger log = Logger.getLogger(PdfOperateUtil.class);

	public static void main(String[] args) throws Exception {
		List<String>  list = splitPdfFile("D:\\report\\GISS\\20150708113914663_LIST_133.pdf");
		for(String fileName : list){
			System.out.println(fileName);
		}
		//合并
		mergePdfFiles(list,"D:\\report\\GISS\\20150708113914663_LIST_gaoweigang.pdf");
	}

	/**
	 * PDF合并
	 * @param files
	 * @param savepath
	 */
	public static void mergePdfFiles(List<String> files, String savepath) {
		try {
			Document document = new Document(new PdfReader(files.get(0)).getPageSize(1));
			PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));
			document.open();

			for (int i = 0; i < files.size(); i++) {
				PdfReader reader = new PdfReader(files.get(i));

				int n = reader.getNumberOfPages();

				for (int j = 1; j <= n; j++) {
					document.newPage();
					PdfImportedPage page = copy.getImportedPage(reader, j);
					copy.addPage(page);
				}
			}

			document.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
    /**
     * PDF拆分
     * @param filepath
     */
	public static List<String> splitPdfFile(String filepath) throws Exception{
		log.info("进入splitPdfFile方法");
		List<String> fileFullNameList = new ArrayList<String>();//存放拆分之后的文件全路径
		Document document = null;
		PdfCopy copy = null;

		try {
			PdfReader reader = new PdfReader(filepath);

			int n = reader.getNumberOfPages();//获取源PDF共有多少页

			if (n == 1) {//如果只有一页
				fileFullNameList.add(filepath);
				return fileFullNameList;
			}
            //如果有多页PDF
			String remotePath = filepath.substring(0,filepath.lastIndexOf("\\") + 1);//临时文件
			log.info("remote path:"+remotePath);
			String splitfilename = null;//文件名
			String splitfilefullname = null;//文件全路径
			for (int i = 1; i <= n; i++) {
				splitfilename = filepath.substring(
						filepath.lastIndexOf("\\") + 1,filepath.length() - 4);
				splitfilefullname = remotePath + splitfilename + "_" + i + ".pdf";
				fileFullNameList.add(splitfilefullname);
			}
			
			
			for (int i = 0; i < n; i++) {
				//创建一个新的文档,文档大小与reader相同
				document = new Document(reader.getPageSize(1));
				copy = new PdfCopy(document, new FileOutputStream(
						fileFullNameList.get(i)));
				document.open();
				document.newPage();
				//从输入流 中抓取 指定页面
				PdfImportedPage page = copy.getImportedPage(reader, i+1);
				//将 指定页面 复制到 输出流
				copy.addPage(page);
				document.close();
			}
            log.info("拆分成:"+fileFullNameList.size()+"页");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		log.info("退出splitPdfFile方法");
		return fileFullNameList;
	}
	
}

猜你喜欢

转载自weigang-gao.iteye.com/blog/2225271