We need apache poi JARs to convert word file to pdf file.

If you are using maven project. Please add below dependencies in your pom.xml.

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.13</version>
</dependency>

If your are using simple java project download above jars from below mentioned path.

https://mvnrepository.com/artifact/org.apache.poi

Please find below the working code for conversion:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class Test {

/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) {
try {
String inputFile = "D:/Address.docx";
String outputFile = "D:/Address1.pdf";
if (args != null && args.length == 2) {
inputFile = args[0];
outputFile = args[1];
}
System.out.println("inputFile:" + inputFile + ",outputFile:" + outputFile);
FileInputStream in = new FileInputStream(inputFile);
XWPFDocument document = new XWPFDocument(in);
File outFile = new File(outputFile);
OutputStream out = new FileOutputStream(outFile);
PdfOptions options = null;
PdfConverter.getInstance().convert(document, out, options);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Convert Word file(.docx) to PDF file using apache poi
Tagged on:                 

2 thoughts on “Convert Word file(.docx) to PDF file using apache poi

Leave a Reply

Your email address will not be published. Required fields are marked *