• java如何实现批量删除pdf指定的页数


    依赖:

    <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 main(String[] args) {
            //1.刪除前n頁
    //        cutPdfPreNPage(2);
            //2.刪除后n頁
            cutPdfAfterNPage(5);
            //3.刪除第n頁
            cutPdf(name_pre + 0 + name_after,name_pre + (0+1) + name_after,7);//删除第n页
        }
        
        /**
         * 删除前n页
         * @param n
         */
        public static void cutPdfPreNPage(int n) {
            for(int i = 0; i < n; i++)
                cutPdf(name_pre + i + name_after,name_pre + (i+1) + name_after,0);
        }
        
        /**
         * 删除后n页
         * @param n
         */
        public static void cutPdfAfterNPage(int n) {
            for(int i = 0; i < n; i++)
                cutPdf(name_pre + i + name_after,name_pre + (i+1) + name_after,1);
        }
        
        /**
         * 
         * @param pdfPath        旧路径
         * @param newPdfPath    新路径
         * @param flag            0:第一页;1:最后一页 ;else : 要删除的页码
         */
        public static void cutPdf(String pdfPath,String newPdfPath, int flag)
        {
            File file = new File(pdfPath);
            PDDocument document = new PDDocument();
            try{
                document = PDDocument.load(file);
            }catch(Exception e){
                e.printStackTrace();
            }
            int noOfPages = document.getNumberOfPages();
            System.out.println(noOfPages);
            if(flag == 0) 
                document.removePage(0);
            else if(flag == 1) {
                document.removePage(noOfPages-1);
            } else {
                document.removePage(flag-1);
            }
            try{
                document.save(newPdfPath);
                document.close();
            }catch(Exception 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 splitPDFFile(String respdfFile,  
                String savepath, int from, int end) {  
            Document document = null;  
            PdfCopy copy = null;          
            try {  
                PdfReader reader = new PdfReader(respdfFile);            
                int n = reader.getNumberOfPages();            
                if(end==0){  
                    end = n;  
                }  
                ArrayList<String> savepaths = new ArrayList<String>();  
                String staticpath = respdfFile.substring(0, respdfFile.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();  
            }  
        }  
    复制代码
  • 相关阅读:
    ARM里面的APB和AHB
    解决win10点击开始按钮无反应
    keil的51和ARM共存方法
    对于一个液晶而言什么是读状态、读数据、写指令、写数据
    关于VMware中Ubuntu 出现Unknown Display问题解决
    论基于SOA的面向服务架构设计及其应用
    科技小论文2
    软件体系架构的质量属性--论文
    一线架构师实践指南阅读笔记03
    一线架构师阅读指南-阅读感想02
  • 原文地址:https://www.cnblogs.com/qianzf/p/11859051.html
Copyright © 2020-2023  润新知