maven
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/fr.opensagres.xdocreport.converter.docx.xwpf -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.1</version>
</dependency>
poi-tl语法参考文档
http://deepoove.com/poi-tl/
测试poi-tl
import com.deepoove.poi.XWPFTemplate;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestPoiTl {
public static void main(String[] args) throws Exception {
List<Map<String,Object>> users = new ArrayList<>();
users.add(user("张三",10,"zs"));
users.add(user("李四",20,"ls"));
XWPFTemplate template = XWPFTemplate.compile("D:\\wordpdf\\template.docx").render(
new HashMap<String, Object>(){{
put("users", users);
}});
template.writeAndClose(new FileOutputStream("D:\\wordpdf\\output.docx"));
}
static Map<String,Object> user(String name,int age,String account){
Map<String,Object> user = new HashMap<>();
user.put("name",name);
user.put("age",age);
user.put("account",account);
return user;
}
}
测试word转pdf
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.*;
public class word2pdf {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
String docPath = "D:\\wordpdf\\test.docx";
String pdfPath = "D:\\wordpdf\\test.pdf";
XWPFDocument document;
InputStream doc = new FileInputStream(docPath);
document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(pdfPath);
PdfConverter.getInstance().convert(document, out, options);
doc.close();
out.close();
}
}
test.docx 截图
template.docx截图
效果截图文章来源:https://www.toymoban.com/news/detail-738236.html
文章来源地址https://www.toymoban.com/news/detail-738236.html
到了这里,关于java word转pdf,word模板的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!