宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

依赖:

<dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox-app</artifactId>
      <version>1.8.10</version>
 </dependency>

java 用PDFBox 删除 PDF文件中的某一页,前n页,后n页,效率低,不推荐使用

package com.everjiankang;

import java.io.File;

import org.apache.pdfbox.pdmodel.PDDocument;

/**运行效率很慢,因为每次删除一页就读取和保存一次文件,初始文件名格式:xxxx0.pdf*/
public class Test {
    static String  name_pre = "C:\log\jvm";    //文件名前缀
    static String  name_after = ".pdf";//文件名后缀
    public static void mainString[] args) {
        //1.刪除前n頁
//        cutPdfPreNPage2);
        //2.刪除后n頁
        cutPdfAfterNPage5);
        //3.刪除第n頁
        cutPdfname_pre + 0 + name_after,name_pre + 0+1) + name_after,7);//删除第n页
    }
    
    /**
     * 删除前n页
     * @param n
     */
    public static void cutPdfPreNPageint n) {
        forint i = 0; i < n; i++)
            cutPdfname_pre + i + name_after,name_pre + i+1) + name_after,0);
    }
    
    /**
     * 删除后n页
     * @param n
     */
    public static void cutPdfAfterNPageint n) {
        forint i = 0; i < n; i++)
            cutPdfname_pre + i + name_after,name_pre + i+1) + name_after,1);
    }
    
    /**
     * 
     * @param pdfPath        旧路径
     * @param newPdfPath    新路径
     * @param flag            0:第一页;1:最后一页 ;else : 要删除的页码
     */
    public static void cutPdfString pdfPath,String newPdfPath, int flag)
    {
        File file = new FilepdfPath);
        PDDocument document = new PDDocument);
        try{
            document = PDDocument.loadfile);
        }catchException e){
            e.printStackTrace);
        }
        int noOfPages = document.getNumberOfPages);
        System.out.printlnnoOfPages);
        ifflag == 0) 
            document.removePage0);
        else ifflag == 1) {
            document.removePagenoOfPages-1);
        } else {
            document.removePageflag-1);
        }
        try{
            document.savenewPdfPath);
            document.close);
        }catchException e){
            e.printStackTrace);
        }
        System.out.println"已经转完了哦");
        
    }
}

抽取任意范围的PDF页作为新的PDF. 效率高

依赖

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>

代码

 /** 
     * 截取pdfFile的第from页至第end页,组成一个新的文件名 
     * @param pdfFile  需要分割的PDF
     * @param savepath  新PDF
     * @param from  起始页
     * @param end  结束页
     */  
    public static void splitPDFFileString respdfFile,  
            String savepath, int from, int end) {  
        Document document = null;  
        PdfCopy copy = null;          
        try {  
            PdfReader reader = new PdfReaderrespdfFile);            
            int n = reader.getNumberOfPages);            
            ifend==0){  
                end = n;  
            }  
            ArrayList<String> savepaths = new ArrayList<String>);  
            String staticpath = respdfFile.substring0, respdfFile.lastIndexOf"\")+1);  
            //String savepath = staticpath+ newFile;  
            savepaths.addsavepath);  
            document = new Documentreader.getPageSize1));  
            copy = new PdfCopydocument, new FileOutputStreamsavepaths.get0)));  
            document.open);  
            forint j=from; j<=end; j++) {  
                document.newPage);   
                PdfImportedPage page = copy.getImportedPagereader, j);  
                copy.addPagepage);  
            }  
            document.close);  

        } catch IOException e) {  
            e.printStackTrace);  
        } catchDocumentException e) {  
            e.printStackTrace);  
        }  
    }