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

Java實(shí)現(xiàn)pdf文件合并的使用示例

 更新時(shí)間:2023年12月15日 09:39:08   作者:lang20150928  
本文主要介紹了Java實(shí)現(xiàn)pdf文件合并的使用示例,主要是將需要合并的pdf文件都拷貝到指定目錄a中,調(diào)用該工具類將該目錄作為第一個(gè)參數(shù),第二個(gè)參數(shù)傳入輸出文件對(duì)象即可,感興趣的可以了解一下

在maven項(xiàng)目中引入以下依賴包

    <dependencies>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox-examples</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>

創(chuàng)建一個(gè)工具類

package org.apache.pdfbox.utils;

import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.examples.util.PDFMergerExample;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.io.RandomAccessReadMemoryMappedFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author: guanglai.zhou
 * @date: 2023/12/14 13:15
 */
public class PdfMergerUtils {

    /**
     * 合并指定目錄中的pdf文件
     *
     * @param fromDir  指定目錄
     * @param descFile 目標(biāo)pdf文件
     * @return 目標(biāo)pdf文件
     * @throws IOException
     */
    public static File merge(String fromDir, String descFile) throws IOException {
        final File resultFile = new File(descFile);
        File file = new File(fromDir);
        List<File> files = new ArrayList<>();
        list(file, new Predicate<File>() {
            @Override
            public boolean test(File file) {
                return true;
            }
        }, new Predicate<File>() {
            // 選擇pdf文件
            @Override
            public boolean test(File file) {
                return file.getPath().endsWith(".pdf");
            }
        }, files);
        if (files.isEmpty()) {
            throw new RuntimeException("源文件不存在pdf格式文檔?");
        }
//        files.sort(Comparator.comparing(File::getName));
        if (resultFile.exists()) {
            FileUtils.forceDelete(resultFile);
        }
        mergePdfs(resultFile, files);
        return resultFile;
    }

    /**
     * 針對(duì)文件進(jìn)行遍歷 如果文件夾滿足directoryPredicate,則繼續(xù)遍歷文件夾,如果是文件,則判斷是否滿足filePredicate,如果滿足則添加到
     * collector結(jié)果集當(dāng)中
     *
     * @param file               文件夾
     * @param directoryPredicate 文件夾預(yù)期 為null 則不針對(duì)文件夾做過(guò)濾
     * @param filePredicate      文件預(yù)期 為null 則不針對(duì)文件做過(guò)濾
     * @param collector          收集器 收集所有符合條件的文件
     */
    public static void list(File file, Predicate<File> directoryPredicate, Predicate<File> filePredicate, List<File> collector) {
        File[] childFiles = file.listFiles();
        if (childFiles == null) {
            return;
        }
        // 根據(jù)腳本名稱進(jìn)行排序
        List<File> fileList = Arrays.stream(childFiles).sorted(Comparator.comparing(File::getName)).collect(Collectors.toList());
        for (File childFile : fileList) {
            if (childFile.isDirectory()) {
                boolean pass = directoryPredicate == null || directoryPredicate.test(childFile);
                if (pass) {
                    // 繼續(xù)遍歷子文件夾目錄
                    list(childFile, directoryPredicate, filePredicate, collector);
                }
            } else {
                boolean pass = filePredicate == null || filePredicate.test(childFile);
                if (pass) {
                    collector.add(childFile);
                }
            }
        }
    }

    private static void mergePdfs(File resultFile, List<File> files) throws IOException {
        PDFMergerExample example = new PDFMergerExample();
        List<RandomAccessRead> sources = new ArrayList<>();
        for (File currFile : files) {
            sources.add(new RandomAccessReadMemoryMappedFile(currFile));
        }
        InputStream inputStream = example.merge(sources);
        FileUtils.copyInputStreamToFile(inputStream, resultFile);
    }

}

將需要合并的pdf文件都拷貝到指定目錄a中,調(diào)用該工具類將該目錄作為第一個(gè)參數(shù),第二個(gè)參數(shù)傳入輸出文件對(duì)象即可。

到此這篇關(guān)于Java實(shí)現(xiàn)pdf文件合并的使用示例的文章就介紹到這了,更多相關(guān)Java pdf文件合并內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論