`
yanguz123
  • 浏览: 556493 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

PDF书籍拆分

 
阅读更多

PDF书籍拆分

 

 

 

 

 

 

按配置拆分

 

 

 

 

 

按书签拆分

 

 

 

package com.peter.utils;

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

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;
import com.lowagie.text.pdf.SimpleBookmark;

public class SpliterUtil {

	// 分页码
	private static List<Integer> pages = new ArrayList<Integer>();
	// 标题
	private static List<String> titles = new ArrayList<String>();
	private static PdfReader reader = null;
	// 输入pdf全路径文件名
	private static String pdfFile = null;
	// 生成目录
	private static String dir = null;
	// pdf总页码
	private static int count = 0;
	// 书签深度序号
	private static int bookmarkDeepIdx = 1;
	// 书签层级
	private static int bookmarkLevel = 1;

	public static void main(String[] args) throws Exception {
		pdfFile = "E:\\软考\\《Maven实战》完整高清版 - OK.pdf";
		bookmarkLevel = 1;
		dir = pdfFile.substring(0, pdfFile.lastIndexOf("."));
		// 创建输出目录
		File file = new File(dir);
		if (!file.exists()) {
			file.mkdirs();
		}
		reader = new PdfReader(pdfFile);
		count = reader.getNumberOfPages();
		// 获取书签
		List<Map<String, Object>> list = SimpleBookmark.getBookmark(reader);
		// 从书签中获取分页信息
		getPageInfo(list);
		pages.add(count);
		// 拆分
		for (int i = 0; i < titles.size(); i++) {
			pdfSplit(titles.get(i) + ".pdf", pages.get(i), pages.get(i + 1));
		}
		reader.close();
	}

	/**
	 * 初始化pdf分割信息
	 * 
	 * @param pdfFile
	 * @throws Exception
	 */
	public static void getPageInfo(List<Map<String, Object>> list) throws Exception {
		for (int i = 0, size = list.size(); i < size; i++) {
			Map<String, Object> bookmark = list.get(i);
			List<Map<String, Object>> kids = (List<Map<String, Object>>) bookmark.get("Kids");
			// 根据需要解析书签层级进行递归
			if (kids != null && bookmarkDeepIdx < bookmarkLevel) {
				bookmarkDeepIdx++;
				getPageInfo(kids);
				bookmarkDeepIdx--;
			} else {
				String page = String.valueOf(bookmark.get("Page"));
				String title = String.valueOf(bookmark.get("Title"));
				if (page != null && title != null) {
					page = page.split("[ ]")[0];
					int start = Integer.valueOf(page);
					title = title.trim().replace("\r", "").replace("/", "");
					pages.add(start);
					titles.add(title);
				}
			}
		}
	}

	/**
	 * pdf分割
	 * 
	 * @param newFile
	 * @param from
	 * @param end
	 */
	public static void pdfSplit(String newFile, int from, int end) {
		try {
			Document document = null;
			PdfCopy copy = null;
			if (end == 0) {
				end = count;
			}
			document = new Document(reader.getPageSize(1));
			copy = new PdfCopy(document, new FileOutputStream(dir + File.separator + newFile));
			document.open();
			for (int j = from; j <= end; 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文件拆分成JPG图片

    可以把一个PDF文件按页拆分成jpg图片,如果杂志、书籍等出版物

    PMOK(第六版)PDF版章节分解

    本人将PMOK(第六版) pdf书籍,分章节拆分,每章节为一个pdf文档,方便阅读

    PDF文件JAVA去水印源码-Itextsharp:iTextSharp,.NETPDF库

    创建地图和书籍,利用 PDF 中提供的众多交互功能 向现有 PDF 文档添加书签、页码、水印和其他功能 从现有 PDF 文件拆分或连接页面 填写互动表格 将动态生成或处理的 PDF 文档提供给 Web 浏览器 Java、.NET、Android ...

    Objective-C基础教程 中文 PDF 清晰版 [31M]

    资源格式: PDF 版本: 扫描版 出版社: 人民邮电出版社 书号: ISBN: 9787115208774 发行时间: 2009年08月 地区: 大陆 语言: 简体中文 简介: 内容简介 Objective-C是扩展C的面向对象编程语言,也是iPhone开发...

    基于大数据的数据仓库-数据仓库建模基本理论.pdf

    星型模型: 雪花模型: 星型模型和雪花模型的主要区别在于对维度表的拆分,对于雪花模型,维度表的设计更加规范,⼀般符合3NF;⽽星型模型,⼀般采⽤降维 的操作,利⽤冗余来避免模型过于复杂,提⾼易⽤性和分析...

    ORACLE_11g权威指南_12008127_pdf6.ddt

    ORACLE_11g权威指南 讲述11g全面的内容,适合初学习者,考OCP及DBA人员必备参考书籍 (总文件560M多 拆分11个文件供下载)

Global site tag (gtag.js) - Google Analytics