preface

In many projects will encounter the need To operate Word files, especially To B such projects. For example, one of the most common functions of some recruitment websites is to export resumes, which can be exported in doc, PDF, HTML and other formats.

Requirements:

  • When you upload a Word template, replace the data with the Word template and output a PDF file.
  • The uploaded template needs to be displayed on the web page, that is, what the output PDF file looks like, and what the web page also needs to look like.

Based on the above requirements, the most basic function is still to modify the Word file, after modification and then transfer to PDF, how to display in the web page? For example, we can generate HTML, embed it in a web page, and generate images.

The most common operating framework is POI, OpenOffice is also very powerful, you can directly command the way to convert the format. The above requirements you use other framework can certainly achieve, may be more troublesome, today to recommend a very useful artifact XDOC.

I am called the magic, the reason is really very easy to use, mainly simple, do not need any JAR package dependence, do not need to install task software, direct cloud service support, and is free.

Official website: www.xdocin.com/

use

There are several ways to use it:

  • Direct call interface
  • Use the packaged JAR

For simplicity we’ll go straight to the package, either importing the JAR or copying XDocService.java

XDocService is used to invoke xDoc services in Java applications and on the operating system command line.

Libraries: www.xdocin.com/XDocService… Source: www.xdocin.com/XDocService…

To convert a word file to PDF, create a test.docx file in E:\test.

The file has text, font Settings, pictures, and then start to write conversion code:

XDocService service = new XDocService();
try {
	service.to("E:\\test\\test.docx", new File("E:\\test\\test.pdf"));
} catch (IOException e) {
	e.printStackTrace();
}
Copy the code

Only need 2 lines of code, we can realize word to PDF operation, is not very simple.

Next, we will implement a slightly more complicated requirement. When converting Word to PDF, we will dynamically modify the content in Word. We will modify the docX file, make the title dynamic, and write it in ${text} format.

This means that the template is fixed, but we can set the title text at generation time, look at the code:

XDocService service = new XDocService();
try {
	Map<String, Object> param = new HashMap<String, Object>();
	param.put("Title"."Microservices");
	service.run("E:\\test\\test.docx", param, new File("E:\\test\\test.pdf"));
} catch (IOException e) {
	e.printStackTrace();
}
Copy the code

When it comes to passing parameters, we use the run method and the effect is as follows:

Finally, the image in docX can also be dynamically replaced. The same template can be changed into a placeholder.

The code needs to pass the image as a parameter. There are two ways: one is to pass the address of the image, which needs to be http://xx.png, which can be accessed through the network; the other is the local image, which needs to be transferred to the DataURI method.

XDocService service = new XDocService();
try {
	Map<String, Object> param = new HashMap<String, Object>();
	param.put("Title"."Microservices");
	param.put("Image"."http://cxytiandi.com/images/gongzhonghao.jpg");
	service.run("E:\\test\\test.docx", param, new File("E:\\test\\test.pdf"));
} catch (IOException e) {
	e.printStackTrace();
}
Copy the code

Write so much, more ways to use, please go to the official website to view the document. In a word, it is very easy to use.

New book purchase: a single copy of 75 percent discount free shipping