广告
返回顶部
首页 > 资讯 > 后端开发 > Python >使用Java编写一个图片word互转工具
  • 657
分享到

使用Java编写一个图片word互转工具

Python 官方文档:入门教程 => 点击学习

摘要

目录前言实现方法使用前言 前段时间一直使用到Word文档转pdf或者pdf转word,寻思着用Java应该是可以实现的,于是花了点时间写了个文件转换工具 源码weloe/FileCo

前言

前段时间一直使用到Word文档转pdf或者pdf转word,寻思着用Java应该是可以实现的,于是花了点时间写了个文件转换工具

源码weloe/FileConversion (GitHub.com)

主要功能就是word和pdf的文件转换,如下

  • pdf 转 word
  • pdf 转 图片
  • word 转 图片
  • word 转 html
  • word 转 pdf

实现方法

主要使用了pdfbox Apache PDFBox | A Java PDF Library以及spire.doc Free Spire.Doc for Java | 100% 免费 Java Word 组件 (e-iceblue.cn)两个工具包

pom.xml

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>Http://repo.e-iceblue.cn/repository/Maven-public/</url>
        </repository>
    </repositories>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>3.9.0</version>
        </dependency>
    </dependencies>

策略接口

public interface FileConversion {

    boolean isSupport(String s);

    String convert(String pathName,String dirAndFileName) throws Exception;

}

PDF转图片实现

public class PDF2Image implements FileConversion{
    private String suffix = ".jpg";
    public static final int DEFAULT_DPI = 150;


    @Override
    public boolean isSupport(String s) {
        return "pdf2image".equals(s);
    }

    @Override
    public String convert(String pathName,String dirAndFileName) throws Exception {
        String outPath = dirAndFileName + suffix;
        if(Files.exists(Paths.get(outPath))){
            throw new RuntimeException(outPath+" 文件已存在");
        }

        pdf2multiImage(pathName,outPath,DEFAULT_DPI);

        return outPath;
    }

    
    public void pdf2multiImage(String pdfFile, String outPath, int dpi) {
        if (dpi <= 0) {
            // 如果没有设置DPI,默认设置为150
            dpi = DEFAULT_DPI;
        }
        try (PDDocument pdf = PDDocument.load(new FileInputStream(pdfFile))) {
            int actSize = pdf.getNumberOfPages();
            List<BufferedImage> picList = new ArrayList<>();
            for (int i = 0; i < actSize; i++) {
                BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i, dpi, ImageType.RGB);
                picList.add(image);
            }
            // 组合图片
            ImageUtil.yPic(picList, outPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

PDF转word实现

public class PDF2Word implements FileConversion {

    private String suffix = ".doc";

    @Override
    public boolean isSupport(String s) {
        return "pdf2word".equals(s);
    }

    
    @Override
    public String convert(String pathName,String dirAndFileName) throws Exception {
        String outPath = dirAndFileName + suffix;
        if(Files.exists(Paths.get(outPath))){
            throw new RuntimeException(outPath+" 文件已存在");
        }

        pdf2word(pathName, outPath);

        return outPath;
    }


    private void pdf2word(String pathName, String outPath) throws IOException {
        PDDocument doc = PDDocument.load(new File(pathName));
        int pagenumber = doc.getNumberOfPages();
        // 创建文件
        createFile(Paths.get(outPath));

        FileOutputStream fos = new FileOutputStream(outPath);
        Writer writer = new OutputStreamWriter(fos, "UTF-8");
        PDFTextStripper stripper = new PDFTextStripper();


        stripper.setSortByPosition(true);//排序

        stripper.setStartPage(1);//设置转换的开始页
        stripper.setEndPage(pagenumber);//设置转换的结束页
        stripper.writeText(doc, writer);
        writer.close();
        doc.close();
    }

}

word转html

public class Word2HTML implements FileConversion{
    private String suffix = ".html";

    @Override
    public boolean isSupport(String s) {
        return "word2html".equals(s);
    }

    @Override
    public String convert(String pathName, String dirAndFileName) {
        String outPath = dirAndFileName + suffix;
        if(Files.exists(Paths.get(outPath))){
            throw new RuntimeException(outPath+" 文件已存在");
        }

        Document doc = new Document();
        doc.loadFromFile(pathName);
        doc.saveToFile(outPath, FileFORMat.Html);
        doc.dispose();
        return outPath;
    }
}

word转图片

public class Word2Image implements FileConversion{
    private String suffix = ".jpg";

    @Override
    public boolean isSupport(String s) {
        return "word2image".equals(s);
    }

    @Override
    public String convert(String pathName, String dirAndFileName) throws Exception {
        String outPath = dirAndFileName + suffix;
        if(Files.exists(Paths.get(outPath))){
            throw new RuntimeException(outPath+" 文件已存在");
        }

        Document doc = new Document();
        //加载文件
        doc.loadFromFile(pathName);
        //上传文档页数,也是最后要生成的图片数
        Integer pageCount = doc.getPageCount();
        // 参数第一个和第三个都写死 第二个参数就是生成图片数
        BufferedImage[] image = doc.saveToImages(0, pageCount, ImageType.Bitmap);
        // 组合图片
        List<BufferedImage> imageList = Arrays.asList(image);
        ImageUtil.yPic(imageList, outPath);
        return outPath;
    }
}

word转pdf

public class Word2PDF implements FileConversion{

    private String suffix = ".pdf";

    @Override
    public boolean isSupport(String s) {
        return "word2pdf".equals(s);
    }

    @Override
    public String convert(String pathName, String dirAndFileName) throws Exception {
        String outPath = dirAndFileName + suffix;
        if(Files.exists(Paths.get(outPath))){
            throw new RuntimeException(outPath+" 文件已存在");
        }
        //加载word
        Document document = new Document();
        document.loadFromFile(pathName, FileFormat.Docx);
        //保存结果文件
        document.saveToFile(outPath, FileFormat.PDF);
        document.close();
        return outPath;
    }
}

使用

输入转换方法,文件路径,输出路径(输出路径如果输入'null'则为文件同目录下同名不同后缀文件)

转换方法可选项:

  • pdf2word
  • pdf2image
  • word2html
  • word2image
  • word2pdf

例如输入:

pdf2word D:\test\testpdf.pdf null

控制台输出:

转换方法: pdf2word  文件: D:\test\testFile.pdf
转换成功!文件路径: D:\test\testFile.doc

到此这篇关于使用Java编写一个PDF Word文件转换工具的文章就介绍到这了,更多相关PDF Word文件转换工具内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 使用Java编写一个图片word互转工具

本文链接: https://www.lsjlt.com/news/177239.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 使用Java编写一个图片word互转工具
    目录前言实现方法使用前言 前段时间一直使用到word文档转pdf或者pdf转word,寻思着用Java应该是可以实现的,于是花了点时间写了个文件转换工具 源码weloe/FileCo...
    99+
    2023-01-10
    图片word互转工具 PDF Word文件转换工具 java图片word互转
  • 基于Java编写一个PDF与Word文件转换工具
    目录前言实现方法pom.xml策略接口PDF转图片实现PDF转word实现word转htmlword转图片word转pdf使用前言 前段时间一直使用到word文档转pdf或者pdf转...
    99+
    2023-01-10
    Java PDF转Word Java Word转PDF Java Word PDF
  • 用python编写一个图片拼接工具
    目录前言代码展示效果展示总结前言 故事要从上面这张表情包开始讲起,看到这张表情包之后,我突发奇想,觉得可以将室友上班摸鱼的照片拼接起来,做成表情包叫他起床 激励他学习!!!于是我马...
    99+
    2022-11-13
  • 怎么用python编写一个图片拼接工具
    本文小编为大家详细介绍“怎么用python编写一个图片拼接工具”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么用python编写一个图片拼接工具”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。代码展示这里写了两...
    99+
    2023-06-28
  • 怎么在c#中使用WinForm制作一个图片编辑工具
    今天就跟大家聊聊有关怎么在c#中使用WinForm制作一个图片编辑工具,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1 功能介绍程序主界面点击打开图片,可选择多个图片文件。图片缩略图...
    99+
    2023-06-07
  • 如何使用 Go 编写一个 Shell 同步工具?
    在日常开发中,我们经常需要在不同的机器之间同步文件或者目录。虽然有很多云存储服务可以使用,但是有时候我们还是需要一些本地的工具来完成这些任务。在这篇文章中,我们将介绍如何使用 Go 编写一个 Shell 同步工具。 首先,我们需要明确一下...
    99+
    2023-10-31
    shell 同步 索引
  • 利用Java怎么编写一个DES加密解密工具类
    今天就跟大家聊聊有关利用Java怎么编写一个DES加密解密工具类,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。DesUtil.javapackage lsy;import java....
    99+
    2023-05-31
    java des ava
  • 使用python怎么编写一个本地应用搜索工具
    这篇文章主要介绍了使用python怎么编写一个本地应用搜索工具,此处通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下:python可以做什么Python是一种编程语言,内置了许多有效的工具,Pyt...
    99+
    2023-06-06
  • 如何使用Python编写一个能够加载Spring Boot应用的工具?
    Spring Boot是一个用于构建基于Spring框架的应用的开源工具,它提供了许多便捷的功能来加速应用程序的开发。如果你是一名Python开发人员,你也可以使用Python编写一个能够加载Spring Boot应用的工具,来提高你的开...
    99+
    2023-10-24
    load linux spring
  • 使用Java怎么实现一个将字母的大小写相互转换功能
    使用Java怎么实现一个将字母的大小写相互转换功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。实现代码:import java.util.Scanner;public cla...
    99+
    2023-05-31
    java 相互 ava
  • 如何使用 Go 语言编写一个能够自动生成二维码并记录日志的打包工具?
    Go 语言是一种强类型、静态类型的编程语言,它的设计目标是简单、高效和可靠。在开发过程中,我们常常需要使用一些工具来帮助我们完成一些重复性的工作,例如打包和发布应用程序。本文将介绍如何使用 Go 语言编写一个能够自动生成二维码并记录日志的打...
    99+
    2023-07-26
    二维码 日志 打包
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作