package com.emr.util;

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

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import org.apache.pdfbox.pdmodel.PDDocument;

public class pdfDelPageTest {

    public static void main(String[] args) {
         String  name_pre = "C:\\Users\\HIPAA\\Desktop\\test\\权限系统部署初始化文档2019-09-11";    //文件名前缀
         String  name_after = ".pdf";//文件名后缀
        //1.刪除前n頁
//        cutPdfPreNPage(2);
        //2.刪除后n頁
        //cutPdfAfterNPage(5);
        //3.刪除第n頁
        cutPdf(name_pre + name_after,name_pre + name_after,2);//删除第n页
        //splitPDFFile("C:\\Users\\HIPAA\\Desktop\\test\\权限系统部署初始化文档2019-09-11.pdf","C:\\Users\\HIPAA\\Desktop\\test\\权限系统部署初始化文档2019-09-113.pdf",1,3);
    }



    /**
     * 截取pdfFile的第from页至第end页，组成一个新的文件名
     * @param respdfFile  需要分割的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();
        }
    }

    /**
     * 删除前n页
     * @param n
     */
    public static void cutPdfPreNPage(int n,String name_pre,String name_after) {
        for(int i = 0; i < n; i++)
            //cutPdf(name_pre + i + name_after,name_pre + (i+1) + name_after,0);
            cutPdf(name_pre + name_after,name_pre + name_after,0);//删除第n页
    }

    /**
     * 删除后n页
     * @param n
     */
    public static void cutPdfAfterNPage(int n,String name_pre,String name_after) {
        for(int i = 0; i < n; i++)
           // cutPdf(name_pre + i + name_after,name_pre + (i+1) + name_after,1);
            cutPdf(name_pre + name_after,name_pre + name_after,1);//删除第n页
    }

    /**
     *
     * @param pdfPath        旧路径
     * @param newPdfPath    新路径
     * @param flag            0：第一页；1：最后一页 ；else : 要删除的页码
     */
    public static int 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("已经转完了哦");
     return 1;
    }


}
