Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java:using apache POI how to convert ms word file to pdf?

Tags:

By using apache POI how to convert ms word file to pdf?

I an using the following code but its not working giving errors I guess I am importing the wrong classes?

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream;  import org.apache.poi.hslf.record.Document; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.poifs.filesystem.POIFSFileSystem;   public class TestCon {      /**      * @param args      */     public static void main(String[] args) {         // TODO Auto-generated method stub          POIFSFileSystem fs = null;            Document document = new Document();            try {                System.out.println("Starting the test");                fs = new POIFSFileSystem(new FileInputStream("/document/test2.doc"));                 HWPFDocument doc = new HWPFDocument(fs);                WordExtractor we = new WordExtractor(doc);                 OutputStream file = new FileOutputStream(new File("/document/test.pdf"));                PdfWriter writer = PdfWriter.getInstance(document, file);                 Range range = doc.getRange();              document.open();                writer.setPageEmpty(true);                document.newPage();                writer.setPageEmpty(true);                 String[] paragraphs = we.getParagraphText();                for (int i = 0; i < paragraphs.length; i++) {                     org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);                 // CharacterRun run = pr.getCharacterRun(i);                 // run.setBold(true);                 // run.setCapitalized(true);                 // run.setItalic(true);                  paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");                System.out.println("Length:" + paragraphs[i].length());                System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());                 // add the paragraph to the document                document.add(new Paragraph(paragraphs[i]));                }                 System.out.println("Document testing completed");            } catch (Exception e) {                System.out.println("Exception during test");                e.printStackTrace();            } finally {                            // close the document               document.close();                        }            }       } 
like image 249
Harinder Avatar asked Jun 01 '11 13:06

Harinder


People also ask

How do I convert Microsoft Word to PDF?

You can use Word, PowerPoint, and OneNote for the Web to convert your doc into a PDF. Select File > Print > Print (in PowerPoint you will select one of three formats). In the dropdown menu under Printer , select Save as PDF and then select Save.

Does Apache POI support PDF?

pdf, which is much more current and works using the latest stable release apache poi 3.17 . So we should using this. But since even those newer PdfOptions and PdfConverter are not part of the apache poi project, apache poi will not testing those with their releases.


2 Answers

Got It solved

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream;  import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter;   import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.extractor.WordExtractor;  import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.poifs.filesystem.POIFSFileSystem;   public class TestCon {      /**      * @param args      */     public static void main(String[] args) {         // TODO Auto-generated method stub          POIFSFileSystem fs = null;           Document document = new Document();           try {                System.out.println("Starting the test");                fs = new POIFSFileSystem(new FileInputStream("D:/Resume.doc"));                 HWPFDocument doc = new HWPFDocument(fs);                WordExtractor we = new WordExtractor(doc);                 OutputStream file = new FileOutputStream(new File("D:/test.pdf"));                PdfWriter writer = PdfWriter.getInstance(document, file);                 Range range = doc.getRange();              document.open();                writer.setPageEmpty(true);                document.newPage();                writer.setPageEmpty(true);                 String[] paragraphs = we.getParagraphText();                for (int i = 0; i < paragraphs.length; i++) {                     org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);                 // CharacterRun run = pr.getCharacterRun(i);                 // run.setBold(true);                 // run.setCapitalized(true);                 // run.setItalic(true);                  paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");                System.out.println("Length:" + paragraphs[i].length());                System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());                 // add the paragraph to the document                document.add(new Paragraph(paragraphs[i]));                }                 System.out.println("Document testing completed");            } catch (Exception e) {                System.out.println("Exception during test");                e.printStackTrace();            } finally {                            // close the document               document.close();                        }            }       } 
like image 198
Harinder Avatar answered Sep 21 '22 11:09

Harinder


This worked For Me:-

Source :- http://www.programcreek.com/java-api-examples/index.php?api=org.apache.poi.xwpf.converter.pdf.PdfConverter

package pdf;  import java.io.File; import java.io.FileInputStream; 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 PDF {     public static void main(String[] args) throws Exception {           String inputFile="D:/TEST.docx";           String outputFile="D:/TEST.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);         } } 
like image 21
Kushagra Sahni Avatar answered Sep 20 '22 11:09

Kushagra Sahni