最后
如果觉得本文对你有帮助的话,不妨给我点个赞,关注一下吧!
<version>2.0.1</version>
在关闭流之前添加并修改reponse中.docx为.pdf
response.setHeader(“Content-Disposition”, “attachment; filename=” + java.net.URLEncoder.encode(“日报-”+datetime+“.pdf”, “UTF-8”));
//转为PDF
PdfOptions options = PdfOptions.create();
PdfConverter.getInstance().convert(document, outStream, options);
//下面再是转word里面最后的代码,关闭流
2:**使用aspose.words的Document方式将word转换为PDF**
1:下载jar包:[jar包下载]( )
2:将jar包放入项目中resources目录下的lib文件夹中:
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/762adec6635740e2b63ac8fafd6e8795.png)
3:将jar包转为library
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/b3d79e02a6994a799fb263a0a0a30c44.png)
转换后就会出现上面图中箭头处的样子可以打开。
4:引入jar包依赖:
com.aspose.words aspose-words 15.8.0 system ${project.basedir}/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar
在打包的依赖中添加:
<plugin>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
5:转换
String s = “Aspose.Total for JavaAspose.Words for JavaEnterprise20991231209912318bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=”;
//去除水印
ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
License license = new License();
license.setLicense(is);
//将XWPFDocument转换为InputStream
ByteArrayOutputStream b = new ByteArrayOutputStream();
//这里的document=XWPFDocument document,在下面的word转换中
document.write(b);
InputStream inputStream = new ByteArrayInputStream(b.toByteArray());
//这里的Document 的引入是
//import com.aspose.words.Document;
//import com.aspose.words.License;
//import com.aspose.words.SaveFormat;
Document doc = new Document(inputStream);
doc.save(outStream, SaveFormat.PDF);
b.close();
inputStream.close();
//下面再是转word里面最后的代码,关闭流
3:**使用documents4j 的方式将word转换为PDF**
1:引入依赖:
<!-- word 转 pdf 通过documents4j实现 -->
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
2:转换如下:
//将XWPFDocument转换为InputStream
ByteArrayOutputStream b = new ByteArrayOutputStream();
//这里的document=XWPFDocument document,在下面的word转换中
document.write(b);
InputStream docxInputStream = new ByteArrayInputStream(b.toByteArray());
//下面的引入类为:
//import com.documents4j.api.DocumentType;
//import com.documents4j.api.IConverter;
//import com.documents4j.job.LocalConverter;
IConverter converter = LocalConverter.builder().build();
boolean execute = converter.convert(docxInputStream)
.as(DocumentType.DOCX)
.to(outStream)
.as(DocumentType.PDF).schedule().get();
b.close();
docxInputStream.close();
### 3:这里之前转换word方式记录如下
1:制作word模板,将需要转换的数值写成了${变量名}。
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/300e995470ff451f8d6adf6bee972b27.png)
2:转换
//模板文件的地址
String filePath = “/usr/local/data/模板.docx”;
//Map存储需要替换的值
Map<String, Object> map = new HashMap<>();
map.put(“
d
a
t
e
"
,
d
a
t
e
)
;
m
a
p
.
p
u
t
(
"
{date}", date); map.put("
date",date);map.put("{datetime}”, datetime);
//写入
try {
// 替换的的关键字存放到Set集合中
Set set = map.keySet();
// 读取模板文档
XWPFDocument document = new XWPFDocument(new FileInputStream(filePath ));
/**
* 替换段落中的指定文字
*/
// 读取文档中的段落,回车符为一个段落。
// 同一个段落里面会被“:”等符号隔开为多个对象
Iterator itPara = document.getParagraphsIterator();
while (itPara.hasNext()) {
// 获取文档中当前的段落文字信息
XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
List run = paragraph.getRuns();
// 遍历段落文字对象
for (int i = 0; i < run.size(); i++) {
// 获取段落对象
if (run.get(i) == null) { //段落为空跳过
continue;
}
String sectionItem = run.get(i).getText(run.get(i).getTextPosition()); //段落内容
//System.out.println("替换前 === "+sectionItem);
// 遍历自定义表单关键字,替换Word文档中的内容
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
// 当前关键字
String key = iterator.next();
// 替换内容
sectionItem = sectionItem.replace(key, String.valueOf(map.get(key)));
}
//System.out.println(sectionItem);
run.get(i).setText(sectionItem, 0);
}
}
/\*\*
* 替换表格中的指定文字
总结
无论是哪家公司,都很重视高并发高可用的技术,重视基础,重视JVM。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。其实我写了这么多,只是我自己的总结,并不一定适用于所有人,相信经过一些面试,大家都会有这些感触。
最后我整理了一些面试真题资料,技术知识点剖析教程,还有和广大同仁一起交流学习共同进步,还有一些职业经验的分享。