2011-06-01 15 views
11

apache POI का उपयोग करके ms word फ़ाइल को pdf पर कैसे परिवर्तित करें?जावा: apache POI का उपयोग करके ms शब्द फ़ाइल को पीडीएफ में कैसे परिवर्तित करें?

मैं निम्नलिखित कोड का उपयोग कर रहा हूं लेकिन यह त्रुटियों को काम नहीं कर रहा है मुझे लगता है कि मैं गलत वर्ग आयात कर रहा हूं?

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

हैलो डेनिस जब मैं PDF में मैं आयात com.lowagie.text.Document में त्रुटि fallowing गया शब्द फ़ाइल बदलने की कोशिश; आयात com.lowagie.text.DocumentException; आयात com.lowagie.text. पैराग्राफ; आयात com.lowagie.text.pdf.PdfWriter; कृपया मुझे लाइब्रेरी के साथ बताएं, मैं इसे जोड़ने के लिए भूल गया हूं अगर मुझे डाउनलोड करने के लिए एक लिंक देना संभव है – DynamicMind

उत्तर

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

लेकिन मैं समझ नहीं पा रहा हूं कि पीडीएफ दस्तावेज़ फ़ाइल की तरह उचित डिजाइन में नहीं है ..? – Harinder

+0

हैलो डेनिस जब मैं शब्द फ़ाइल को पीडीएफ में कनवर्ट करने का प्रयास करता हूं तो मुझे आयात com.lowagie.text.Document में त्रुटि गिर रही है; com.lowagie.text आयात करें। दस्तावेज़ अपवाद; com.lowagie.text आयात करें। पैराग्राफ; com.lowagie.text.pdf.PdfWriter आयात करें; कृपया मुझे लाइब्रेरी के साथ बताएं, मैं इसे जोड़ने के लिए भूल गया हूं, अगर मुझे डाउनलोड करने के लिए एक लिंक देना संभव है - डायनेमिक माइंड 0 सेकंड पहले संपादित करें – DynamicMind

+0

wiil यह एंड्रॉइड के लिए काम करता है ?? –

1

कई कदम यहां हैं:

  1. पढ़ें वर्ड दस्तावेज़ पीडीएफ में
  2. Convert प्रारूप-नास्तिक प्रपत्र एक प्रारूप-नास्तिक के रूप में POI का उपयोग कर
  3. पीडीएफ लिखें

मुझे नहीं पता कि पीओआई आपके लिए चरण 2 करेगा या नहीं। मैं iText की तरह कुछ और सिफारिश करेंगे।

+0

कृपया मेरा कोड – Harinder

+3

जांचें, आपकी प्रारंभिक पोस्ट में कोड लोगाई/आईटेक्स्ट पैकेज का उल्लेख नहीं कर रहा था। मैं पहले से ही परेशान था कि पीओआई लाइब्रेरी से संबंधित कुछ पीडीएफ कहां से ढूंढें। डफिमो सूचीबद्ध चरणों में सही है। इसी तरह की स्थिति में मैं 'वर्डएमएल' (वर्ड 2003 एक्सएमएल प्रारूप) का उपयोग करता हूं जिसे एफओ में बदल दिया जाता है और फिर अपाचे एफओपी का उपयोग करके प्रस्तुत किया जाता है। ओपनऑफिस एपीआई सहित अन्य संभावनाएं हैं। StackOverflow के माध्यम से खोजें और आपको Office2PDF के बारे में बहुत सारे प्रश्न/उत्तर मिलेंगे। – Wivani

1

हल एक तरफ ध्यान दें के रूप में, यह भी सामग्री पढ़ने के लिए संभव है बजाय फाइल सिस्टम से इसे पढ़ने और डिस्क के लिए यह serializing की वर्ड/एक्सेल सामग्री धारा से सीधे ऑन-द-मक्खी , उदाहरण के लिए जब सीएमआईएस रिपॉजिटरीज से सामग्री पुनर्प्राप्त करते हैं:

उदा।

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

(दस्तावेज़ प्रकार org.apache.chemistry.opencmis.client.api.Document की है और इस मामले में मैं opencmis के माध्यम से एक Alfresco भंडार से एक शब्द फ़ाइल को प्राप्त करने के लिए अपने कोड अनुकूलित और पीडीएफ करने के लिए इसे बदल)

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

हैलो, स्टैक ओवरफ्लो में आपका स्वागत है और आपके उत्तर के लिए धन्यवाद। कोड पोस्ट करते समय, कृपया इसे कोड के रूप में प्रदर्शित करने के लिए 4 वर्णों (या टूलबार पर कोड-स्वरूपण बटन का उपयोग करें) को इंडेंट करें (मैंने आपके लिए इसे ठीक करने के लिए एक संपादन का सुझाव दिया है)। साथ ही, कोड-केवल उत्तर यहां निराश हो जाते हैं, क्या आप अपना जवाब संपादित कर सकते हैं _why_ यह प्रश्न का उत्तर देता है? यह कॉपी-पेस्ट कोडिंग को प्रोत्साहित करने के बजाय दूसरों को सिखाने में मदद करेगा। बहुत बहुत धन्यवाद! –

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

विधि परिवर्तित –

+0

@KamilIbadov हल करने में असमर्थ: - अरे बडी उपयोग निम्नलिखित Maven निर्भरता: - \t \t org.apache.poi \t \t poi \t \t 3,13 \t \t \t \t संगठन। 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 लेटेस टी \t आप अभी भी सामना करना पड़ता है, तो किसी भी त्रुटि मुझे [email protected] –

+0

org.apache.poi.xwpf.converter.pdf.PdfConverter पर एक मेल ड्रॉप (और PdfOptions) Apache POI का हिस्सा नहीं है लेकिन xDocReport जो अपाचे पीओआई नेमस्पेस का दुरुपयोग करता है https://github.com/opensagres/xdocreport/issues/174 आजकल उनके पीडीएफ कनवर्टर पैकेज में है fr.opensagres.odfdom.converter.pdf – Fenix

संबंधित मुद्दे