将一个PDF分割成多个子文件的改进版

package com.peter.mytool;

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

public class PDFTool {

	public static void main(String[] args) {
		String fileName = "I:\\iOS电子版资料\\IOS5.pdf";
		int[] pages ={1,31,47,65,97,139,153,179,193,231,251,
				283,299,325,347,369,389,423,445,457,479,517,541};
		String titles[] = new String[]{
				"00-Introduction",
				"01-Getting Started with iOS 5 Programming",
				"02-Writing Your First Hello World! Application",
				"03-Understanding Views, Outlets,and Actions",
				"04-Exploring the Different View Controllers",
				"05-Enabling Multi-Platform Support for the iPhone and iPad",
				"06-Handling Keyboard Inputs",
				"07-Supporting Screen Rotations",
				"08-Creating and Persisting Data Using the Table View",
				"09-Using Application Preferences",
				"10-File Handling",
				"11-Database Storage Using SQLite",
				"12-Programming iCloud",
				"13-Performing Simple Animations and Video Playback",
				"14-Accessing Built-In Applications",
				"15-Accessing the Sensors",
				"16-Using Web Services",
				"17-Bluetooth Programming",
				"18-Bonjour Programming",
				"19-Programming Remote Notifications Using Apple Push Notification Services",
				"20-Displaying Maps",
				"21-Programming Background Applications",
				"22-Appendix"				
		};
		//splitPDFIntoSubfiles(fileName, pages);
		splitPDFIntoSubfiles(fileName, pages,titles);
	}
	
	/**
	 * 把pdf文件按照指定的页码进行分页
	 * @param pdfFile 指定dpf文件
	 * @param pages 指定页码
	 * @param titles 指定标题
	 */
	public static void splitPDFIntoSubfiles(String pdfFile,int[] pages, String titles[]) {		
		try {
			PdfReader reader = new PdfReader(pdfFile);			
			int totalPages = reader.getNumberOfPages();			
			String staticpath = pdfFile.substring(0, pdfFile.lastIndexOf("\\")+1);
			String subfileName = pdfFile.substring(pdfFile.lastIndexOf("\\")+1) ;
			subfileName = subfileName.substring(0, subfileName.lastIndexOf("."))+"-C";
			String savepath = "";
			int end = 0;
			for(int i=0; i<pages.length; i++){
				if(i == pages.length-1){
					end = totalPages;
				}else{
					end = pages[i+1]-1;
				}				
				savepath = staticpath + subfileName + titles[i] +".pdf";
				createNewPDFFile(reader,pages[i],end,savepath);
			}
			System.out.println(" splitPDFIntoSubfiles is done..... ");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 把pdf文件按照指定的页码进行分页
	 * @param pdfFile 指定dpf文件
	 * @param pages 指定页码
	 */
	public static void splitPDFIntoSubfiles(String pdfFile,int[] pages) {		
		try {
			PdfReader reader = new PdfReader(pdfFile);			
			int totalPages = reader.getNumberOfPages();			
			String staticpath = pdfFile.substring(0, pdfFile.lastIndexOf("\\")+1);
			String subfileName = pdfFile.substring(pdfFile.lastIndexOf("\\")+1) ;
			subfileName = subfileName.substring(0, subfileName.lastIndexOf("."))+"-Chapter";
			String savepath = "";
			int end = 0;
			String ChapterNO = "";
			for(int i=0; i<pages.length; i++){
				if(i == pages.length-1){
					end = totalPages;
				}else{
					end = pages[i+1]-1;
				}
				if(i<10){
					ChapterNO = "0";
				}
				savepath = staticpath + subfileName + ChapterNO +i+".pdf";
				createNewPDFFile(reader,pages[i],end,savepath);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 从PdfReader的from页到end页copy一份,生成一份指定文件目录的文件savepath
	 * @param reader PdfReader 读取的源文件
	 * @param from 开始页码
	 * @param end 结束页面
	 * @param savepath 保存新的路径
	 * @throws Exception
	 */
	private static void createNewPDFFile(PdfReader reader, int from , int end,String savepath) throws Exception{
		Document document = null;
		PdfCopy copy = null;
		document = new Document(reader.getPageSize(1));
		copy = new PdfCopy(document, new FileOutputStream(savepath));
		document.open();
		for(int j=from; j<=end; j++) {
			document.newPage(); 
			PdfImportedPage page = copy.getImportedPage(reader, j);
			copy.addPage(page);
		}
		document.close();
	}
	

}

 直接上程序,不加解释。因为很简单,可以直接看得懂。

 至于所使用的jar见我之前的博文

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值