内容简介:docx文档转pdf文件
注:这里只能转换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方法进行测试。
源代码: 点击前往
以上所述就是小编给大家介绍的《docx文档转pdf文件》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 文件 package.json 的说明文档。
- [ Laravel 5.7 文档 ] 进阶系列 —— 文件存储
- kkFileView v2.2.0 发布,文件文档在线预览解决方案
- kkFileView v2.1.0 发布,文件文档在线预览解决方案
- kkFileView v2.1.0 发布,文件文档在线预览解决方案
- kkFileView v2.1.1 发布,文件文档在线预览解决方案
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Machine Learning
Kevin Murphy / The MIT Press / 2012-9-18 / USD 90.00
Today's Web-enabled deluge of electronic data calls for automated methods of data analysis. Machine learning provides these, developing methods that can automatically detect patterns in data and then ......一起来看看 《Machine Learning》 这本书的介绍吧!