Skip to content

Instantly share code, notes, and snippets.

@milindjagre
Created April 13, 2016 11:57
Show Gist options
  • Save milindjagre/36931c77e469e1aacdd9599ed5edd52b to your computer and use it in GitHub Desktop.
Save milindjagre/36931c77e469e1aacdd9599ed5edd52b to your computer and use it in GitHub Desktop.
This java file will convert word file into pdf file. Word to Pdf converter using JAVA API.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.milind.word.to.pdf;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
/**
*
* @author milind
*/
public class WordToPdf {
public static String readDocFile(String fileName) {
String output = "";
try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
HWPFDocument doc = new HWPFDocument(fis);
WordExtractor we = new WordExtractor(doc);
String[] paragraphs = we.getParagraphText();
for (String para : paragraphs) {
output = output + "\n" + para.toString() + "\n";
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
public static String readDocxFile(String fileName) {
String output = "";
try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph para : paragraphs) {
output = output + "\n" + para.getText() + "\n";
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
public static void writePdfFile(String output) throws FileNotFoundException, DocumentException {
File file = new File("D:\\itext-test.pdf");
FileOutputStream fileout = new FileOutputStream(file);
Document document = new Document();
PdfWriter.getInstance(document, fileout);
document.addAuthor("Milind");
document.addTitle("My Converted PDF");
document.open();
String[] splitter = output.split("\\n");
for (int i = 0; i < splitter.length; i++) {
Chunk chunk = new Chunk(splitter[i]);
Font font = new Font();
font.setStyle(Font.UNDERLINE);
font.setStyle(Font.ITALIC);
chunk.setFont(font);
document.add(chunk);
Paragraph paragraph = new Paragraph();
paragraph.add("");
document.add(paragraph);
}
document.close();
}
public static void main(String[] args) throws FileNotFoundException, DocumentException {
String ext = FilenameUtils.getExtension("D:\\test.docx");
String output = "";
if ("docx".equalsIgnoreCase(ext)) {
output = readDocxFile("D:\\Test.docx");
} else if ("doc".equalsIgnoreCase(ext)) {
output = readDocFile("D:\\Test.doc");
} else {
System.out.println("INVALID FILE TYPE. ONLY .doc and .docx are permitted.");
}
writePdfFile(output);
}
}
@sureshrathore309
Copy link

this program terminated automatically why

@AlanneSoares
Copy link

qual api .jar devo baixar para criar este sistema?

@ameersabith
Copy link

Please share that .jar file

@LemjidMohamed
Copy link

I need a code that converts a pdf to simple word (unstructured)

@dipaklohar
Copy link

i need all pdf to word and word to pdf

@AbrorbekAllaberganov
Copy link

i need all pdf to word and word to pdf

Did you find? If so, can you share it with me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment