Template fill content
Write to Word documents using EasyPoi.
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class WriteWordtemplate { public static void main(String[] args) throws Exception { Map<String, Object> params = createDate(); String sourceFile = "d:/temp/temp"; String targetFile = "d:/temp/output result"; FileOutputStream fos = null; try { //Get template document File templateFile = new File(sourceFile); (()); // Write to word XWPFDocument doc = WordExportUtil.exportWord07((), params); fos = (new File(targetFile)); (fos); } catch (Exception e) { (e); } finally { (fos); } } private static Map<String, Object> createDate() { //Fill in data List<WordExportBatch> resultList = new ArrayList<>(); WordExportBatch wordExport = new WordExportBatch(); WordExportBatch wordExport1 = new WordExportBatch(); ("2022/9/30"); ("2022/9/28"); ("11"); ("11"); ("Alipay"); ("15"); ("1234.5"); ("2345.77"); ("2022/12/31"); ("2022/11/30"); ("Alipay"); ("WeChat"); ("22"); ("WeChat"); (wordExport); (wordExport1); //Prepare data Map<String, Object> params = new HashMap<>(); ("number", "112"); ("amount", "1234.5"); ("endDate", "2022/11/30"); ("resultList", resultList); return params; } }
Pom dependencies can be referenced:Use EasyPoi to implement word document generation and paragraph loop
2. Convert word to pdf
There are many ways to generate pdf in word, and I have investigated common methods
One way is to deploy a Windows Server, provide an interface to generate, and restore the format to the greatest extent.
The second way is to use jodconverter, which will cause a little distortion.
What we are introducing now is to convert word to PDF based on jodconverter. First introduce the jar package
<dependency> <groupId></groupId> <artifactId>jodconverter-local</artifactId> <version>4.2.2</version> </dependency> <dependency> <groupId></groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.2.2</version> </dependency>
Add the following configuration file
jodconverter: local: enabled: true # Enable local mode office-home: /opt/libreoffice7.5 # LibreOffice installation path max-tasks-per-process: 10 # Maximum number of tasks per Office process port-numbers: 8202 # The port number used by the LibreOffice backend service can be modified
Injected in the processing class, the reason why @Lazy is used is because the use of loading will report an error, allowing it to delay loading.
@Lazy @Autowired private DocumentConverter converter; public void wordConvertToPdf(File wordFile, File pdfFile) { try { ("word conversion pdf start"); (wordFile).to(pdfFile).execute(); ("word conversion pdf ends"); } catch (Exception e) { ("Word to pdf exception", e); } }
3. Subsequent use
I have written paragraphs that generate paragraphs and generate tables before.
Combined, you can generate a relatively complete word. If you need to stamp or something, just take the generated PDF file.
This is the article about Java filling word content based on templates and converting it into pdf. For more related Java template filling word content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!