Recently encountered a project need to turn Word into a PDF, Baidu has a lot of options on the Internet, such as virtual printing, to Word extension, etc., these programs rely on MS Word program, in Java code also have to use such as Jacob or jcom Java COM bridge , so that server development is limited to the win platform, and it is cumbersome to deploy. Later in a forum saw a openoffice+jodconverter conversion program, you can complete the conversion of Word to PDF work, server development needs to install OpenOffice, But the need for additional operations--needing a openoffice service on one of the ports on the server development--is a bit cumbersome and seemingly less secure to deploy.
The occasional opportunity to discover the Pageoffice component also enables Word to PDF functionality. And instead of simply converting Word to PDF, you can also support dynamically populating the data into a Word template file and then into a PDF file.
The following are the official website:
The core code that calls the Pageoffice component's FileMaker object implementation to dynamically populate the data into a Word template and convert it to PDF is as follows:
Filemakerctrl Fmctrl = new Filemakerctrl (request); Fmctrl.setserverpage (Request.getcontextpath () + "/poserver.zz"); Worddocument doc = new worddocument (); Doc.opendataregion ("Po_company"). SetValue ("A Beijing Company Limited");//Assign data to the data area, that is, fill in the corresponding position in the template fmctrl.setsavefilepage ("/ Savepdf "); Save PDF Action or Requestmapping method Fmctrl.setwriter (DOC); Fmctrl.filldocumentaspdf ("Doc/template.doc", Documentopentype.word, "123.pdf");//fill in the Word template and convert to PDF
Save the code for the action or Requestmapping method (/savepdf) of the PDF:
Filesaver fs = new Filesaver (request, response); Fs.savetofile (Request.getsession (). Getservletcontext (). Getrealpath ("Doc") + "/" + fs.getfilename ()); Fs.close ();
Sample source and Effect: http://www.zhuozhengsoft.com/dowm/, download Pageoffice for Java, unzip, copy the Samples4 folder to the WebApps directory of Tomcat, Access:/http localhost:8080/samples4/index.html, view example: Two, 34, FileMaker convert a single document to PDF (for example in Word)
[Reprint]java for Word to PDF