最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Java編寫一個PDF與Word文件轉(zhuǎn)換工具

 更新時間:2023年01月10日 09:09:24   作者:秋玻  
前段時間一直使用到word文檔轉(zhuǎn)pdf或者pdf轉(zhuǎn)word,尋思著用Java應(yīng)該是可以實(shí)現(xiàn)的,于是花了點(diǎn)時間寫了個文件轉(zhuǎn)換工具,感興趣的可以了解一下

前言

前段時間一直使用到word文檔轉(zhuǎn)pdf或者pdf轉(zhuǎn)word,尋思著用Java應(yīng)該是可以實(shí)現(xiàn)的,于是花了點(diǎn)時間寫了個文件轉(zhuǎn)換工具

源碼weloe/FileConversion (github.com)

主要功能就是word和pdf的文件轉(zhuǎn)換,如下

  • pdf 轉(zhuǎn) word
  • pdf 轉(zhuǎn) 圖片
  • word 轉(zhuǎn) 圖片
  • word 轉(zhuǎn) html
  • word 轉(zhuǎn) pdf

實(shí)現(xiàn)方法

主要使用了pdfbox Apache PDFBox | A Java PDF Library以及spire.doc Free Spire.Doc for Java | 100% 免費(fèi) 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轉(zhuǎn)圖片實(shí)現(xiàn)

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;
    }

    /**
     * pdf轉(zhuǎn)圖片
     * 多頁P(yáng)DF會每頁轉(zhuǎn)換為一張圖片,下面會有多頁組合成一頁的方法
     *
     * @param pdfFile pdf文件路徑
     * @param outPath 圖片輸出路徑
     * @param dpi 相當(dāng)于圖片的分辨率,值越大越清晰,但是轉(zhuǎn)換時間變長
     */
    public void pdf2multiImage(String pdfFile, String outPath, int dpi) {
        if (dpi <= 0) {
            // 如果沒有設(shè)置DPI,默認(rèn)設(shè)置為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轉(zhuǎn)word實(shí)現(xiàn)

public class PDF2Word implements FileConversion {

    private String suffix = ".doc";

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

    /**
     *
     * @param pathName
     * @throws IOException
     */
    @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();
        // 創(chuàng)建文件
        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);//設(shè)置轉(zhuǎn)換的開始頁
        stripper.setEndPage(pagenumber);//設(shè)置轉(zhuǎn)換的結(jié)束頁
        stripper.writeText(doc, writer);
        writer.close();
        doc.close();
    }

}

word轉(zhuǎn)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轉(zhuǎn)圖片

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);
        //上傳文檔頁數(shù),也是最后要生成的圖片數(shù)
        Integer pageCount = doc.getPageCount();
        // 參數(shù)第一個和第三個都寫死 第二個參數(shù)就是生成圖片數(shù)
        BufferedImage[] image = doc.saveToImages(0, pageCount, ImageType.Bitmap);
        // 組合圖片
        List<BufferedImage> imageList = Arrays.asList(image);
        ImageUtil.yPic(imageList, outPath);
        return outPath;
    }
}

word轉(zhuǎn)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);
        //保存結(jié)果文件
        document.saveToFile(outPath, FileFormat.PDF);
        document.close();
        return outPath;
    }
}

使用

輸入轉(zhuǎn)換方法,文件路徑,輸出路徑(輸出路徑如果輸入'null'則為文件同目錄下同名不同后綴文件)

轉(zhuǎn)換方法可選項(xiàng):

  • pdf2word
  • pdf2image
  • word2html
  • word2image
  • word2pdf

例如輸入:

pdf2word D:\test\testpdf.pdf null

控制臺輸出:

轉(zhuǎn)換方法: pdf2word  文件: D:\test\testFile.pdf
轉(zhuǎn)換成功!文件路徑: D:\test\testFile.doc

到此這篇關(guān)于基于Java編寫一個PDF與Word文件轉(zhuǎn)換工具的文章就介紹到這了,更多相關(guān)Java PDF轉(zhuǎn)Word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Vue+Spring Boot實(shí)現(xiàn)Excel上傳功能

    使用Vue+Spring Boot實(shí)現(xiàn)Excel上傳功能

    這篇文章主要介紹了使用Vue+Spring Boot實(shí)現(xiàn)Excel上傳,需要的朋友可以參考下
    2018-11-11
  • springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢websocket的集成使用

    springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢websocket的集成使用

    WebSocket使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡單,允許服務(wù)端主動向客戶端推送數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢websocket的集成使用,需要的朋友可以參考下
    2022-10-10
  • Java模擬UDP通信示例代碼

    Java模擬UDP通信示例代碼

    這篇文章主要介紹了Java模擬UDP通信,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • maven導(dǎo)入本地倉庫jar包,報:Could?not?find?artifact的解決

    maven導(dǎo)入本地倉庫jar包,報:Could?not?find?artifact的解決

    這篇文章主要介紹了maven導(dǎo)入本地倉庫jar包,報:Could?not?find?artifact的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • springboot使用IDEA遠(yuǎn)程Debug

    springboot使用IDEA遠(yuǎn)程Debug

    項(xiàng)目上線之后,如果日志打印的很模糊或者業(yè)務(wù)邏輯比較復(fù)雜,有時候無法定位具體的錯誤原因,因此可以通過IDEA遠(yuǎn)程代理進(jìn)行Debug,本文就來介紹一下如何使用
    2021-06-06
  • Jenkins系統(tǒng)如何進(jìn)行數(shù)據(jù)備份

    Jenkins系統(tǒng)如何進(jìn)行數(shù)據(jù)備份

    隨著我們的長期使用,Jenkins系統(tǒng)中的內(nèi)容會越來越多,特別是一些配置相關(guān)的東西,不能有任何丟失。這個時候我們就需要定期備份我們的Jenkins系統(tǒng),避免一些誤操作不小心刪除了某些重要文件,本文就將介紹下Jenkins系統(tǒng)如何進(jìn)行數(shù)據(jù)備份
    2021-06-06
  • Spring?Boot?Security認(rèn)證之Redis緩存用戶信息詳解

    Spring?Boot?Security認(rèn)證之Redis緩存用戶信息詳解

    本文介紹了如何使用Spring Boot Security進(jìn)行認(rèn)證,并通過Redis緩存用戶信息以提高系統(tǒng)性能,通過配置RedisUserDetailsManager,我們成功地將用戶信息存儲到了Redis中,并在Spring Security中進(jìn)行了集成,需要的朋友可以參考下
    2024-01-01
  • Springboot通用mapper和mybatis-generator代碼示例

    Springboot通用mapper和mybatis-generator代碼示例

    這篇文章主要介紹了Springboot通用mapper和mybatis-generator代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-12-12
  • Java如何實(shí)現(xiàn)圖像的卷積效果

    Java如何實(shí)現(xiàn)圖像的卷積效果

    這篇文章主要介紹了Java如何實(shí)現(xiàn)圖像的卷積效果問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • SpringBoot 策略模式實(shí)現(xiàn)切換上傳文件模式

    SpringBoot 策略模式實(shí)現(xiàn)切換上傳文件模式

    策略模式是指有一定行動內(nèi)容的相對穩(wěn)定的策略名稱,這篇文章主要介紹了SpringBoot 策略模式 切換上傳文件模式,需要的朋友可以參考下
    2023-11-11

最新評論

克东县| 涡阳县| 措美县| 郑州市| 滁州市| 房产| 和林格尔县| 康乐县| 白山市| 濮阳县| 繁峙县| 大渡口区| 赤城县| 梁平县| 库尔勒市| 方城县| 黄石市| 康平县| 大新县| 雅江县| 永德县| 建始县| 塘沽区| 龙里县| 新密市| 甘肃省| 格尔木市| 中方县| 长子县| 赫章县| 乐陵市| 成都市| 高阳县| 岢岚县| 镇安县| 青河县| 水城县| 祁阳县| 唐海县| 贵定县| 阿合奇县|