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

基于Java實(shí)現(xiàn)將word,excel文件轉(zhuǎn)換為pdf的工具類(lèi)

 更新時(shí)間:2025年08月01日 10:30:37   作者:yuanzhengme  
這篇文章主要為大家詳細(xì)介紹了如何使用Java編寫(xiě)一個(gè)工具類(lèi),可以實(shí)現(xiàn)將word,excel文件轉(zhuǎn)換為pdf格式和將pdf文檔轉(zhuǎn)換為image格式,希望對(duì)大家有所幫助

包含的工具類(lèi)

WordToPdfUtil用于將word文檔轉(zhuǎn)換為pdf格式的工具類(lèi)

ExcelToPdfUtil用于將excel文檔轉(zhuǎn)換為pdf格式的工具類(lèi)

PdfToImageUtil用于將pdf文檔轉(zhuǎn)換為image格式的工具類(lèi)

lib文件說(shuō)明

使用的

aspose-words-15.8.0-jdk16.jar 將word文檔轉(zhuǎn)換為pdf需要引入

aspose-cells-8.5.2.jar 將excel文檔轉(zhuǎn)換為pdf需要引入

aspose-cells-20.7.jar 將excel文檔轉(zhuǎn)換為pdf需要引入(Linux端中文出現(xiàn)亂碼時(shí)使用)

未使用的

aspose-words-15.12.0-jdk16.jar 未測(cè)試

aspose-pdf-22.4.cracked.jar 將pdf轉(zhuǎn)換為其他格式【破解版效果不佳】

aspose-pdf-22.4.jar 將pdf轉(zhuǎn)換為其他格式【未破解效果依然不佳】

核心代碼

WordToPdfUtil

    /**
     * word 轉(zhuǎn) pdf
     *
     * @param wordFilePath word文件路徑
     * @param pdfFilePath  pdf文件路徑
     */
    public static void convert(String wordFilePath, String pdfFilePath) {
        FileOutputStream fileOutputStream = null;
        try {
            pdfFilePath = pdfFilePath == null ? getPdfFilePath(wordFilePath) : pdfFilePath;
            setLicense();
            File file = new File(pdfFilePath);
            fileOutputStream = new FileOutputStream(file);
            Document doc = new Document(wordFilePath);
            doc.save(fileOutputStream, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                assert fileOutputStream != null;
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

ExcelToPdfUtil

    /**
     * excel 轉(zhuǎn) pdf
     *
     * @param excelFilePath excel文件路徑
     * @param pdfFilePath   pdf文件路徑
     * @param convertSheets 需要轉(zhuǎn)換的sheet
     */
    public static void convert(String excelFilePath, String pdfFilePath, int[] convertSheets) {
        FileOutputStream fileOutputStream = null;
        try {
            pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
            // 設(shè)置License
            setLicense();
            // 讀取excel文件
            Workbook wb = new Workbook(excelFilePath);
            fileOutputStream = new FileOutputStream(pdfFilePath);
            // 設(shè)置pdf格式
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);
            if (null != convertSheets) {
                printSheetPage(wb, convertSheets);
            }
            wb.save(fileOutputStream, pdfSaveOptions);
            fileOutputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                assert fileOutputStream != null;
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

PdfToImageUtil

    /**
     * 根據(jù)參數(shù)將全部的PDF轉(zhuǎn)換為Image
     *
     * @param pdfFilePath   PDF文件路徑
     * @param imageFileDir  圖片存儲(chǔ)目錄
     * @param imageFileName 圖片存儲(chǔ)文件沒(méi)
     * @param type          圖片類(lèi)型
     */
    public static void convertAllPage(String pdfFilePath, String imageFileDir, String imageFileName, String type) {
        System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
        // 圖片類(lèi)型
        if (type == null || "".equals(type)) {
            type = IMAGE_TYPE_JPG;
        }
        // 1.加載PDF文件
        File file = new File(pdfFilePath);
        // 2.生成JPG圖片的文件夾
        imageFileDir = imageFileDir == null ? getImageFileDir(pdfFilePath) : imageFileDir;
        imageFileName = imageFileName == null ? getImageFileName(pdfFilePath) : imageFileName;
        try {
            PDDocument pdDocument = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            int pageCount = pdDocument.getNumberOfPages();

            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 144);
                ImageIO.write(image, type,
                        new File(imageFileDir.concat(File.separator).concat(imageFileName).concat("_")
                                .concat(String.valueOf(i + 1)).concat(".").concat(type)));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

問(wèn)題處理

都需要將字體文件simsun.ttc上傳到jarPath/font目錄下。

Word中文無(wú)法轉(zhuǎn)換

在Linux環(huán)境下,如果轉(zhuǎn)換后的pdf文件無(wú)中文,在WordToPdfUtil轉(zhuǎn)換方法里添加以下代碼:

// 設(shè)置字體
String realPath = new ApplicationHome(WordToPdfUtil.class).getSource().getParentFile().toString();
FontSettings.setFontsFolder(realPath + File.separatorChar + "font", false);

Excel中文無(wú)法轉(zhuǎn)換

使用aspose-cells-20.7.jar:

<dependency>
	<groupId>com.aspose.cells</groupId>
	<artifactId>aspose-cells</artifactId>
	<version>20.7</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/lib/aspose-cells-20.7.jar</systemPath>
</dependency>

并在ExcelToPdfUtil轉(zhuǎn)換方法里添加以下代碼:

// 設(shè)置字體
String realPath = new ApplicationHome(WordToPdfUtil.class).getSource().getParentFile().toString();
String fontDir = realPath + File.separatorChar + "font";
IndividualFontConfigs individualFontConfigs = new IndividualFontConfigs();
individualFontConfigs.setFontFolder(fontDir, false);
LoadOptions loadOptions = new LoadOptions();
loadOptions.setFontConfigs(individualFontConfigs);
// 讀取excel文件
Workbook wb = new Workbook(excelFilePath, loadOptions);

7.總結(jié)

PDF轉(zhuǎn)換為其他格式的方法效果不佳,遇到好的方案會(huì)進(jìn)行補(bǔ)充。

主要用到aspose的jar包,實(shí)際上是需要授權(quán)的,否則會(huì)有水印,是個(gè)隱患。

到此這篇關(guān)于基于Java實(shí)現(xiàn)將word,excel文件轉(zhuǎn)換為pdf的工具類(lèi)的文章就介紹到這了,更多相關(guān)Java word與excel轉(zhuǎn)pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論