You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.3 KiB
Java
95 lines
3.3 KiB
Java
package com.docus.sw.word;
|
|
|
|
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.*;
|
|
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();
|
|
// 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));
|
|
pic.writeImageContent(out);
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static void readPicture(String path, String toPath) {
|
|
FileInputStream in = null;
|
|
try {
|
|
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 = null;
|
|
try {
|
|
out = new FileOutputStream(new File(toPath + i + afileName));
|
|
pic.writeImageContent(out);
|
|
} catch (FileNotFoundException e) {
|
|
e.getMessage();
|
|
} catch (IOException e) {
|
|
e.getMessage();
|
|
}finally {
|
|
if(out!= null){
|
|
try {
|
|
out.close();
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
e.getMessage();
|
|
}finally {
|
|
if(in!=null){
|
|
try {
|
|
in.close();
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
} |