当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

[简单]docx4j提取word 2007图片

发表于: 2015-03-11   作者:53873039oycg   来源:转载   浏览:
摘要:       见代码:       import java.io.File; import java.io.FileOutputStream; import java.util.Map.Entry; import org.docx4j.openpackaging.packages.Wor

      见代码:

     

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();
			}
		}
	}
}

 

    全文完。

[简单]docx4j提取word 2007图片

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号