|
|
|
@ -1,38 +1,40 @@
|
|
|
|
|
package com.docus.sw.word;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.hwpf.HWPFDocument;
|
|
|
|
|
import org.apache.poi.hwpf.model.PicturesTable;
|
|
|
|
|
import org.apache.poi.hwpf.usermodel.CharacterRun;
|
|
|
|
|
import org.apache.poi.hwpf.usermodel.Picture;
|
|
|
|
|
import org.apache.poi.hwpf.usermodel.Range;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
public class ReadImgDoc {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
new ReadImgDoc().readPicture("C:\\Users\\zhanghai\\Desktop\\桌面\\test\\a.doc");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void readPicture(String path)throws Exception{
|
|
|
|
|
FileInputStream in=new FileInputStream(new File(path));
|
|
|
|
|
HWPFDocument doc=new HWPFDocument(in);
|
|
|
|
|
int length=doc.characterLength();
|
|
|
|
|
PicturesTable pTable=doc.getPicturesTable();
|
|
|
|
|
private void readPicture(String path) throws Exception {
|
|
|
|
|
FileInputStream in = new FileInputStream(new File(path));
|
|
|
|
|
HWPFDocument doc = new HWPFDocument(in);
|
|
|
|
|
int length = doc.characterLength();
|
|
|
|
|
PicturesTable pTable = doc.getPicturesTable();
|
|
|
|
|
// int TitleLength=doc.getSummaryInformation().getTitle().length();
|
|
|
|
|
|
|
|
|
|
// System.out.println(TitleLength);
|
|
|
|
|
// System.out.println(length);
|
|
|
|
|
for (int i=0;i<length;i++){
|
|
|
|
|
Range range=new Range(i, i+1,doc);
|
|
|
|
|
|
|
|
|
|
CharacterRun cr=range.getCharacterRun(0);
|
|
|
|
|
if(pTable.hasPicture(cr)){
|
|
|
|
|
Picture pic=pTable.extractPicture(cr, false);
|
|
|
|
|
String afileName=pic.suggestFullFileName();
|
|
|
|
|
OutputStream out=new FileOutputStream(new File("C:\\Users\\zhanghai\\Desktop\\桌面\\test\\"+UUID.randomUUID()+afileName));
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
Range range = new Range(i, i + 1, doc);
|
|
|
|
|
|
|
|
|
|
CharacterRun cr = range.getCharacterRun(0);
|
|
|
|
|
if (pTable.hasPicture(cr)) {
|
|
|
|
|
Picture pic = pTable.extractPicture(cr, false);
|
|
|
|
|
String afileName = pic.suggestFullFileName();
|
|
|
|
|
OutputStream out = new FileOutputStream(new File("C:\\Users\\zhanghai\\Desktop\\桌面\\test\\" + UUID.randomUUID() + afileName));
|
|
|
|
|
pic.writeImageContent(out);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -40,4 +42,28 @@ public class ReadImgDoc {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void readPicture(String path, String toPath) throws Exception {
|
|
|
|
|
FileInputStream in = new FileInputStream(new File(path));
|
|
|
|
|
HWPFDocument doc = new HWPFDocument(in);
|
|
|
|
|
int length = doc.characterLength();
|
|
|
|
|
PicturesTable pTable = doc.getPicturesTable();
|
|
|
|
|
// int TitleLength=doc.getSummaryInformation().getTitle().length();
|
|
|
|
|
|
|
|
|
|
// System.out.println(TitleLength);
|
|
|
|
|
// System.out.println(length);
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
Range range = new Range(i, i + 1, doc);
|
|
|
|
|
CharacterRun cr = range.getCharacterRun(0);
|
|
|
|
|
if (pTable.hasPicture(cr)) {
|
|
|
|
|
Picture pic = pTable.extractPicture(cr, false);
|
|
|
|
|
String afileName = pic.suggestFullFileName();
|
|
|
|
|
OutputStream out = new FileOutputStream(new File(toPath + i + afileName));
|
|
|
|
|
pic.writeImageContent(out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|