2017-03-08 3 views
1

docx 파일에서 모든 이미지를 추출하는 작업이 있습니다. 아래의 스 니펫을 동일한 용도로 사용하고 있습니다. 나는 Apache POI api를 사용하고 있습니다.JAX : docx 문서에서 푸터 이미지 추출

`File file = new File(InputFileString); 
FileInputStream fs = new FileInputStream(file.getAbsolutePath()); 
//FileInputStream fs=new FileInputStream(src); 
    //create office word 2007+ document object to wrap the word file 
    XWPFDocument doc1x=new XWPFDocument(fs); 
    //get all images from the document and store them in the list piclist 
    List<XWPFPictureData> piclist=doc1x.getAllPictures(); 
    //traverse through the list and write each image to a file 
    Iterator<XWPFPictureData> iterator=piclist.iterator(); 
    int i=0; 
    while(iterator.hasNext()){ 
    XWPFPictureData pic=iterator.next(); 
    byte[] bytepic=pic.getData(); 
    BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytepic)); 
      ImageIO.write(imag, "jpg", new File("C:/imagefromword"+i+".jpg")); 
      i++; 
    }` 

그러나이 코드는 문서의 바닥 글이나 머리글 섹션에있는 이미지를 감지 할 수 없습니다.

저는 Google 기술을 광범위하게 사용했으며 유용한 정보가 없었습니다.

어쨌든 docx 파일의 바닥 글 섹션에 이미지 파일을 캡처 할 수 있습니까?

+0

머리글과 바닥 글에 [getAllPictures] (https://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.html#getAllPictures())를 호출 해 보았습니까? – Gagravarr

답변

1

나는 Apache POI 문제에 전문가는 오전,하지만 간단한 검색은 this 코드를 내놓았다 :

package com.concretepage; 
import java.io.FileInputStream; 
import org.apache.poi.openxml4j.opc.OPCPackage; 
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; 
import org.apache.poi.xwpf.usermodel.XWPFDocument; 
import org.apache.poi.xwpf.usermodel.XWPFFooter; 
import org.apache.poi.xwpf.usermodel.XWPFHeader; 
public class ReadDOCXHeaderFooter { 
    public static void main(String[] args) { 
    try { 
    FileInputStream fis = new FileInputStream("D:/docx/read-test.docx"); 
    XWPFDocument xdoc=new XWPFDocument(OPCPackage.open(fis)); 
    XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(xdoc); 
    //read header 
    XWPFHeader header = policy.getDefaultHeader(); 
    System.out.println(header.getText()); 
    //read footer 
    XWPFFooter footer = policy.getDefaultFooter(); 
    System.out.println(footer.getText()); 
    } catch(Exception ex) { 
    ex.printStackTrace(); 
    } 
    } 
} 

그리고 XWPFFooter 클래스의 바로 아버지의 클래스에 XWPFHeaderFooter의 문서 페이지 (위의 예제 ...)는 문서 본문의 모든 그림을 반복하는 데 사용한 동일한 getAllPictures 메서드를 보여줍니다.

모바일에서 나, 그래서 나는 정말로 아무것도 테스트하지 못했지만 - 일할만큼 똑바로 보인다.

행운을 빈다.