在此我就只介紹我使用的方法:xdocreport。xdocreport適合對轉換後的pdf格式要求不高的情況,我們需求中的pdf雖然內容種類和格式都挺多,但最後實現出來,基本可以滿足需要,並且xdocreport使用起來確實方便。
public void makePdf(String basePath, String fileName) throws Exception {
File docxFile = null;
InputStream docxStream = null;
File outFile = null;
OutputStream out = null;
try {
long startTime = System.currentTimeMillis();
docxFile = new File(basePath + fileName + ".docx");
docxStream = new FileInputStream(docxFile);
XWPFDocument document = new XWPFDocument(docxStream);
outFile = new File(basePath + fileName + ".pdf");
outFile.getParentFile().mkdirs();
out = new FileOutputStream(outFile);
PdfOptions options = PdfOptions.create(); // gb2312】
PdfConverter.getInstance().convert(document, out, options);
} catch (Exception e) {
logger.error("讀取模板docx文件並轉換pdf異常", e);
} finally {
try {
if (docxStream != null) {
docxStream.close();
logger.info("讀取模板轉換pdf關閉docxStream流正常");
}
} catch (Exception e) {
logger.error("讀取模板轉換pdf關閉流異常", e);
}
if (out != null) {
out.close();
}
}
}
對於xdocreport使用,我感覺我還處於最初級的使用,其更高級的一些設置還有待學習,也希望和大家多多交流學習.
這裏遇到的主要問題就是,轉換後表格邊框的顏色太深、文字靠下顯示,有壓下邊框的情況。暫時通過調整word中邊框的顏色或是在文字後邊加換行符\n進行了處理。如果大家誰有更好的解決方案,煩請留言交流。謝謝!