Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5976371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:13:07+00:00 2026-05-22T21:13:07+00:00

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

  • 0

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();  
                     }  
         }  
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:13:08+00:00Added an answer on May 22, 2026 at 9:13 pm

    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();  
                         }  
             }  
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, we're using the following package to programmatically create excel documents: org.apache.poi.hssf If
I'm using Apache POI 3.6, I want to read an excel file which has
I am writing an Excel File using Apache POI. I want to write in
I'm writing an app the generates a big xlsx file using apache-POI. At a
I have opened a large excel file as a Apache POI workbook (using VM
Using Apache POI HSSF, we can create xls file like this private void write(HSSFWorkbook
I am using Apache POI-HSSF for working with Excel files. I have a cell
I have been using Apache POI for quite sometime and it works great but
Can u give me the simple code snippt using org.apache.poi.ss.formula.FormulaParser. FormulaParser class having the
I have been using Apache POI to manipulate Microsoft Word .docx files — ie

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.