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.
66 lines
2.4 KiB
Java
66 lines
2.4 KiB
Java
package com.docus.sw.word;
|
|
|
|
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
|
|
public class GetPicsDocx {
|
|
public static void main(String[] args) {
|
|
String path = "E:\\上海项目测试\\文档\\35.docx";
|
|
File file = new File(path);
|
|
try {
|
|
FileInputStream fis = new FileInputStream(file);
|
|
XWPFDocument document = new XWPFDocument(fis);
|
|
XWPFWordExtractor xwpfWordExtractor = new XWPFWordExtractor(document);
|
|
String text = xwpfWordExtractor.getText();
|
|
System.out.println(text);
|
|
List<XWPFPictureData> picList = document.getAllPictures();
|
|
for (XWPFPictureData pic : picList) {
|
|
System.out.println(pic.getPictureType() + File.separator + pic.suggestFileExtension() + File.separator + pic.getFileName());
|
|
byte[] bytev = pic.getData();
|
|
FileOutputStream fos = new FileOutputStream("E:\\上海项目测试\\docxImage\\" + pic.getFileName());
|
|
fos.write(bytev);
|
|
}
|
|
fis.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
public static void getPics(String fromPath, String toPath) {
|
|
File file = new File(fromPath);
|
|
FileInputStream fis = null;
|
|
try {
|
|
fis = new FileInputStream(file);
|
|
XWPFDocument document = new XWPFDocument(fis);
|
|
XWPFWordExtractor xwpfWordExtractor = new XWPFWordExtractor(document);
|
|
String text = xwpfWordExtractor.getText();
|
|
System.out.println(text);
|
|
List<XWPFPictureData> picList = document.getAllPictures();
|
|
int i = 1;
|
|
for (XWPFPictureData pic : picList) {
|
|
byte[] bytev = pic.getData();
|
|
FileOutputStream fos = new FileOutputStream(toPath +i+ pic.getFileName());
|
|
fos.write(bytev);
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
if (fis != null) {
|
|
try {
|
|
fis.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |