JAva POIを使用してWordドキュメントを操作し、文字と画像を書き込む

5433 ワード

まず、XWPFDocumentを継承し、画像の書き込みサイズ、位置を操作する方法を書きます.この方法がなければ、画像の書き込みは間違いませんが、Wordドキュメントを開くと、画像が見えません.
package word;

import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlToken;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;

public class CustomXWPFDocument extends XWPFDocument{
	public CustomXWPFDocument(InputStream in) throws IOException {
		super(in);
	}

	public CustomXWPFDocument() {
		super();
	}

	public CustomXWPFDocument(OPCPackage pkg) throws IOException {
		super(pkg);
	}

	/**
	 * @param id
	 * @param width
	 *             
	 * @param height
	 *             
	 * @param paragraph
	 *              
	 */
	public void createPicture(int id, int width, int height,
			XWPFParagraph paragraph) {
		final int EMU = 9525;
		width *= EMU;
		height *= EMU;
		String blipId = getAllPictures().get(id).getPackageRelationship()
				.getId();
		CTInline inline = paragraph.createRun().getCTR().addNewDrawing()
				.addNewInline();
		String picXml = ""
				+ ""
				+ "   "
				+ "      "
				+ "         " + "            "
				+ "            "
				+ "         "
				+ "         "
				+ "            "
				+ "            "
				+ "               "
				+ "            "
				+ "         "
				+ "         "
				+ "            "
				+ "               "
				+ "               "
				+ "            "
				+ "            "
				+ "               "
				+ "            "
				+ "         "
				+ "      "
				+ "   " + "";

		inline.addNewGraphic().addNewGraphicData();
		XmlToken xmlToken = null;
		try {
			xmlToken = XmlToken.Factory.parse(picXml);
		} catch (XmlException xe) {
			xe.printStackTrace();
		}
		inline.set(xmlToken);

		inline.setDistT(0);
		inline.setDistB(0);
		inline.setDistL(0);
		inline.setDistR(0);

		CTPositiveSize2D extent = inline.addNewExtent();
		extent.setCx(width);
		extent.setCy(height);

		CTNonVisualDrawingProps docPr = inline.addNewDocPr();
		docPr.setId(id);
		docPr.setName("    ");
		docPr.setDescr("    ");
	}
}

ステップ2、具体的な操作、文字と画像の書き込み:
package word;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class Test {
	public static void main(String[] args) throws Exception{
		exportSimpleWord();
	}

	public static void exportSimpleWord() throws Exception {
		CustomXWPFDocument document = new CustomXWPFDocument();
		String path = "D://  ";
		FileOutputStream out = new FileOutputStream(new File(path + ".doc"));
		
		//     
		XWPFParagraph titleParagraph = document.createParagraph();
		//       
		titleParagraph.setAlignment(ParagraphAlignment.CENTER);
		XWPFRun titleRun = titleParagraph.createRun();
		titleRun.setText("Java  word  ,       ");
		titleRun.setFontSize(20);
		titleRun.setFontFamily("  ");
		titleRun.setBold(true);

		XWPFParagraph firstParagraph = document.createParagraph();
		XWPFRun firstRun = firstParagraph.createRun();
		firstRun.setText("      :");
		firstRun.setFontFamily("  ");
		firstRun.setFontSize(11);
		//  
		firstParagraph.setWordWrap(true);
		
		XWPFParagraph twoParagraph = document.createParagraph();
		twoParagraph.setIndentationFirstLine(500);
		XWPFRun twoRun = twoParagraph.createRun();
		twoRun.setFontFamily("  ");
		twoRun.setFontSize(11);
		twoRun.setText("  POI  Word  XWPFDocument。");
		
		FileInputStream in = new FileInputStream("D://logo.jpg");
		byte[] ba = new byte[in.available()];
		in.read(ba);
		ByteArrayInputStream byteInputStream = new ByteArrayInputStream(ba);
		XWPFParagraph picture = document.createParagraph();
		//    
		document.addPictureData(byteInputStream, CustomXWPFDocument.PICTURE_TYPE_JPEG);
		//    、  
		document.createPicture(document.getAllPictures().size() - 1, 100, 100, picture);
		
		document.write(out);
		out.close();
	}
}