對于docx文件獲取總頁數(shù)試了很多種方式,大多數(shù)都不能準(zhǔn)確獲取對應(yīng)的頁數(shù)玄捕,記錄一下自己是怎么解決這個問題的
/**
* xdocreport 模板 docx
* 描述:獲取DOCX的總頁數(shù) - 嵌入圖片和文字樣式 - 導(dǎo)致頁數(shù)不準(zhǔn)確
* 1:docx4j - 失敗 (失真)31 -> 27
* 2:虛擬打印機(jī) - Spire.Doc for Java (windows 31 -> 31)(centos拋出環(huán)境運(yùn)行異常)
* https://jingyan.baidu.com/article/4f7d571224dae91a201927d1.html
* 3:open office (失真) 31 -> 27
* https://www.cnblogs.com/bb1008/p/10019576.html
* 4:centos cups-pdf虛擬打印機(jī) (失敗,31 -> 1)
* lp -d Cups-PDF /root/桌面/sss.docx
* pdf 目錄位置:/var/spool/cups-pdf/$USER
* vi /etc/cups/cups-pdf.conf #{
* Out ${HOME}/urd/pdf
* 5:libreoffice (失敗 is not assignable to 內(nèi)存溢出)
* cd D:\Program Files\LibreOffice4\program
* soffice --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard
* 6:aspose jar (失真 31 -> 27)
* 7:xdocreport.converter.docx.xwpf (成功裹芝, 31 -> 31 雖然樣式亂了刻坊,總頁數(shù)未受到樣式和圖片的影響,未產(chǎn)生頁數(shù)不一致的情況)
* 總結(jié):獲取 docx 的頁數(shù) 需采用 xdocreport.converter.docx.xwpf 方式獲取
* -- 上傳類型什么就下載什么類型醋虏,若是 docx 需要轉(zhuǎn)換給 客戶寻咒,建議使用 spire doc 走 windows + PDF Architect 虛擬打印機(jī)方式
* #}
* @version : Ver 1.0
* @date : 14/06/2018 16:25
*/
public static int getDocxPageCount(String filePath, String outPath) {
try {
InputStream in =new FileInputStream(new File(filePath));
? ? ? ? XWPFDocument document =new XWPFDocument(in);
? ? ? ? PdfOptions options = PdfOptions.create();
? ? ? ? OutputStream out =new FileOutputStream(new File(outPath));
? ? ? ? PdfConverter.getInstance().convert(document, out, options);
? ? ? ? return getPDFPageCount(outPath);
? ? }catch (Exception e) {
LOGGER.error(e.getMessage(), e);
? ? ? ? throw new GeneralException("docx read error!");
? ? }
}