赞
踩
起因,pdf下载的太大了,无法上传有道云笔记,切割成上下两部分
import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; 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 PDF { public static void main(String[] args) { partitionPdfFile("D:/Java 8编程官方参考教程(第9版).pdf","D:/Java 8编程官方参考教程(第9版).pdf)下.pdf", 700,1281); } /** * 截取pdfFile的第from页至第end页,组成一个新的文件名 * @param pdfFile * @param subfileName * @param from * @param end */ public static void partitionPdfFile(String pdfFile, String newFile, int from, int end) { Document document = null; PdfCopy copy = null; try { PdfReader reader = new PdfReader(pdfFile); int n = reader.getNumberOfPages(); if(end==0){ end = n; } ArrayList<String> savepaths = new ArrayList<String>(); String staticpath = pdfFile.substring(0, pdfFile.lastIndexOf("\\")+1); String savepath = staticpath+ newFile; savepaths.add(savepath); document = new Document(reader.getPageSize(1)); copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0))); 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(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。