2015-10-20 40 views
0

我有一個簡單的Word文件,我想用documents4j api將其轉換爲PDF。一直在尋找幾個小時,但還沒有找到如何去編寫代碼。我只需要一個基本的工作代碼。在pom.xml中使用documents4j將Word文件簡單轉換爲PDF格式的明確示例?

+0

你有沒有試圖解決這個問題?如果你有,請編輯你的問題,包括你的代碼和研究,以顯示什麼不適合你。如果你還沒有,你應該先嚐試自己解決它,然後在這裏發佈代碼和研究。它讓你的問題更容易讓別人回答! – SuperBiasedMan

回答

2

添加所需的依賴

即,

 <dependency> 
      <groupId>com.documents4j</groupId> 
      <artifactId>documents4j-api</artifactId> 
      <version>0.2.1</version> 
     </dependency>  

     <dependency> 
      <groupId>com.documents4j</groupId> 
      <artifactId>documents4j-util-conversion</artifactId> 
      <version>0.2.1</version> 
     </dependency> 

     <dependency> 
      <groupId>com.documents4j</groupId> 
      <artifactId>documents4j-transformer</artifactId> 
      <version>0.2.1</version> 
     </dependency> 

     <dependency> 
      <groupId>com.documents4j</groupId> 
      <artifactId>documents4j-util-all</artifactId> 
      <version>0.2.1</version> 
     </dependency> 

     <dependency> 
      <groupId>com.documents4j</groupId> 
      <artifactId>documents4j-local</artifactId> 
      <version>0.2.1</version> 
     </dependency> 

     <dependency> 
      <groupId>com.documents4j</groupId> 
      <artifactId>documents4j-local-demo</artifactId> 
      <version>0.2.1</version> 
     </dependency> 

而且使用後以文檔轉換成PDF的代碼片段。 在下面的示例中,我將RTF文件轉換爲PDF。

 ByteArrayOutputStream bo = new ByteArrayOutputStream(); 

     InputStream in = new BufferedInputStream(new FileInputStream("C:\\PDF\\RichText1.rtf")); 
     IConverter converter = LocalConverter.builder() 
              .baseFolder(new File("C:\\PDF\\")) 
              .workerPool(20, 25, 2, TimeUnit.SECONDS) 
              .processTimeout(5, TimeUnit.SECONDS) 
              .build(); 

     Future<Boolean> conversion = converter 
             .convert(in).as(DocumentType.RTF) 
             .to(bo).as(DocumentType.PDF) 
             .prioritizeWith(1000) // optional 
             .schedule(); 

因此,您需要的轉換後的文件將存儲在此處的ByteArrayOutputStream對象(bo)中。

相關問題