2013-08-09 2 views
1

자바를 사용하여 JPEG를 Pdf로 변환했지만 Java를 사용하여 PDF로 변환하는 방법을 모르면 JPEG를 Pdf로 변환하는 코드가 아래에 나와 있습니다.자바를 사용하여 pdf를 워드 문서로 변환

누구든지 Java를 사용하여 Pdf를 Word (.doc/.docx)로 변환하는 방법을 말해 줄 수 있습니까?

import java.io.FileOutputStream; 
import com.itextpdf.text.Image; 
import com.itextpdf.text.pdf.PdfWriter; 
import com.itextpdf.text.Document; 

public class JpegToPDF { 
    public static void main(String[] args) { 
     try { 
      Document convertJpgToPdf = new Document(); 
      PdfWriter.getInstance(convertJpgToPdf, new FileOutputStream(
        "c:\\java\\ConvertImagetoPDF.pdf")); 
      convertJpgToPdf.open(); 
      Image convertJpg = Image.getInstance("c:\\java\\test.jpg"); 
      convertJpgToPdf.add(convertJpg); 
      convertJpgToPdf.close(); 
      System.out.println("Successfully Converted JPG to PDF in iText"); 
     } catch (Exception i1) { 
      i1.printStackTrace(); 
     } 
    } 
} 
+0

나는'jpeg'를'pdf'로 변환 한 것과 비슷한 라이브러리를 발견 할 것입니다. – supersam654

+0

당신이 이것을 위해 도서관을 사용하고 있다고 생각합니다. pdf를 단어로 변환 할 수있는 방법이 있는지 확인하십시오. 그렇지 않으면 ... –

+0

jpeg를 pdf로 변환하기위한 선생님 iText5.1.0.jar 라이브러리를 사용하고 있습니다 ......... 그러나 어떤 라이브러리를 PDF로 변환해야합니까? –

답변

3

사실 두 라이브러리가 필요합니다. 두 라이브러리 모두 오픈 소스입니다. 첫 번째 문자는 iText이며, PDF 파일에서 텍스트를 추출하는 데 사용됩니다. 두번째 것은 POI이고, 그것은 문서라는 단어를 만드는 데 사용됩니다.

코드

은 매우 간단합니다 :
//Create the word document 
XWPFDocument doc = new XWPFDocument(); 

// Open the pdf file 
String pdf = "myfile.pdf"; 
PdfReader reader = new PdfReader(pdf); 
PdfReaderContentParser parser = new PdfReaderContentParser(reader); 

// Read the PDF page by page 
for (int i = 1; i <= reader.getNumberOfPages(); i++) { 
    TextExtractionStrategy strategy = parser.processContent(i, new SimpleTextExtractionStrategy()); 
    // Extract the text 
    String text=strategy.getResultantText(); 
    // Create a new paragraph in the word document, adding the extracted text 
    XWPFParagraph p = doc.createParagraph(); 
    XWPFRun run = p.createRun(); 
    run.setText(text); 
    // Adding a page break 
    run.addBreak(BreakType.PAGE); 
} 
// Write the word document 
FileOutputStream out = new FileOutputStream("myfile.docx"); 
doc.write(out); 
// Close all open files 
out.close(); 
reader.close(); 

는주의 : 사용 된 추출 전략으로, 당신은 모든 서식을 잃게됩니다. 그러나 자신 만의 복잡한 추출 전략을 삽입하여이를 수정할 수 있습니다.

+0

선생님 PrintWriter out = 새 PrintWriter (새 FileOutputStream (txt)); ------------- 및 FileOutputStream outs = new FileOutputStream ("myfile.docx"); ----------- 코드 사이에 관계가 없으므로 –

+0

예외 .................. 스레드 "main"의 예외 java.lang.NoClassDefFoundError : org/apache/xmlbeans/XmlException \t at Send.Receive.converter.main (변환기 된 .java : java.lang.ClassNotFoundException가 : 56)에 의해 발생 org.apache.xmlbeans.XmlException 이는 java.net.URLClassLoader $ 1.run (URLClassLoader.java:366) \t에서 \t 이는 java.net.URLClassLoader $ 1부터. run (URLClassLoader.java:355) \t at java.security.AccessController.doPrivileged (네이티브 메소드) –

0

OpenOffice/LibreOfffice는 순수한 Java 솔루션이 아니지만 TCP 포트를 통해 OpenOffice/LibreOfffice에 연결할 수 있습니다. 그것을 사용하여 문서를 변환 할 수 있습니다. 수용 가능한 해결책 인 경우 JODConverter을 사용하면 도움이됩니다.

관련 문제