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

使用Java編寫一個圖片word互轉(zhuǎn)工具

 更新時間:2023年01月10日 08:43:54   作者:秋玻  
這篇文章主要介紹了使用Java編寫一個PDF?Word文件轉(zhuǎn)換工具的相關(guān)資料,需要的朋友可以參考下

前言

前段時間一直使用到word文檔轉(zhuǎn)pdf或者pdf轉(zhuǎn)word,尋思著用Java應(yīng)該是可以實現(xià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

實現(xiàn)方法

主要使用了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轉(zhuǎn)圖片實現(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)圖片
     * 多頁PDF會每頁轉(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,默認設(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實現(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)換方法可選項:

  • 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)PDF Word文件轉(zhuǎn)換工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論