2011-06-01 531 views
11

通過使用apache POI如何將ms word文件轉換爲pdfJava:使用apache POI如何將ms word文件轉換爲pdf?

我使用下面的代碼,但它不工作給錯誤我想我導入錯誤的類?

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(); 
        } 
     } 
    } 
+0

您好丹尼斯當我嘗試轉換成Word文件,PDF我得到了休耕進口com.lowagie.text.Document錯誤; import com.lowagie.text.DocumentException; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter; 請告訴我與圖書館我忘了添加它也如果有可能給我一個鏈接下載 – DynamicMind 2011-08-08 11:36:22

回答

8

得到它解決

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(); 
        } 
     } 
    } 
+1

但我不能理解你的PDF是不是在像doc文件的適當設計..? – Harinder 2011-06-03 04:11:44

+0

你好丹尼斯,當我嘗試將Word文件轉換爲PDF我在導入com.lowagie.text.Document休耕錯誤; import com.lowagie.text.DocumentException; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter;請告訴我與圖書館我忘了添加它也如果可以給我一個鏈接下載 - DynamicMind 0秒前編輯 – DynamicMind 2011-08-08 11:36:59

+0

wiil它適用於Android? – 2016-12-26 11:51:16

1

這裏有幾個步驟:

    使用POI成格式無關的形式
  1. 轉換格式無關的形式轉換成PDF
  2. 閱讀Word文檔
  3. 寫PDF

我不知道POI是否會爲你做第2步。我會推薦別的東西,比如iText。

+0

plz檢查我的代碼 – Harinder 2011-06-01 13:25:03

+3

你最初的帖子中的代碼並沒有提到lowagie/iText包。我對於在POI庫中在哪裏找到相關的PDF已經感到困惑。 Duffymo在他列出的步驟中是正確的。在類似的情況下,我使用'WordML'(Word 2003 xml格式),將其轉換爲FO,然後使用Apache FOP進行渲染。還有其他可能性,包括OpenOffice API。通過StackOverflow進行搜索,您會發現許多有關Office2PDF的問題/答案。 – Wivani 2011-06-06 10:59:56

1

作爲一個側面說明,它也可以閱讀的內容直接從Word/Excel的內容流於即時的,而不是從文件系統讀取它,並將其序列化到磁盤,例如從CMIS存儲庫檢索內容時:

eg

//HWPFDocument docx = new HWPFDocument(fs); 
HWPFDocument docx = new HWPFDocument(doc.getContentStream().getStream()); 

(DOC是org.apache.chemistry.opencmis.client.api.Document型的,在這種情況下,我適應您的代碼通過opencmis的方式來檢索的露天倉庫Word文件,並將其轉換爲PDF)

HTH

1

的下面的代碼爲我工作:

Public class DocToPdfConverter{ 

public static void main(String[] args) { 

     String k=null; 
     OutputStream fileForPdf =null; 
     try { 

      String fileName="/document/test2.doc"; 
      //Below Code is for .doc file 
      if(fileName.endsWith(".doc")) 
      { 
      HWPFDocument doc = new HWPFDocument(new FileInputStream(
        fileName)); 
      WordExtractor we=new WordExtractor(doc); 
      k = we.getText(); 

      fileForPdf = new FileOutputStream(new File(
         "/document/DocToPdf.pdf")); 
      we.close(); 
      } 

      //Below Code for 

      else if(fileName.endsWith(".docx")) 
      { 
       XWPFDocument docx = new XWPFDocument(new FileInputStream(
         fileName)); 
       // using XWPFWordExtractor Class 
       XWPFWordExtractor we = new XWPFWordExtractor(docx); 
       k = we.getText(); 

       fileForPdf = new FileOutputStream(new File(
          "/document/DocxToPdf.pdf"));  
       we.close(); 
      } 



      Document document = new Document(); 
      PdfWriter.getInstance(document, fileForPdf); 

      document.open(); 

      document.add(new Paragraph(k)); 

      document.close(); 
      fileForPdf.close(); 



     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

您好,歡迎來到StackOverflow並感謝您的回答。發佈代碼時,請縮進4個字符(或使用工具欄上的代碼格式化按鈕),以確保它顯示爲代碼(我建議您修改該代碼以解決該問題)。另外,由於這裏只提供了代碼唯一的答案,你能編輯你的答案來解釋_爲什麼這會回答這個問題嗎?它會幫助教別人,而不僅僅是鼓勵複製粘貼編碼。非常感謝! – 2016-08-12 07:46:32

2

這爲我工作: -

來源: - 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); 
     } 
} 
+0

未能解決方法轉換 – 2017-05-06 14:54:04

+0

@KamilIbadov: - 嘿,夥計使用以下Maven相關性: - \t \t org.apache.poi \t \t POI \t \t 3.13 \t \t \t \t org。 apache.poi \t \t POI-OOXML \t \t 3.13 \t \t \t \t fr.opensagres.xdocreport \t \t org.apache.poi.xwpf.converter.pdf \t \t LATES牛逼 \t 如果仍然遇到任何錯誤給我發郵件,在[email protected] – 2017-05-20 22:40:59

+0

org.apache.poi.xwpf.converter.pdf.PdfConverter(和PdfOptions)是不是在Apache POI的一部分但錯誤使用Apache POI命名空間的xDocReport請參閱https://github.com/opensagres/xdocreport/issues/174如今他們的PdfConverter在包fr.opensagres.odfdom.converter.pdf – Fenix 2017-11-06 16:55:50

相關問題