Replace a simple Word document with a PDF document in Java

Source: Internet
Author: User

There are many ways to replace a simple Word document with a PDF document in Java, because many of them are not actually tested, so here's a general look.

On the whole there are two kinds of:

1. Pure Java code Implementation, there are a lot of good open source software can be used, such as poi,itext,xdocreport,docx4j and so on. The main drawback is that you can only work with simple documents

2. By installing the conversion software on the operating system, invoke the software command in the Java code to implement the conversion. The common use of Openoffice,pandoc,jacob (limited to Windows environments) and other software, the advantages of complex documents can also be handled well. The disadvantage is that there will be trouble, some can not cross the platform, the speed may also be slower

The main point here is that I use Xdocreport to convert a Word document into a PDF document, Xdocreport actually encapsulates the POI and itext, further simplifying the code. See my maven dependency below

        <!--maven dependency, just a lot--<dependency> <groupId>commons-codec</groupId> <artifactId>common-codec</artifactId> <version>1.5</version> </dependenc y> <dependency> <groupId>dom4j</groupId> <artifactid>dom4j</art            ifactid> <version>1.6.1</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactid>fr.opensagres.xdocreport.itext.exte nsion</artifactid> <version>1.0.4</version> </dependency> <dependency&            Gt <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version&gt ;2.1.7</version> </dependency> <dependency> <groupid>org.apache.poi</g Roupid> <artifactId>ooxml-schemas</artifactId> <version>1.1</version> </dependency&gt        ; <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactid>org.apa        Che.poi.xwpf.converter.core</artifactid> <version>1.0.4</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId> Org.apache.poi.xwpf.converter.pdf</artifactid> <version>1.0.4</version> </dependenc y> <dependency> <groupId>org.apache.poi</groupId> <artifactid>poi&            lt;/artifactid> <version>3.9</version> </dependency> <dependency>            <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> &lT;version>3.9</version> </dependency> <dependency> <groupid>javax.xml. Stream</groupid> <artifactId>stax-api</artifactId> <version>1.0-2</versio            n> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>3.0.0</version> </depend     Ency>

MAVEN depends on the version of a lot of older, but this is not important, can realize the function is good, if you change to other higher version may be error, in addition there may be an individual dependency is not necessary, you are interested can try it yourself. The code below

ImportOrg.apache.poi.xwpf.converter.pdf.PdfConverter;Importorg.apache.poi.xwpf.converter.pdf.PdfOptions;Importorg.apache.poi.xwpf.usermodel.XWPFDocument;ImportJava.io.*;ImportJava.util.HashMap;ImportJava.util.Map; Public classWordtopdf {/*** Convert the Word document to PDF, replace the variable in the middle *@paramSOURCE is a Word document and must be a docx document *@paramTarget Output *@throwsException*/     Public Static voidWordconvertertopdf (InputStream source, OutputStream Target)throwsException {xwpfdocument doc=Newxwpfdocument (source); Pdfoptions Options=NULL;//because it is a simple process, the parameter is set to NULL, and it is necessary to studypdfconverter.getinstance (). CONVERT (Doc, target, options); }    //Test     Public Static voidMain (string[] args) {String filepath= "F:\\temp\\test.docx"; String Outpath= "F:\\temp\\test.pdf";        InputStream source;        OutputStream Target; Try{Source=NewFileInputStream (filepath); Target=NewFileOutputStream (Outpath); Map<string, string> params =NewHashmap<string, string>();        Wordconvertertopdf (source, target); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); }    }}

This enables the ability to turn a simple Word document into a PDF document. And finally, the place to be noticed,

1. If you change the version of Maven dependency, you may get an error,

2. Note the font of Chinese characters in Word documents

Here will show you the document's Chinese character font name, some of the fonts will disappear when the conversion, can not be displayed, I know there is "Arial", note that it is different from the "Song Body."

There is a mistake welcome to point out that there is a good code to share.

Attached to the reference address: Https://github.com/opensagres/xdocreport/wiki/DocxReportingJavaMainConverter

Replace a simple Word document with a PDF document in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.