使用Java編寫一個圖片word互轉(zhuǎ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)文章
數(shù)組實現(xiàn)Java 自定義Queue隊列及應(yīng)用操作
這篇文章主要介紹了數(shù)組實現(xiàn)Java 自定義Queue隊列及應(yīng)用操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java實現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址
這篇文章主要介紹了如何利用Java語言實現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址,文中的示例代碼講解詳細,對我們學(xué)習(xí)有一定的參考價值,快跟隨小編一起學(xué)習(xí)一下吧2022-06-06Maven依賴中scope的runtime和provied的區(qū)別及說明
這篇文章主要介紹了Maven依賴中scope的runtime和provied的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11Elasticsearch中FST與前綴搜索應(yīng)用實戰(zhàn)解析
這篇文章主要為大家介紹了Elasticsearch中FST與前綴搜索應(yīng)用實戰(zhàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08Spring?@Conditional通過條件控制bean注冊過程
這篇文章主要為大家介紹了Spring?@Conditional通過條件控制bean注冊過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02JAVA WEB中Servlet和Servlet容器的區(qū)別
這篇文章主要介紹了JAVA WEB中Servlet和Servlet容器的區(qū)別,文中示例代碼非常詳細,供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06