[简单]docx4j提取word 2007图片

      见代码:

     

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map.Entry;

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;

public class Docx4j_SaveDocxImg_S3_Test {
	public static void main(String[] args) throws Exception {
		Docx4j_SaveDocxImg_S3_Test t = new Docx4j_SaveDocxImg_S3_Test();
		t.saveDocxImg("f:/saveFile/temp/word_docx4j_img_125.docx",
				"f:/saveFile/temp/docx4j_");
	}

	/**
	 * @Description: 提取word图片
	 */
	public void saveDocxImg(String filePath, String savePath) throws Exception {
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
				.load(new File(filePath));
		for (Entry<PartName, Part> entry : wordMLPackage.getParts().getParts()
				.entrySet()) {
			if (entry.getValue() instanceof BinaryPartAbstractImage) {
				BinaryPartAbstractImage binImg = (BinaryPartAbstractImage) entry
						.getValue();
				// 图片minetype
				String imgContentType = binImg.getContentType();
				PartName pt = binImg.getPartName();
				String fileName = null;
				if (pt.getName().indexOf("word/media/") != -1) {
					fileName = pt.getName().substring(
							pt.getName().indexOf("word/media/")
									+ "word/media/".length());
				}
				System.out.println(String.format("mimetype=%s,filePath=%s",
						imgContentType, pt.getName()));
				FileOutputStream fos = new FileOutputStream(savePath + fileName);
				((BinaryPart) entry.getValue()).writeDataToOutputStream(fos);
				fos.close();
			}
		}
	}
}

 

    全文完。

你可能感兴趣的:(word)