# Java Html转Wrod与Word转PDF
# 安装依赖
1. 将 jar 包拖至 E 盘或其他位置
2. 执行 cmd 命令将 jar 包加入本地仓库
-Dfile :本地 jar 包路径
```shell
mvn install:install-file -Dfile=D:\\aspose-words-20.1-jdk17.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=20.1-jdk17 -Dpackaging=jar -DgeneratePom=true
```
3. 添加依赖
```xml
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.1-jdk17</version>
</dependency>
```
4. 运行 Demo
Html To Word
```java
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.aspose.words.Document;
import com.aspose.words.FieldType;
import com.aspose.words.HeaderFooter;
import com.aspose.words.HeaderFooterCollection;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.ListFormat;
import com.aspose.words.ListLevel;
import com.aspose.words.Node;
import com.aspose.words.NodeCollection;
import com.aspose.words.PageSetup;
import com.aspose.words.Paragraph;
import com.aspose.words.ParagraphAlignment;
import com.aspose.words.Run;
import com.aspose.words.SaveFormat;
import com.aspose.words.Section;
import com.aspose.words.SectionCollection;
import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* HTML => word
*/
public class DocUtil {
public static final String CONTENT = "<!doctype html>\n" +
"<html lang=\"en\">\n" +
" <head>\n" +
" <title>Document</title>\n" +
" </head>\n" +
" <body>\n" +
" ${test}\n" +
" <img src=\"https://fastly.picsum.photos/id/15/200/200.jpg?hmac=8F3A7g2kO57xRlUcdio-9o4LDz0oEFZrYMldJkHMpVo\" width=\"200\" height=\"200\" border=\"0\" alt=\"\">\n" +
" </body>\n" +
"</html>\n";
private static final Pattern PATTERN = Pattern.compile("\\$\\{(.+?)}", Pattern.CASE_INSENSITIVE);
public static void replaceDocTextNew(Map dataMap) {
String content = CONTENT;
// 需要替换的数据不能为空
if (StrUtil.isNotEmpty(content)) {
// 正则匹配
Matcher matcher = PATTERN.matcher(content);
while (matcher.find()) {
// 数据key
String dateKey = matcher.group(1);
// 数据替换
String targetValue = Convert.toStr(dataMap.get(dateKey), "");
// 只替换首次匹配的字符串
content = matcher.replaceFirst(targetValue);
// matcher指向新的字符串
matcher = PATTERN.matcher(content);
}
// documentRoot.setContent(content);
}
}
public static void html2Word(InputStream htmlInputStream, String docxPath) {
try {
File file = new File(docxPath);
if (file.exists()) {
file.delete();
}
// getLicense();
Document document = new Document(htmlInputStream);
document.save(docxPath, SaveFormat.DOCX);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加页码
*
* @author
* @date 2022/07/19
*/
public static void addPageNo(String wordPath) {
try {
// getLicense();
Document document = new Document(wordPath);
// 获取最后一节
Section lastSection = document.getLastSection();
// 添加页脚
HeaderFooter headerFooter = new HeaderFooter(document, HeaderFooterType.FOOTER_PRIMARY);
HeaderFooterCollection headersFooters = lastSection.getHeadersFooters();
headersFooters.add(headerFooter);
// 页脚段落
Paragraph paragraph = new Paragraph(document);
paragraph.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// 起始文本
Run beginRun = new Run(document);
beginRun.setText("第");
beginRun.getFont().setName("宋体");
// 小五号字体
beginRun.getFont().setSize(9.0);
paragraph.appendChild(beginRun);
// 添加页码
paragraph.appendField(FieldType.FIELD_PAGE, true);
// 中间文本
Run middleRun = new Run(document);
middleRun.setText("页 共");
middleRun.getFont().setName("宋体");
// 小五号字体
middleRun.getFont().setSize(9.0);
paragraph.appendChild(middleRun);
// 添加页码
paragraph.appendField(FieldType.FIELD_NUM_PAGES, true);
// 末尾文本
Run endRun = new Run(document);
endRun.setText("页");
endRun.getFont().setName("宋体");
// 小五号字体
endRun.getFont().setSize(9.0);
paragraph.appendChild(endRun);
// 页脚添加段落
headerFooter.appendChild(paragraph);
// 页面设置
PageSetup pageSetup = lastSection.getPageSetup();
// 重新编号
pageSetup.setRestartPageNumbering(true);
pageSetup.setPageStartingNumber(1);
// 更新域
document.updateFields();
document.save(wordPath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 修改文档这里主要是修改标题序号的颜色样式
*
* @author lhl
* @date 2022/07/19
*/
public static void setStyle(String wordPath) {
try {
// getLicense();
Document document = new Document(wordPath);
// 获取所有节
SectionCollection sections = document.getSections();
for (int i = 0; i < sections.getCount(); i++) {
Section section = sections.get(i);
// 获取没节的子节点
NodeCollection childNodes = section.getBody().getChildNodes();
for (int j = 0; j < childNodes.getCount(); j++) {
Node node = childNodes.get(j);
if (node instanceof Paragraph) {
// 获取段落
Paragraph paragraph = (Paragraph) node;
ListFormat listFormat = paragraph.getListFormat();
ListLevel listLevel = listFormat.getListLevel();
if (listLevel != null) {
// 设置字体颜色为黑色
listLevel.getFont().setColor(Color.BLACK);
}
}
}
}
document.save(wordPath);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String saveDoc() {
String docName = UUID.randomUUID() + ".docx";
// 获取系统临时文件夹
String docPath = "C:\\Users\\Qz1997\\Desktop\\";
System.out.println("docPath = " + docPath);
docPath = docPath + docName;
InputStream wordInputStream = new ByteArrayInputStream(DocUtil.CONTENT.getBytes(Stand
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
word处理.zip (4个子文件)
demo
Doc2pdf.java 589B
DocUtil.java 7KB
aspose-words-20.1-jdk17.jar 13.39MB
README.md 9KB
共 4 条
- 1
资源评论
Qz1997
- 粉丝: 36
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功