欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA PDF操作之實(shí)現(xiàn)截取N頁(yè)和多個(gè)PDF合并

 更新時(shí)間:2025年01月16日 10:27:14   作者:VipSoft  
這篇文章主要為大家詳細(xì)介紹了java關(guān)于PDF的一些操作,例如截取N頁(yè)并生成新文件,轉(zhuǎn)圖片以及多個(gè)PDF合并,文中的示例代碼講解詳細(xì),感興趣的可以了解下

JAVA PDF 截取N頁(yè),生成新文件,轉(zhuǎn)圖片,多個(gè)PDF 合并

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

完整代碼 

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

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class PdfUtil {

    /**
     * 截取pdfFile的第from頁(yè)至第end頁(yè),組成一個(gè)新的文件名
     *
     * @param pdfFile 要切割的pdf文件
     * @param newFile 切割后形成的新的pdf文件
     * @param from    從第N頁(yè)開始
     * @param end     到第N頁(yè)結(jié)束
     */
    public static void partitionPdf(String pdfFile, String newFile, int from, int end) {
        Document document = null;
        PdfCopy copy = null;
        PdfReader reader = null;
        try {
            reader = new PdfReader(pdfFile);
            int pageCount = reader.getNumberOfPages();
            if (from < 1) {
                from = 1;
            }
            if (from > pageCount) {
                from = pageCount;
            }
            if (end == 0 || end > pageCount) {
                end = pageCount;
            }
            document = new Document(reader.getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(newFile));
            document.open();
            for (int j = from; j <= end; j++) {
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, j);
                copy.addPage(page);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document != null) {
                document.close();
            }
            if (copy != null) {
                copy.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
    }

    /**
     * pdf轉(zhuǎn)圖片
     *
     * @param pdfFile   PDF 文件
     * @param imageFile 輸出的圖片文件
     * @param from      開始頁(yè) 從1開始
     * @param end       結(jié)束頁(yè) 最大為PDF總頁(yè)數(shù)
     * @throws Exception
     */
    public static void pdfToImage(String pdfFile, String imageFile, int from, int end) throws Exception {
        PDDocument doc = null;
        ByteArrayOutputStream os = null;
        InputStream stream = null;
        OutputStream out = null;
        try {
            //pdf路徑
            stream = new FileInputStream(pdfFile);
            // 加載解析PDF文件
            doc = PDDocument.load(stream);
            PDFRenderer pdfRenderer = new PDFRenderer(doc);
            PDPageTree pages = doc.getPages();
            int pageCount = pages.getCount();
            if (from < 1) {
                from = 1;
            }
            if (from > pageCount) {
                from = pageCount;
            }
            if (end == 0 || end > pageCount) {
                end = pageCount;
            }
            for (int i = from; i <= end; i++) {
                BufferedImage bim = pdfRenderer.renderImageWithDPI(i - 1, 200); //PDFBOX 是從0開始的,from初始值為1,所以這邊要減 i-1
                os = new ByteArrayOutputStream();
                ImageIO.write(bim, "jpg", os);
                byte[] dataList = os.toByteArray(); 
                //只取一頁(yè),等于傳進(jìn)來(lái)的名稱,多頁(yè)時(shí),加上 頁(yè)號(hào)
                String imageFilePath = from == end ? saveImgFile : saveImgFile.replace(".jpg", "_" + i + ".jpg");
                File file = new File(imageFilePath);
                if (!file.getParentFile().exists()) {
                    // 不存在則創(chuàng)建父目錄及子文件
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }
                out = new FileOutputStream(file);
                out.write(dataList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (doc != null) {
                doc.close();
            }
            if (os != null) {
                os.close();
            }
            if (stream != null) {
                stream.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    //多個(gè)PDF合并成一個(gè)
    public static void mergePDFFiles(List<String> pdfFiles, String outputPdf) throws IOException {
        // 創(chuàng)建一個(gè)新的 PDF 閱讀器對(duì)象和一個(gè)新的 PDF 寫入對(duì)象
        PdfReader reader = null;
        PdfCopy copy = null;
        Document document = new Document();
        try {
            // 創(chuàng)建 PDF 閱讀器對(duì)象和寫入對(duì)象
            reader = new PdfReader(pdfFiles.get(0));
            copy = new PdfCopy(document, new FileOutputStream(outputPdf));
            // 打開文檔準(zhǔn)備寫入內(nèi)容
            document.open();

            // 將第一個(gè) PDF 的所有頁(yè)面復(fù)制到輸出 PDF 中
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                PdfImportedPage page = copy.getImportedPage(reader, i);
                copy.addPage(page);
            }

            // 將其它PDF的所有頁(yè),輸出到 PDF 中
            for (int i = 1; i < pdfFiles.size(); i++) {
                reader = new PdfReader(pdfFiles.get(i));
                for (int j = 1; j <= reader.getNumberOfPages(); j++) {
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document != null) {
                document.close();
            }
            if (copy != null) {
                copy.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
    }
}

測(cè)試類 

@Test
void pdf() throws Exception {
    String pdfFile = "D:\\Desktop\\20220117.pdf";
    String jpgFile = "D:\\Desktop\\20220117.jpg";
    PdfUtil.pdfToImage(pdfFile, jpgFile, 1, 1); 
}

@Test
 void testMerge() throws IOException {
    List<String> pdfFiles = new ArrayList<>();
    pdfFiles.add("D:\\Projects\\20231225180735.pdf");
    pdfFiles.add("D:\\Projects\\20231225182535.pdf");
    pdfFiles.add("D:\\Projects\\20231225184135.pdf");
    PdfUtil.mergePDFFiles(pdfFiles, "D:\\Projects\\New.pdf");
}

到此這篇關(guān)于JAVA PDF操作之實(shí)現(xiàn)截取N頁(yè)和多個(gè)PDF合并的文章就介紹到這了,更多相關(guān)JAVA PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Ribbon和Feign的區(qū)別及說(shuō)明

    Ribbon和Feign的區(qū)別及說(shuō)明

    本文介紹了Spring Cloud Netflix中的兩個(gè)負(fù)載均衡組件:Ribbon和Feign,Ribbon是一個(gè)基于HTTP和TCP客戶端的負(fù)載均衡工具,使用起來(lái)較為繁瑣,而Feign是一個(gè)使用接口方式的HTTP客戶端,采用類似MyBatis的@Mapper注解方式,使得編寫客戶端變得非常容易
    2024-11-11
  • 淺談Arrays.asList()方法的使用

    淺談Arrays.asList()方法的使用

    本文主要介紹了Arrays.asList()方法的使用。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • SpringBoot集成EasyExcel的步驟

    SpringBoot集成EasyExcel的步驟

    EasyExcel是阿里巴巴開源poi插件之一,主要解決了poi框架使用復(fù)雜,sax解析模式不容易操作,數(shù)據(jù)量大起來(lái)容易OOM,解決了POI并發(fā)造成的報(bào)錯(cuò)。主要解決方式:通過(guò)解壓文件的方式加載,一行一行的加載,并且拋棄樣式字體等不重要的數(shù)據(jù),降低內(nèi)存的占用。
    2021-06-06
  • java發(fā)送http請(qǐng)求并獲取狀態(tài)碼的簡(jiǎn)單實(shí)例

    java發(fā)送http請(qǐng)求并獲取狀態(tài)碼的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)?lái)一篇java發(fā)送http請(qǐng)求并獲取狀態(tài)碼的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05
  • Kotlin與Java 泛型缺陷和應(yīng)用場(chǎng)景詳解

    Kotlin與Java 泛型缺陷和應(yīng)用場(chǎng)景詳解

    這篇文章主要為大家介紹了Kotlin與Java 泛型缺陷和應(yīng)用場(chǎng)景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • idea hibernate jpa 生成實(shí)體類的實(shí)現(xiàn)

    idea hibernate jpa 生成實(shí)體類的實(shí)現(xiàn)

    這篇文章主要介紹了idea hibernate jpa 生成實(shí)體類的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • SpringBoot中注解實(shí)現(xiàn)定時(shí)任務(wù)的兩種方式

    SpringBoot中注解實(shí)現(xiàn)定時(shí)任務(wù)的兩種方式

    這篇文章主要介紹了SpringBoot中注解實(shí)現(xiàn)定時(shí)任務(wù)的兩種方式,SpringBoot 定時(shí)任務(wù)是一種在SpringBoot應(yīng)用中自動(dòng)執(zhí)行任務(wù)的機(jī)制,通過(guò)使用Spring框架提供的@Scheduled注解,我們可以輕松地創(chuàng)建定時(shí)任務(wù),需要的朋友可以參考下
    2023-10-10
  • Mybatis-plus配置分頁(yè)插件返回統(tǒng)一結(jié)果集

    Mybatis-plus配置分頁(yè)插件返回統(tǒng)一結(jié)果集

    本文主要介紹了Mybatis-plus配置分頁(yè)插件返回統(tǒng)一結(jié)果集,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • springboot的LogbackLoggingSystem配置加載流程解析

    springboot的LogbackLoggingSystem配置加載流程解析

    這篇文章主要介紹了springboot的LogbackLoggingSystem配置加載流程源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 創(chuàng)建并運(yùn)行一個(gè)java線程方法介紹

    創(chuàng)建并運(yùn)行一個(gè)java線程方法介紹

    這篇文章主要介紹了創(chuàng)建并運(yùn)行一個(gè)java線程,涉及線程代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11

最新評(píng)論