首页 > 解决方案 > 如何将多个文件转换为PDF

问题描述

我正在尝试将文件夹中的 Informix4gl 报告文件列表转换为 PDF 文件。

我正在使用 java 和 iText 来转换文件。我目前只成功转换了一个文件。这就是我从文件夹中获取文件的方式。

    //File directory
    public static final String TEXT
    = "O:\\CONVERT\\FOLDER ORI\\BL2054.801";

    //Where file will be stored after conversion
    public static final String DEST
    = "O:\\CONVERT\\FOLDER PDF\\BL2054.801.pdf";

问题是,我必须在代码中定义输入文件名和输出文件名。我想要做的是添加循环,以便程序自动从文件夹中获取文件一并转换为 PDF。然后重复下一个文件。

标签: javaitextpdf-generation

解决方案


getPathFilesFromFolder().stream().forEach({
    file -> convertToPdf(file, generateDestinationPathFile(file))
    //convertToPdf is a method from your lib
})

generateDestinationPathFile(File file) {
    // rename file
}
List<Path> getPathFilesFromFolder() throws IOException {
        return Files.list(Paths.get("D:\\Example"))
                    .collect(Collectors.toList());
}

我们将循环文件夹中的所有文件,然后将其放入需要两个参数(sourceFile、destinationFile)的方法中。


推荐阅读