亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

前言

前段時(shí)間一直使用到word文檔轉(zhuǎn)pdf或者pdf轉(zhuǎn)word,尋思著用Java應(yīng)該是可以實(shí)現(xiàn)的,于是花了點(diǎn)時(shí)間寫了個(gè)文件轉(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)兩個(gè)工具包

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)換時(shí)間變長
     */
    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ù)第一個(gè)和第三個(gè)都寫死 第二個(gè)參數(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編寫一個(gè)PDF與Word文件轉(zhuǎn)換工具的文章就介紹到這了,更多相關(guān)Java PDF轉(zhuǎn)Word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring集成mybatis實(shí)現(xiàn)mysql數(shù)據(jù)庫讀寫分離

    spring集成mybatis實(shí)現(xiàn)mysql數(shù)據(jù)庫讀寫分離

    本文通過實(shí)例代碼給大家介紹了spring集成mybatis實(shí)現(xiàn)mysql數(shù)據(jù)庫讀寫分離,需要的朋友可以參考下
    2017-08-08
  • JDK動態(tài)代理提高代碼可維護(hù)性和復(fù)用性利器

    JDK動態(tài)代理提高代碼可維護(hù)性和復(fù)用性利器

    這篇文章主要為大家介紹了JDK動態(tài)代理提高代碼可維護(hù)性和復(fù)用性利器,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Mybatis中#{}與${}的區(qū)別詳解

    Mybatis中#{}與${}的區(qū)別詳解

    這篇文章主要介紹了Mybatis中#{}與${}的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • SpringCloud Open feign 使用okhttp 優(yōu)化詳解

    SpringCloud Open feign 使用okhttp 優(yōu)化詳解

    這篇文章主要介紹了SpringCloud Open feign 使用okhttp 優(yōu)化詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 在spring中手寫全局異常攔截器

    在spring中手寫全局異常攔截器

    這篇文章主要介紹了如何在spring中手寫全局異常攔截器,幫助大家更好的理解和使用spring框架,感興趣的朋友可以了解下
    2020-11-11
  • 詳解SpringBoot開發(fā)案例之整合定時(shí)任務(wù)(Scheduled)

    詳解SpringBoot開發(fā)案例之整合定時(shí)任務(wù)(Scheduled)

    本篇文章主要介紹了詳解SpringBoot開發(fā)案例之整合定時(shí)任務(wù)(Scheduled),具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-07-07
  • Java實(shí)現(xiàn)簡單的萬年歷

    Java實(shí)現(xiàn)簡單的萬年歷

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單的萬年歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Java輕松入門冒泡?選擇?插入?希爾?歸并排序算法

    Java輕松入門冒泡?選擇?插入?希爾?歸并排序算法

    這篇文章主要介紹了Java常用的排序算法及代碼實(shí)現(xiàn),在Java開發(fā)中,對排序的應(yīng)用需要熟練的掌握,這樣才能夠確保Java學(xué)習(xí)時(shí)候能夠有扎實(shí)的基礎(chǔ)能力。那Java有哪些排序算法呢?本文小編就來詳細(xì)說說Java常見的排序算法,需要的朋友可以參考一下
    2022-02-02
  • Java BufferedReader相關(guān)源碼實(shí)例分析

    Java BufferedReader相關(guān)源碼實(shí)例分析

    這篇文章主要介紹了Java BufferedReader相關(guān)源碼實(shí)例分析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 詳解java設(shè)計(jì)模式中的門面模式

    詳解java設(shè)計(jì)模式中的門面模式

    門面模式又叫外觀模式(Facade?Pattern),主要用于隱藏系統(tǒng)的復(fù)雜性,并向客戶端提供了一個(gè)客戶端可以訪問系統(tǒng)的接口,本文通過實(shí)例代碼給大家介紹下java門面模式的相關(guān)知識,感興趣的朋友一起看看吧
    2022-02-02

最新評論