`
yanguz123
  • 浏览: 564983 次
  • 性别: 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 合并拆分工具

    **PDF合并**是将多个PDF文件整合成一个文件的过程,这对于组织大量文档、报告或书籍章节非常有用。例如,如果你有多个独立的章节文件,你可以使用PDF Split and Merge V2.2.1将它们合并成一本完整的电子书。这个过程...

    pdf合并与拆分小软件绿色版

    PDF合并允许用户将多个PDF文件整合成一个单一的文件,这对于整理报告、书籍章节或是会议材料等非常有用。例如,如果你有一系列相关的PDF文件,如研究报告的不同部分,通过这个工具,你可以快速地将它们合并成一个...

    PDF拆页组合小工具PDF编辑非常实用

    PDF文件在日常工作中被广泛应用,尤其在文档分享、电子书籍和报告等方面。然而,有时我们需要对PDF文件进行拆分或组合,以便更好地管理和利用其中的内容。"PDF拆页组合小工具"是一个非常实用的工具,它能帮助用户...

    PDF合并分割工具(Ap PDF Split-Merge) v3.2 绿色中文版

    1. **PDF合并**:PDF合并功能允许用户将多个PDF文件整合成一个单一的文档,这对于整理报告、书籍或大型项目文档非常有用。在Ap PDF Split-Merge中,用户可以选择要合并的PDF文件,然后通过简单的界面操作,一次性将...

    java重构pdf书籍

    - **分解条件式(Decompose Conditional)**:将复杂的条件判断拆分成多个独立的函数,提高代码的可测试性和可理解性。 - **封装集合(Encapsulate Collection)**:将数据集合的管理细节隐藏起来,提供更安全、更便捷的...

    Pdf Split Merge:拆分和合并PDF文件-开源

    PDF Split Merge允许用户将多个PDF文件合并成一个,这对于整理报告、书籍章节或者会议材料等非常方便。用户只需选择要合并的文件,按照所需顺序排列,然后点击合并按钮,即可生成一个新的PDF文件,其中包含了所有...

Global site tag (gtag.js) - Google Analytics