Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理

1.感谢

感谢小伙伴儿的分享:
不羁
郭中天
整合调整后的工具类Gitee地址:https://gitee.com/yuanzhengme/java_application_aspose_demo

2.包含的工具类

● WordToPdfUtil用于将word文档转换为pdf格式的工具类
● ExcelToPdfUtil用于将excel文档转换为pdf格式的工具类
● PdfToImageUtil用于将pdf文档转换为image格式的工具类

3.lib文件说明

3.1 使用的

● aspose-words-15.8.0-jdk16.jar 将word文档转换为pdf需要引入
● aspose-cells-8.5.2.jar 将excel文档转换为pdf需要引入
● aspose-cells-20.7.jar 将excel文档转换为pdf需要引入(Linux端中文出现乱码时使用)

3.2 未使用的

● aspose-words-15.12.0-jdk16.jar 未测试
● aspose-pdf-22.4.cracked.jar 将pdf转换为其他格式【破解版效果不佳】
● aspose-pdf-22.4.jar 将pdf转换为其他格式【未破解效果依然不佳】

4.核心代码

4.1 WordToPdfUtil

    /\*\*
 \* word 转 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();
            }

        }
    }

4.2 ExcelToPdfUtil

    /\*\*
 \* excel 转 pdf
 \*
 \* @param excelFilePath excel文件路径
 \* @param pdfFilePath pdf文件路径
 \* @param convertSheets 需要转换的sheet
 \*/
    public static void convert(String excelFilePath, String pdfFilePath, int[] convertSheets) {
        FileOutputStream fileOutputStream = null;
        try {
            pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
            // 设置License
            setLicense();
            // 读取excel文件
            Workbook wb = new Workbook(excelFilePath);
            fileOutputStream = new FileOutputStream(pdfFilePath);
            // 设置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();
            }
        }
    }

4.3 PdfToImageUtil

    /\*\*
 \* 根据参数将全部的PDF转换为Image
 \*
 \* @param pdfFilePath PDF文件路径
 \* @param imageFileDir 图片存储目录
 \* @param imageFileName 图片存储文件没
 \* @param type 图片类型
 \*/
    public static void convertAllPage(String pdfFilePath, String imageFileDir, String imageFileName, String type) {
        System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
        // 图片类型
        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();
        }
    }

6.问题处理

  • 都需要将字体文件simsun.ttc上传到jarPath/font目录下。

6.1 Word中文无法转换

还有兄弟不知道网络安全面试可以提前刷题吗?费时一周整理的160+网络安全面试题,金九银十,做网络安全面试里的显眼包!

王岚嵚工程师面试题(附答案),只能帮兄弟们到这儿了!如果你能答对70%,找一个安全工作,问题不大。

对于有1-3年工作经验,想要跳槽的朋友来说,也是很好的温习资料!

【完整版领取方式在文末!!】

93道网络安全面试题

内容实在太多,不一一截图了

黑客学习资源推荐

最后给大家分享一份全套的网络安全学习资料,给那些想学习 网络安全的小伙伴们一点帮助!

对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。

1️⃣零基础入门
① 学习路线

对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。

image

② 路线对应学习视频

同时每个成长路线对应的板块都有配套的视频提供:

image-20231025112050764

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以点击这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 15
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Aspose.Words for .NET提供了将Word文档中的内容转换为图片的功能,可以使用以下代码Word文档转换为图片并调整图片格式列宽: ```csharp // 加载Word文档 Document doc = new Document("input.docx"); // 创建ImageSaveOptions对象 ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png); // 设置图片格式列宽 options.HorizontalResolution = 200; options.VerticalResolution = 200; // 遍历文档中的每个页面,并将其转换为图片 for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++) { // 创建MemoryStream对象 MemoryStream stream = new MemoryStream(); // 将当前页面保存为图片 doc.Save(stream, options); // 加载图片 Image image = Image.FromStream(stream); // 调整图片格式列宽 int newWidth = image.Width * 2; int newHeight = image.Height * 2; // 创建新的Bitmap对象 Bitmap newBitmap = new Bitmap(newWidth, newHeight); // 创建Graphics对象 Graphics graphics = Graphics.FromImage(newBitmap); // 绘制图像 graphics.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); // 保存图片 newBitmap.Save("output.png", ImageFormat.Png); // 释放资源 stream.Dispose(); image.Dispose(); graphics.Dispose(); newBitmap.Dispose(); } ``` 在上述代码中,我们使用了ImageSaveOptions对象来设置生成的图片的格式,包括水平分辨率和垂直分辨率。然后,我们遍历文档中的每个页面,并将其转换为图片。对于每个页面,我们先将其保存为MemoryStream对象,然后将其加载为Image对象。接着,我们根据需要调整图片的格式列宽,然后创建新的Bitmap对象,并使用Graphics对象将原始图像绘制到新的Bitmap对象中。最后,我们保存新的Bitmap对象为PNG格式的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值