docx文件轉pdf檔案
阿新 • • 發佈:2019-01-10
注:這裡只能轉換docx檔案,doc不行,原始碼在文末。
可以在windows,linux上執行,增加了內容替換功能,因為有些文件內容需要我們用程式碼來動態生成。
下面是具體操作步驟:
- maven依賴
<!-- docx轉pdf -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version >3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId >com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId >
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-ImportXHTML</artifactId>
<version>3.2.0</version>
</dependency>
<!-- docx轉pdf end -->
- java程式碼
package com.gitee.docx2pdf;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import fr.opensagres.xdocreport.itext.extension.font.IFontProvider;
/**
* 文件工具類
*/
public class DocUtil {
public static void main(String[] args) throws Exception {
String docx = "1.docx";
String pdf = "1.pdf";
// 直接轉換
InputStream docxStream = DocUtil.class.getClassLoader().getResourceAsStream(docx);
byte[] pdfData = docxToPdf(docxStream);
FileUtils.writeByteArrayToFile(new File(pdf), pdfData);
// 替換內容後轉換例子
InputStream docxStream2 = DocUtil.class.getClassLoader().getResourceAsStream("2.docx");
Map<String, String> data = new HashMap<String, String>();
data.put("{title}", "標題內容");
data.put("{username}", "張三");
byte[] pdfData2 = bindDocxDataAndToPdf(docxStream2, data);
FileUtils.writeByteArrayToFile(new File("data.pdf"), pdfData2);
System.out.println("finished.");
}
/**
* 替換docx檔案內容,並轉換成PDF
*
* @param input
* @param data
* @return
* @throws Exception
*/
public static byte[] bindDocxDataAndToPdf(InputStream input, Map<String, String> data) throws Exception {
byte[] replacedContent = replaceDocxContent(input, data);
byte[] pdfData = docxToPdf(new ByteArrayInputStream(replacedContent));
return pdfData;
}
/**
* docx轉成pdf
*
* @param docxStream
* docx檔案流
* @return 返回pdf資料
* @throws Exception
*/
public static byte[] docxToPdf(InputStream docxStream) throws Exception {
ByteArrayOutputStream targetStream = null;
XWPFDocument doc = null;
try {
doc = new XWPFDocument(docxStream);
PdfOptions options = PdfOptions.create();
// 中文字型處理
options.fontProvider(new IFontProvider() {
@Override
public Font getFont(String familyName, String encoding, float size, int style, java.awt.Color color) {
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, style, color);
if (familyName != null)
fontChinese.setFamily(familyName);
return fontChinese;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
});
targetStream = new ByteArrayOutputStream();
PdfConverter.getInstance().convert(doc, targetStream, options);
return targetStream.toByteArray();
} catch (IOException e) {
throw new Exception(e);
} finally {
IOUtils.closeQuietly(targetStream);
}
}
/**
* docx轉換成html內容
*
* @param docxIn
* docx檔案輸入流
* @return
* @throws Exception
*/
public static byte[] docxToHtml(InputStream docxIn) throws Exception {
ByteArrayOutputStream out = null;
try {
XWPFDocument document = new XWPFDocument(docxIn);
XHTMLOptions options = XHTMLOptions.create();
out = new ByteArrayOutputStream();
XHTMLConverter.getInstance().convert(document, out, options);
return out.toByteArray();
} catch (IOException e) {
throw new Exception(e);
} finally {
IOUtils.closeQuietly(out);
}
}
/**
* 替換docx內容
*
* @param in
* docx輸入流
* @param map
* 替換鍵值對
* @return 返回替換後的檔案流
* @throws Exception
*/
public static byte[] replaceDocxContent(InputStream in, Map<String, String> map) throws Exception {
// 讀取word模板
XWPFDocument hdt = null;
ByteArrayOutputStream out = null;
try {
hdt = new XWPFDocument(in);
// 替換段落內容
List<XWPFParagraph> paragraphs = hdt.getParagraphs();
replaceParagraphsContent(paragraphs, map);
// 替換表格內容
List<XWPFTable> tables = hdt.getTables();
// 讀取表格
for (XWPFTable table : tables) {
int rcount = table.getNumberOfRows();
// 遍歷表格中的行
for (int i = 0; i < rcount; i++) {
XWPFTableRow row = table.getRow(i);
// 遍歷行中的單元格
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
List<XWPFParagraph> cellParagraphs = cell.getParagraphs();
replaceParagraphsContent(cellParagraphs, map);
}
}
}
out = new ByteArrayOutputStream();
hdt.write(out);
return out.toByteArray();
} catch (IOException e) {
throw new Exception(e.getMessage());
} finally {
IOUtils.closeQuietly(out);
}
}
private static void replaceParagraphsContent(List<XWPFParagraph> paragraphs, Map<String, String> map) {
for (XWPFParagraph paragraph : paragraphs) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
String text = run.getText(0);
if (text != null) {
boolean isSetText = false;
for (Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
if (text.indexOf(key) != -1) {// 在配置檔案中有這個關鍵字對應的鍵
String value = entry.getValue();
if (value == null) {
throw new RuntimeException(key + "對應的值不能為null");
}
// 文字替換
text = text.replace(key, value);
isSetText = true;
}
}
if (isSetText) {
run.setText(text, 0);
}
}
}
}
}
}
- 新增字型資原始檔
把字型資原始檔新增到resources下面,具體內容在下面的原始碼中可檢視。
這裡的資原始檔是直接從itextasian-1.5.2.jar下拷貝過來的,因為itextasian-1.5.2.jar在中央倉庫已經下載不到了。
最後執行DocUtil 中的main方法進行測試。
原始碼:點選前往