Maven Dependency
<dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId> Org.apache.poi.xwpf.converter.pdf</artifactid><version>1.0.4</version></dependency>
Gradle Dependency
Compile ' fr.opensagres.xdocreport:org.apache.poi.xwpf.converter.pdf:1.0.4 '
Example
import java.io.fileinputstream;import java.io.fileoutputstream;import java.io.inputstream; import java.io.outputstream;import java.util.hashmap;import java.util.list;import java.util.map;import org.apache.commons.collections.maputils;import Org.apache.poi.xwpf.converter.pdf.pdfconverter;import org.apache.poi.xwpf.converter.pdf.pdfoptions;import org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFParagraph; Import org.apache.poi.xwpf.usermodel.xwpfrun;import org.apache.poi.xwpf.usermodel.xwpftable;import org.apache.poi.xwpf.usermodel.XWPFTableCell;import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.slf4j.logger;import org.slf4j.loggerfactory;import fr.opensagres.xdocreport.utils.stringutils;/** * @author rocca * */public class wordpdfutils {protected static final logger logger = loggerfActory.getlogger (Wordpdfutils.class); public static void main (String[] args) throws exception{ string filepath = "C:/users/rocca/desktop/vork/xxx.docx"; string outpath = "F:/test.pdf"; inputstream source = new fileinputstream (filepath); outputstream target = new fileoutputstream (Outpath); map<string, string> params = new HashMap<String, String> (); PdfOptions Options = pdfoptions.create(); Wordconvertertopdf (Source, target, options, params); }/** * Convert Word document, to pdf, Intermediate swap variables * @param source source for Word document, must be docx document * @param target target output * @param params variables to be replaced * @throws Exception */ public static void Wordconvertertopdf (inputstream source,outputstream target, map<string, string> params) throws exception {wordconvertertopdf (source, target, null, params);} /** * Convert Word document, to pdf, intermediate swap variable * @param source source for Word document, must beFor docx document * @param target target Output * @ param params variables that need to be replaced * @param options pdfoptions.create ( ). Fontencoding ( "windows-1250" ) or other * @throws exception */ public static void Wordconvertertopdf (inputstream source, outputstream target, Pdfoptions options, map<string, string> params) throws Exception { xwpfdocument doc = new xwpfdocument (source); paragraphreplace (Doc.getparagraphs (), params); for (xwpftable table : doc.gettables ()) { for (XWPFTABLEROW&NBSp;row : table.getrows ()) { for (xwpftablecell cell : Row.gettablecells ()) { paragraphreplace (Cell.getparagraphs (), params); } } } pdfconverter.getinstance (). CONVERT (doc, target, options); } /** replace content in paragraphs */private static void Paragraphreplace (List<xwpfparagraph> paragraphs, map<string, string> params) {if (Maputils.isnotempty (params)) {for (xwpfparagraph p : paragraphs) {for (Xwpfrun r : p.getruns ()) {String content = r.gettext (r.getTextPosition ()); Logger.info (content); if (Stringutils.isnotempty (content) && params.containskey (content)) {r.settext (Params.get (content), 0);}}}}}
Call
Response.reset (); Response.setcontenttype ("application/pdf"); String exportname = new String ((Companyfullname + Contractno + ". pdf"). GetBytes ("GBK"), "iso8859-1"); Response.setheader ("Content-disposition", "attachment; Filename= "+ exportname); InputStream input = Request.getservletcontext (). getResourceAsStream ("/web-inf/classes/"+ sourceFileName); Wordpdfutils.wordconvertertopdf (Input, Response.getoutputstream (), params);
Font installation:
Here the window on the font packaging, select the text in the line, upload to the Ubuntu system, installation is done.
Font path in Window System: C:\Windows\Fonts
Ubuntu system fonts have a number of places, I put it in:/usr/share/fonts directory, in which a win directory was built to store the uploaded Chinese font, and then execute the command: FC-CACHE-FV the font to the font cache. Then restart the app.
Ubuntu Java Word to PDF