`
53873039oycg
  • 浏览: 843883 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

[原创]使用docx4j创建word 2007(完结贴)

 
阅读更多

       接上一篇博文,修改的地方:
       1,新增段落段前段后间隔设置,段行距设置
       2,页眉页脚底部下划线可配置
       3,表格新增合并行和列,可设置表格宽度
       4,可设置表格边框和横竖水平线样式
       5,可设置超链接字体样式
       6,可设置段落边框样式

      效果如下

     

 

 

 

      最近不在写docx4j博文,大家也看厌了,上代码

     

import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.docx4j.XmlUtils;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.jaxb.Context;
import org.docx4j.model.properties.table.tr.TrHeight;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.relationships.Relationship;
import org.docx4j.utils.BufferUtil;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.Br;
import org.docx4j.wml.CTBorder;
import org.docx4j.wml.CTHeight;
import org.docx4j.wml.CTShd;
import org.docx4j.wml.CTVerticalJc;
import org.docx4j.wml.Color;
import org.docx4j.wml.Drawing;
import org.docx4j.wml.FldChar;
import org.docx4j.wml.FooterReference;
import org.docx4j.wml.Ftr;
import org.docx4j.wml.Hdr;
import org.docx4j.wml.HdrFtrRef;
import org.docx4j.wml.HeaderReference;
import org.docx4j.wml.HpsMeasure;
import org.docx4j.wml.Jc;
import org.docx4j.wml.JcEnumeration;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.P;
import org.docx4j.wml.P.Hyperlink;
import org.docx4j.wml.PPr;
import org.docx4j.wml.PPrBase.Ind;
import org.docx4j.wml.PPrBase.PBdr;
import org.docx4j.wml.PPrBase.Spacing;
import org.docx4j.wml.ParaRPr;
import org.docx4j.wml.R;
import org.docx4j.wml.RFonts;
import org.docx4j.wml.RPr;
import org.docx4j.wml.STBorder;
import org.docx4j.wml.STBrType;
import org.docx4j.wml.STFldCharType;
import org.docx4j.wml.STHint;
import org.docx4j.wml.STLineSpacingRule;
import org.docx4j.wml.STVerticalJc;
import org.docx4j.wml.SectPr;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.TblBorders;
import org.docx4j.wml.TblGrid;
import org.docx4j.wml.TblGridCol;
import org.docx4j.wml.TblPr;
import org.docx4j.wml.TblWidth;
import org.docx4j.wml.Tc;
import org.docx4j.wml.TcPr;
import org.docx4j.wml.TcPrInner.HMerge;
import org.docx4j.wml.TcPrInner.VMerge;
import org.docx4j.wml.Text;
import org.docx4j.wml.Tr;
import org.docx4j.wml.TrPr;
import org.docx4j.wml.U;
import org.docx4j.wml.UnderlineEnumeration;

public class Docx4J_例子2 {
	public static void main(String[] args) throws Exception {
		Docx4J_例子2 t = new Docx4J_例子2();
		WordprocessingMLPackage wordMLPackage = t
				.createWordprocessingMLPackage();
		MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
		ObjectFactory factory = Context.getWmlObjectFactory();

		Relationship relationship = t.createHeaderPart(wordMLPackage, mp,
				factory, false, "3");
		relationship = t.createTextHeaderPart(wordMLPackage, mp, factory,
				"我是页眉,独乐乐不如众乐乐", true, "3", JcEnumeration.CENTER);
		t.addParagraphTest(wordMLPackage, mp, factory);
		t.addPageBreak(wordMLPackage, factory, STBrType.PAGE);
		t.createHeaderReference(wordMLPackage, mp, factory, relationship);
		t.createNormalTableTest(wordMLPackage, mp, factory);
		t.addPageBreak(wordMLPackage, factory, STBrType.TEXT_WRAPPING);
		t.createTableTest(wordMLPackage, mp, factory);
		t.addPageBreak(wordMLPackage, factory, STBrType.TEXT_WRAPPING);
		P paragraph=factory.createP();
		CTBorder topBorder=new CTBorder() ;
		topBorder.setSpace(new BigInteger("1"));
		topBorder.setSz(new BigInteger("2"));
		topBorder.setVal(STBorder.WAVE);
		t.createParagraghLine(wordMLPackage, mp, factory, paragraph, topBorder, topBorder, topBorder, topBorder);
		mp.addObject(paragraph);
		t.createHyperlink(wordMLPackage, mp, factory,paragraph,
				"mailto:1329186624@qq.com?subject=docx4j测试", "联系我","微软雅黑","24",JcEnumeration.CENTER);
		
		// 页脚
		// relationship = t.createFooterPart(wordMLPackage, mp, factory,
		// false,"3");
		// relationship = t.createTextFooterPart(wordMLPackage, mp,
		// factory,"我是页脚", true, "3", JcEnumeration.CENTER);
		relationship = t.createFooterPageNumPart(wordMLPackage, mp, factory,
				false, "3", JcEnumeration.CENTER);
		t.createFooterReference(wordMLPackage, mp, factory, relationship);
		t.saveWordPackage(wordMLPackage, new File(
				"f:/saveFile/temp/s7_simple.docx"));
	}

	public void addParagraphTest(WordprocessingMLPackage wordMLPackage,
			MainDocumentPart t, ObjectFactory factory) throws Exception {
		RPr titleRPr = getRPr(factory, "黑体", "000000", "30", STHint.EAST_ASIA,
				true, false, false, false);
		RPr boldRPr = getRPr(factory, "宋体", "000000", "24", STHint.EAST_ASIA,
				true, false, false, false);
		RPr fontRPr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA,
				false, false, false, false);
		P paragraph = factory.createP();
		Text txt = factory.createText();
		R run = factory.createR();

		txt.setValue("七年级上册Unit2 This is just a test. sectionA测试卷答题卡");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(titleRPr);
		paragraph.getContent().add(run);
		t.addObject(paragraph);

		paragraph = factory.createP();
		setParagraphSpacing(factory, paragraph, JcEnumeration.CENTER, true,
				"0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
		txt = factory.createText();
		txt.setValue("班级:________    姓名:________");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("一、单选题");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(boldRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", "200", "100", true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("1.下列有关仪器用途的说法错误的是(    )");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("A.烧杯用于较多量试剂的反应容器");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("B.烧杯用于较多量试剂的反应容器");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("C.烧杯用于较多量试剂的反应容器");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("D.烧杯用于较多量试剂的反应容器");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("2.下列实验操作中,正确的是(    ) ");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		// 段前8磅 段后0.5磅
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true,
				"160", "10", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		File file = new File("f:/saveFile/temp/image1.png");
		java.io.InputStream is = new java.io.FileInputStream(file);
		createImageParagraph(wordMLPackage, factory, paragraph, "img_1", "A.",
				BufferUtil.getBytesFromInputStream(is), JcEnumeration.LEFT);

		file = new File("f:/saveFile/temp/image2.png");
		is = new java.io.FileInputStream(file);
		createImageParagraph(wordMLPackage, factory, paragraph, "img_2",
				StringUtils.leftPad("B.", 20),
				BufferUtil.getBytesFromInputStream(is), JcEnumeration.LEFT);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		file = new File("f:/saveFile/temp/image3.png");
		is = new java.io.FileInputStream(file);
		createImageParagraph(wordMLPackage, factory, paragraph, "img_3", "C.",
				BufferUtil.getBytesFromInputStream(is), JcEnumeration.LEFT);

		file = new File("f:/saveFile/temp/image4.png");
		is = new java.io.FileInputStream(file);
		createImageParagraph(wordMLPackage, factory, paragraph, "img_4",
				StringUtils.leftPad("D.", 20),
				BufferUtil.getBytesFromInputStream(is), JcEnumeration.LEFT);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		addPageBreak(wordMLPackage, factory, STBrType.PAGE);
		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试首行缩进");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		setParagraphInd(factory, paragraph, JcEnumeration.LEFT, true, "200",
				false, null);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试悬挂缩进");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		setParagraphInd(factory, paragraph, JcEnumeration.LEFT, false, null,
				true, "200");
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试段前2行单倍行距");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", "200", null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试段后1行单倍行距");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, "100", true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试单倍行距");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试段前2行段后2行单倍行距");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", "200", "200", true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试段前10磅段后10磅段单倍行距");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true,
				"200", "200", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试段前10磅段后10磅段前2行段后2行单倍行距");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true,
				"200", "200", "200", "200", true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试段后12磅单倍行距");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"240", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试单倍行距");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试行距300[实际为多倍行距1.25]");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "300", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试1.5倍行距");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "360", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试2倍行距");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "480", STLineSpacingRule.AUTO);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试最小值 12磅");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AT_LEAST);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试固定值12磅");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.EXACT);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试固定值13磅");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "260", STLineSpacingRule.EXACT);
		t.addObject(paragraph);

		paragraph = factory.createP();
		txt = factory.createText();
		txt.setValue("测试多倍行距3倍");
		run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);
		setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0",
				"0", null, null, true, "720", STLineSpacingRule.AUTO);
		t.addObject(paragraph);
	}

	public void createNormalTableTest(WordprocessingMLPackage wordMLPackage,
			MainDocumentPart t, ObjectFactory factory) throws Exception {
		RPr titleRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA,
				true, false, false, false);
		RPr contentRpr = getRPr(factory, "宋体", "000000", "22",
				STHint.EAST_ASIA, false, false, false, false);
		Tbl table = factory.createTbl();
		CTBorder topBorder = new CTBorder();
		topBorder.setColor("80C687");
		topBorder.setVal(STBorder.DOUBLE);
		topBorder.setSz(new BigInteger("2"));
		CTBorder leftBorder = new CTBorder();
		leftBorder.setVal(STBorder.NONE);
		leftBorder.setSz(new BigInteger("0"));

		CTBorder hBorder = new CTBorder();
		hBorder.setVal(STBorder.SINGLE);
		hBorder.setSz(new BigInteger("1"));

		addBorders(table, topBorder, topBorder, leftBorder, leftBorder,
				hBorder, null);

		double[] colWidthPercent = new double[] { 15, 20, 20, 20, 25 };// 百分比
		setTableGridCol(wordMLPackage, factory, table, 80, colWidthPercent);

		Tr titleRow = factory.createTr();
		setTableTrHeight(factory, titleRow, "500");
		addTableCell(factory, wordMLPackage, titleRow, "序号", titleRpr,
				JcEnumeration.CENTER, true, "C6D9F1");
		addTableCell(factory, wordMLPackage, titleRow, "姓甚", titleRpr,
				JcEnumeration.CENTER, true, "C6D9F1");
		addTableCell(factory, wordMLPackage, titleRow, "名谁", titleRpr,
				JcEnumeration.CENTER, true, "C6D9F1");
		addTableCell(factory, wordMLPackage, titleRow, "籍贯", titleRpr,
				JcEnumeration.CENTER, true, "C6D9F1");
		addTableCell(factory, wordMLPackage, titleRow, "营生", titleRpr,
				JcEnumeration.CENTER, true, "C6D9F1");
		table.getContent().add(titleRow);

		for (int i = 0; i < 10; i++) {
			Tr contentRow = factory.createTr();
			addTableCell(factory, wordMLPackage, contentRow, i + "",
					contentRpr, JcEnumeration.CENTER, false, null);
			addTableCell(factory, wordMLPackage, contentRow, "无名氏", contentRpr,
					JcEnumeration.CENTER, false, null);
			addTableCell(factory, wordMLPackage, contentRow, "佚名", contentRpr,
					JcEnumeration.CENTER, false, null);
			addTableCell(factory, wordMLPackage, contentRow, "武林", contentRpr,
					JcEnumeration.CENTER, false, null);
			addTableCell(factory, wordMLPackage, contentRow, "吟诗赋曲",
					contentRpr, JcEnumeration.CENTER, false, null);
			table.getContent().add(contentRow);
		}
		setTableAlign(factory, table, JcEnumeration.CENTER);
		t.addObject(table);
	}

	public void createTableTest(WordprocessingMLPackage wordMLPackage,
			MainDocumentPart t, ObjectFactory factory) throws Exception {
		RPr titleRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA,
				true, false, false, false);
		RPr contentRpr = getRPr(factory, "宋体", "000000", "22",
				STHint.EAST_ASIA, false, false, false, false);
		Tbl table = factory.createTbl();
		addBorders(table, "2");

		double[] colWidthPercent = new double[] { 15, 20, 20, 20, 25 };// 百分比
		setTableGridCol(wordMLPackage, factory, table, 100, colWidthPercent);
		List<String> columnList = new ArrayList<String>();
		columnList.add("序号");
		columnList.add("姓名信息|姓甚|名谁");
		columnList.add("名刺信息|籍贯|营生");
		addTableTitleCell(factory, wordMLPackage, table, columnList, titleRpr,
				JcEnumeration.CENTER, true, "C6D9F1");

		for (int i = 0; i < 10; i++) {
			Tr contentRow = factory.createTr();
			addTableCell(factory, wordMLPackage, contentRow, i + "",
					contentRpr, JcEnumeration.CENTER, false, null);
			addTableCell(factory, wordMLPackage, contentRow, "无名氏", contentRpr,
					JcEnumeration.CENTER, false, null);
			addTableCell(factory, wordMLPackage, contentRow, "佚名", contentRpr,
					JcEnumeration.CENTER, false, null);
			addTableCell(factory, wordMLPackage, contentRow, "武林", contentRpr,
					JcEnumeration.CENTER, false, null);
			addTableCell(factory, wordMLPackage, contentRow, "吟诗赋曲",
					contentRpr, JcEnumeration.CENTER, false, null);
			table.getContent().add(contentRow);
		}
		setTableAlign(factory, table, JcEnumeration.CENTER);
		t.addObject(table);
	}

	// 设置段间距-->行距 段前段后距离
	// 段前段后可以设置行和磅 行距只有磅
	// 段前磅值和行值同时设置,只有行值起作用
	// TODO 1磅=20 1行=100 单倍行距=240 为什么是这个值不知道
	/**
	 * @param jcEnumeration
	 *            对齐方式
	 * @param isSpace
	 *            是否设置段前段后值
	 * @param before
	 *            段前磅数
	 * @param after
	 *            段后磅数
	 * @param beforeLines
	 *            段前行数
	 * @param afterLines
	 *            段后行数
	 * @param isLine
	 *            是否设置行距
	 * @param lineValue
	 *            行距值
	 * @param sTLineSpacingRule
	 *            自动auto 固定exact 最小 atLeast
	 */
	public void setParagraphSpacing(ObjectFactory factory, P p,
			JcEnumeration jcEnumeration, boolean isSpace, String before,
			String after, String beforeLines, String afterLines,
			boolean isLine, String lineValue,
			STLineSpacingRule sTLineSpacingRule) {
		PPr pPr = p.getPPr();
		if (pPr == null) {
			pPr = factory.createPPr();
		}
		Jc jc = pPr.getJc();
		if (jc == null) {
			jc = new Jc();
		}
		jc.setVal(jcEnumeration);
		pPr.setJc(jc);

		Spacing spacing = new Spacing();
		if (isSpace) {
			if (before != null) {
				// 段前磅数
				spacing.setBefore(new BigInteger(before));
			}
			if (after != null) {
				// 段后磅数
				spacing.setAfter(new BigInteger(after));
			}
			if (beforeLines != null) {
				// 段前行数
				spacing.setBeforeLines(new BigInteger(beforeLines));
			}
			if (afterLines != null) {
				// 段后行数
				spacing.setAfterLines(new BigInteger(afterLines));
			}
		}
		if (isLine) {
			if (lineValue != null) {
				spacing.setLine(new BigInteger(lineValue));
			}
			spacing.setLineRule(sTLineSpacingRule);
		}
		pPr.setSpacing(spacing);
		p.setPPr(pPr);
	}

	// 设置缩进 同时设置为true,则为悬挂缩进
	public void setParagraphInd(ObjectFactory factory, P p,
			JcEnumeration jcEnumeration, boolean firstLine,
			String firstLineValue, boolean hangLine, String hangValue) {
		PPr pPr = p.getPPr();
		if (pPr == null) {
			pPr = factory.createPPr();
		}
		Jc jc = pPr.getJc();
		if (jc == null) {
			jc = new Jc();
		}
		jc.setVal(jcEnumeration);
		pPr.setJc(jc);

		Ind ind = pPr.getInd();
		if (ind == null) {
			ind = new Ind();
		}
		if (firstLine) {
			if (firstLineValue != null) {
				ind.setFirstLineChars(new BigInteger(firstLineValue));
			}
		}
		if (hangLine) {
			if (hangValue != null) {
				ind.setHangingChars(new BigInteger(hangValue));
			}
		}
		pPr.setInd(ind);
		p.setPPr(pPr);
	}

	/**
	 * 创建字体
	 * 
	 * @param isBlod
	 *            粗体
	 * @param isUnderLine
	 *            下划线
	 * @param isItalic
	 *            斜体
	 * @param isStrike
	 *            删除线
	 */
	public RPr getRPr(ObjectFactory factory, String fontFamily,
			String colorVal, String fontSize, STHint sTHint, boolean isBlod,
			boolean isUnderLine, boolean isItalic, boolean isStrike) {
		RPr rPr = factory.createRPr();
		RFonts rf = new RFonts();
		rf.setHint(sTHint);
		rf.setAscii(fontFamily);
		rf.setHAnsi(fontFamily);
		rPr.setRFonts(rf);

		BooleanDefaultTrue bdt = factory.createBooleanDefaultTrue();
		rPr.setBCs(bdt);
		if (isBlod) {
			rPr.setB(bdt);
		}
		if (isItalic) {
			rPr.setI(bdt);
		}
		if (isStrike) {
			rPr.setStrike(bdt);
		}
		if (isUnderLine) {
			U underline = new U();
			underline.setVal(UnderlineEnumeration.SINGLE);
			rPr.setU(underline);
		}

		Color color = new Color();
		color.setVal(colorVal);
		rPr.setColor(color);

		HpsMeasure sz = new HpsMeasure();
		sz.setVal(new BigInteger(fontSize));
		rPr.setSz(sz);
		rPr.setSzCs(sz);
		return rPr;
	}

	// 文字页面
	public Relationship createTextHeaderPart(
			WordprocessingMLPackage wordprocessingMLPackage,
			MainDocumentPart t, ObjectFactory factory, String content,
			boolean isUnderLine, String underLineSz, JcEnumeration jcEnumeration)
			throws Exception {
		HeaderPart headerPart = new HeaderPart();
		Relationship rel = t.addTargetPart(headerPart);
		headerPart.setJaxbElement(getTextHdr(wordprocessingMLPackage, factory,
				headerPart, content, isUnderLine, underLineSz, jcEnumeration));
		return rel;
	}

	// 文字页脚
	public Relationship createTextFooterPart(
			WordprocessingMLPackage wordprocessingMLPackage,
			MainDocumentPart t, ObjectFactory factory, String content,
			boolean isUnderLine, String underLineSz, JcEnumeration jcEnumeration)
			throws Exception {
		FooterPart footerPart = new FooterPart();
		Relationship rel = t.addTargetPart(footerPart);
		footerPart.setJaxbElement(getTextFtr(wordprocessingMLPackage, factory,
				footerPart, content, isUnderLine, underLineSz, jcEnumeration));
		return rel;
	}

	// 图片页眉
	public Relationship createHeaderPart(
			WordprocessingMLPackage wordprocessingMLPackage,
			MainDocumentPart t, ObjectFactory factory, boolean isUnderLine,
			String underLineSize) throws Exception {
		HeaderPart headerPart = new HeaderPart();
		Relationship rel = t.addTargetPart(headerPart);
		// After addTargetPart, so image can be added properly
		headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, factory,
				headerPart, isUnderLine, underLineSize));
		return rel;
	}

	// 图片页脚
	public Relationship createFooterPart(
			WordprocessingMLPackage wordprocessingMLPackage,
			MainDocumentPart t, ObjectFactory factory, boolean isUnderLine,
			String underLineSize) throws Exception {
		FooterPart footerPart = new FooterPart();
		Relationship rel = t.addTargetPart(footerPart);
		footerPart.setJaxbElement(getFtr(wordprocessingMLPackage, factory,
				footerPart, isUnderLine, underLineSize));
		return rel;
	}

	public Relationship createFooterPageNumPart(
			WordprocessingMLPackage wordprocessingMLPackage,
			MainDocumentPart t, ObjectFactory factory, boolean isUnderLine,
			String underLineSz, JcEnumeration jcEnumeration) throws Exception {
		FooterPart footerPart = new FooterPart();
		footerPart.setPackage(wordprocessingMLPackage);
		footerPart.setJaxbElement(createFooterWithPageNr(
				wordprocessingMLPackage, factory, isUnderLine, underLineSz,
				jcEnumeration));
		return t.addTargetPart(footerPart);
	}

	public Ftr createFooterWithPageNr(
			WordprocessingMLPackage wordprocessingMLPackage,
			ObjectFactory factory, boolean isUnderLine, String underLineSz,
			JcEnumeration jcEnumeration) throws Exception {
		Ftr ftr = factory.createFtr();
		P paragraph = factory.createP();
		RPr fontRPr = getRPr(factory, "宋体", "000000", "20", STHint.EAST_ASIA,
				false, false, false, false);
		R run = factory.createR();
		run.setRPr(fontRPr);
		paragraph.getContent().add(run);

		addPageTextField(factory, paragraph, "第");
		addFieldBegin(factory, paragraph);
		addPageNumberField(factory, paragraph);
		addFieldEnd(factory, paragraph);
		addPageTextField(factory, paragraph, "页");

		addPageTextField(factory, paragraph, " 总共");
		addFieldBegin(factory, paragraph);
		addTotalPageNumberField(factory, paragraph);
		addFieldEnd(factory, paragraph);
		addPageTextField(factory, paragraph, "页");
		setParagraphSpacing(factory, paragraph, jcEnumeration, true, "0", "0",
				null, null, true, "240", STLineSpacingRule.AUTO);
		PPr pPr = paragraph.getPPr();
		if (pPr == null) {
			pPr = factory.createPPr();
		}
		Jc jc = pPr.getJc();
		if (jc == null) {
			jc = new Jc();
		}
		jc.setVal(jcEnumeration);
		pPr.setJc(jc);
		if (isUnderLine) {
			PBdr pBdr = pPr.getPBdr();
			if (pBdr == null) {
				pBdr = factory.createPPrBasePBdr();
			}
			CTBorder value = new CTBorder();
			value.setVal(STBorder.SINGLE);
			value.setColor("000000");
			value.setSpace(new BigInteger("0"));
			value.setSz(new BigInteger(underLineSz));
			pBdr.setBetween(value);
			pPr.setPBdr(pBdr);
			paragraph.setPPr(pPr);
		}
		ftr.getContent().add(paragraph);
		if (isUnderLine) {
			ftr.getContent().add(
					createHeaderBlankP(wordprocessingMLPackage, factory,
							underLineSz, jcEnumeration));
		}
		return ftr;
	}

	public void addFieldBegin(ObjectFactory factory, P paragraph) {
		R run = factory.createR();
		FldChar fldchar = factory.createFldChar();
		fldchar.setFldCharType(STFldCharType.BEGIN);
		run.getContent().add(fldchar);
		paragraph.getContent().add(run);
	}

	public void addFieldEnd(ObjectFactory factory, P paragraph) {
		FldChar fldcharend = factory.createFldChar();
		fldcharend.setFldCharType(STFldCharType.END);
		R run3 = factory.createR();
		run3.getContent().add(fldcharend);
		paragraph.getContent().add(run3);
	}

	public void addPageNumberField(ObjectFactory factory, P paragraph) {
		R run = factory.createR();
		Text txt = new Text();
		txt.setSpace("preserve");
		txt.setValue("PAGE  \\* MERGEFORMAT ");
		run.getContent().add(factory.createRInstrText(txt));
		paragraph.getContent().add(run);
	}

	public void addTotalPageNumberField(ObjectFactory factory, P paragraph) {
		R run = factory.createR();
		Text txt = new Text();
		txt.setSpace("preserve");
		txt.setValue("NUMPAGES  \\* MERGEFORMAT ");
		run.getContent().add(factory.createRInstrText(txt));
		paragraph.getContent().add(run);
	}

	private void addPageTextField(ObjectFactory factory, P paragraph,
			String value) {
		R run = factory.createR();
		Text txt = new Text();
		txt.setSpace("preserve");
		txt.setValue(value);
		run.getContent().add(txt);
		paragraph.getContent().add(run);
	}

	public void createHeaderReference(
			WordprocessingMLPackage wordprocessingMLPackage,
			MainDocumentPart t, ObjectFactory factory, Relationship relationship)
			throws InvalidFormatException {
		List<SectionWrapper> sections = wordprocessingMLPackage
				.getDocumentModel().getSections();
		SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
		// There is always a section wrapper, but it might not contain a sectPr
		if (sectPr == null) {
			sectPr = factory.createSectPr();
			t.addObject(sectPr);
			sections.get(sections.size() - 1).setSectPr(sectPr);
		}
		HeaderReference headerReference = factory.createHeaderReference();
		headerReference.setId(relationship.getId());
		headerReference.setType(HdrFtrRef.DEFAULT);
		sectPr.getEGHdrFtrReferences().add(headerReference);
	}

	public void createFooterReference(
			WordprocessingMLPackage wordprocessingMLPackage,
			MainDocumentPart t, ObjectFactory factory, Relationship relationship)
			throws InvalidFormatException {
		List<SectionWrapper> sections = wordprocessingMLPackage
				.getDocumentModel().getSections();
		SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
		// There is always a section wrapper, but it might not contain a sectPr
		if (sectPr == null) {
			sectPr = factory.createSectPr();
			t.addObject(sectPr);
			sections.get(sections.size() - 1).setSectPr(sectPr);
		}
		FooterReference footerReference = factory.createFooterReference();
		footerReference.setId(relationship.getId());
		footerReference.setType(HdrFtrRef.DEFAULT);
		sectPr.getEGHdrFtrReferences().add(footerReference);
	}

	public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage,
			ObjectFactory factory, Part sourcePart, String content,
			boolean isUnderLine, String underLineSz, JcEnumeration jcEnumeration)
			throws Exception {
		Hdr hdr = factory.createHdr();
		P headP = factory.createP();
		Text text = factory.createText();
		text.setValue(content);
		R run = factory.createR();
		run.getContent().add(text);
		headP.getContent().add(run);

		PPr pPr = headP.getPPr();
		if (pPr == null) {
			pPr = factory.createPPr();
		}
		Jc jc = pPr.getJc();
		if (jc == null) {
			jc = new Jc();
		}
		jc.setVal(jcEnumeration);
		pPr.setJc(jc);

		if (isUnderLine) {
			PBdr pBdr = pPr.getPBdr();
			if (pBdr == null) {
				pBdr = factory.createPPrBasePBdr();
			}
			CTBorder value = new CTBorder();
			value.setVal(STBorder.SINGLE);
			value.setColor("000000");
			value.setSpace(new BigInteger("0"));
			value.setSz(new BigInteger(underLineSz));
			pBdr.setBetween(value);
			pPr.setPBdr(pBdr);
			headP.setPPr(pPr);
		}
		setParagraphSpacing(factory, headP, jcEnumeration, true, "0", "0",
				null, null, true, "240", STLineSpacingRule.AUTO);
		hdr.getContent().add(headP);
		if (isUnderLine) {
			hdr.getContent().add(
					createHeaderBlankP(wordprocessingMLPackage, factory,
							underLineSz, jcEnumeration));
		}
		return hdr;
	}

	public Ftr getTextFtr(WordprocessingMLPackage wordprocessingMLPackage,
			ObjectFactory factory, Part sourcePart, String content,
			boolean isUnderLine, String underLineSz, JcEnumeration jcEnumeration)
			throws Exception {
		Ftr ftr = factory.createFtr();
		P footerP = factory.createP();
		Text text = factory.createText();
		text.setValue(content);
		R run = factory.createR();
		run.getContent().add(text);
		footerP.getContent().add(run);

		PPr pPr = footerP.getPPr();
		if (pPr == null) {
			pPr = factory.createPPr();
		}
		Jc jc = pPr.getJc();
		if (jc == null) {
			jc = new Jc();
		}
		jc.setVal(jcEnumeration);
		pPr.setJc(jc);
		footerP.setPPr(pPr);
		setParagraphSpacing(factory, footerP, jcEnumeration, true, "0", "0",
				null, null, true, "240", STLineSpacingRule.AUTO);
		if (isUnderLine) {
			PBdr pBdr = pPr.getPBdr();
			if (pBdr == null) {
				pBdr = factory.createPPrBasePBdr();
			}
			CTBorder value = new CTBorder();
			value.setVal(STBorder.SINGLE);
			value.setColor("000000");
			value.setSpace(new BigInteger("0"));
			value.setSz(new BigInteger(underLineSz));
			pBdr.setBetween(value);
			pPr.setPBdr(pBdr);
			footerP.setPPr(pPr);
		}
		ftr.getContent().add(footerP);
		if (isUnderLine) {
			ftr.getContent().add(
					createHeaderBlankP(wordprocessingMLPackage, factory,
							underLineSz, jcEnumeration));
		}
		return ftr;
	}

	public Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
			ObjectFactory factory, Part sourcePart, boolean isUnderLine,
			String underLineSize) throws Exception {
		Hdr hdr = factory.createHdr();
		File file = new File("f:/saveFile/tmp/xxt.jpg");
		java.io.InputStream is = new java.io.FileInputStream(file);
		hdr.getContent().add(
				newImage(wordprocessingMLPackage, factory, sourcePart,
						BufferUtil.getBytesFromInputStream(is), "filename",
						"这是页眉部分", 1, 2, isUnderLine, underLineSize,
						JcEnumeration.CENTER));
		if (isUnderLine) {
			hdr.getContent().add(
					createHeaderBlankP(wordprocessingMLPackage, factory,
							underLineSize, JcEnumeration.CENTER));
		}
		return hdr;
	}

	public Ftr getFtr(WordprocessingMLPackage wordprocessingMLPackage,
			ObjectFactory factory, Part sourcePart, boolean isUnderLine,
			String underLineSz) throws Exception {
		Ftr ftr = factory.createFtr();
		File file = new File("f:/saveFile/tmp/xxt.jpg");
		java.io.InputStream is = new java.io.FileInputStream(file);
		ftr.getContent().add(
				newImage(wordprocessingMLPackage, factory, sourcePart,
						BufferUtil.getBytesFromInputStream(is), "filename",
						"这是页脚", 1, 2, isUnderLine, underLineSz,
						JcEnumeration.CENTER));
		if (isUnderLine) {
			ftr.getContent().add(
					createHeaderBlankP(wordprocessingMLPackage, factory,
							underLineSz, JcEnumeration.CENTER));
		}
		return ftr;
	}

	// 段落中插入文字和图片
	public P createImageParagraph(WordprocessingMLPackage wordMLPackage,
			ObjectFactory factory, P p, String fileName, String content,
			byte[] bytes, JcEnumeration jcEnumeration) throws Exception {
		BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
				.createImagePart(wordMLPackage, bytes);
		Inline inline = imagePart.createImageInline(fileName, "这是图片", 1, 2,
				false);
		Text text = factory.createText();
		text.setValue(content);
		text.setSpace("preserve");
		R run = factory.createR();
		p.getContent().add(run);
		run.getContent().add(text);
		Drawing drawing = factory.createDrawing();
		run.getContent().add(drawing);
		drawing.getAnchorOrInline().add(inline);
		PPr pPr = p.getPPr();
		if (pPr == null) {
			pPr = factory.createPPr();
		}
		Jc jc = pPr.getJc();
		if (jc == null) {
			jc = new Jc();
		}
		jc.setVal(jcEnumeration);
		pPr.setJc(jc);
		p.setPPr(pPr);
		setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null,
				null, true, "240", STLineSpacingRule.AUTO);
		return p;
	}

	public P newImage(WordprocessingMLPackage wordMLPackage,
			ObjectFactory factory, Part sourcePart, byte[] bytes,
			String filenameHint, String altText, int id1, int id2,
			boolean isUnderLine, String underLineSize,
			JcEnumeration jcEnumeration) throws Exception {
		BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
				.createImagePart(wordMLPackage, sourcePart, bytes);
		Inline inline = imagePart.createImageInline(filenameHint, altText, id1,
				id2, false);
		P p = factory.createP();
		R run = factory.createR();
		p.getContent().add(run);
		Drawing drawing = factory.createDrawing();
		run.getContent().add(drawing);
		drawing.getAnchorOrInline().add(inline);
		PPr pPr = p.getPPr();
		if (pPr == null) {
			pPr = factory.createPPr();
		}
		Jc jc = pPr.getJc();
		if (jc == null) {
			jc = new Jc();
		}
		jc.setVal(jcEnumeration);
		pPr.setJc(jc);
		p.setPPr(pPr);

		if (isUnderLine) {
			PBdr pBdr = pPr.getPBdr();
			if (pBdr == null) {
				pBdr = factory.createPPrBasePBdr();
			}
			CTBorder value = new CTBorder();
			value.setVal(STBorder.SINGLE);
			value.setColor("000000");
			value.setSpace(new BigInteger("0"));
			value.setSz(new BigInteger(underLineSize));
			pBdr.setBetween(value);
			pPr.setPBdr(pBdr);
		}
		setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null,
				null, true, "240", STLineSpacingRule.AUTO);
		return p;
	}

	public P createHeaderBlankP(WordprocessingMLPackage wordMLPackage,
			ObjectFactory factory, String underLineSz,
			JcEnumeration jcEnumeration) throws Exception {
		P p = factory.createP();
		R run = factory.createR();
		p.getContent().add(run);
		PPr pPr = p.getPPr();
		if (pPr == null) {
			pPr = factory.createPPr();
		}
		Jc jc = pPr.getJc();
		if (jc == null) {
			jc = new Jc();
		}
		jc.setVal(jcEnumeration);
		pPr.setJc(jc);

		PBdr pBdr = pPr.getPBdr();
		if (pBdr == null) {
			pBdr = factory.createPPrBasePBdr();
		}
		CTBorder value = new CTBorder();
		value.setVal(STBorder.SINGLE);
		value.setColor("000000");
		value.setSpace(new BigInteger("0"));
		value.setSz(new BigInteger(underLineSz));
		pBdr.setBetween(value);
		pPr.setPBdr(pBdr);
		p.setPPr(pPr);
		setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null,
				null, true, "240", STLineSpacingRule.AUTO);
		return p;
	}

	// 分页
	public void addPageBreak(WordprocessingMLPackage wordMLPackage,
			ObjectFactory factory, STBrType sTBrType) {
		MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
		Br breakObj = new Br();
		breakObj.setType(sTBrType);
		P paragraph = factory.createP();
		paragraph.getContent().add(breakObj);
		documentPart.addObject(paragraph);
	}

	// 得到页面宽度
	public int getWritableWidth(WordprocessingMLPackage wordPackage)
			throws Exception {
		return wordPackage.getDocumentModel().getSections().get(0)
				.getPageDimensions().getWritableWidthTwips();
	}

	// 设置整列宽度
	/**
	 * @param tableWidthPercent
	 *            表格占页面宽度百分比
	 * @param widthPercent
	 *            各列百分比
	 */
	public void setTableGridCol(WordprocessingMLPackage wordPackage,
			ObjectFactory factory, Tbl table, double tableWidthPercent,
			double[] widthPercent) throws Exception {
		int width = getWritableWidth(wordPackage);
		int tableWidth = (int) (width * tableWidthPercent / 100);
		TblGrid tblGrid = factory.createTblGrid();
		for (int i = 0; i < widthPercent.length; i++) {
			TblGridCol gridCol = factory.createTblGridCol();
			gridCol.setW(BigInteger.valueOf((long) (tableWidth
					* widthPercent[i] / 100)));
			tblGrid.getGridCol().add(gridCol);
		}
		table.setTblGrid(tblGrid);

		TblPr tblPr = table.getTblPr();
		if (tblPr == null) {
			tblPr = factory.createTblPr();
		}
		TblWidth tblWidth = new TblWidth();
		tblWidth.setType("dxa");// 这一行是必须的,不自己设置宽度默认是auto
		tblWidth.setW(new BigInteger(tableWidth + ""));
		tblPr.setTblW(tblWidth);
		table.setTblPr(tblPr);
	}

	// 表格水平对齐方式
	public void setTableAlign(ObjectFactory factory, Tbl table,
			JcEnumeration jcEnumeration) {
		TblPr tablePr = table.getTblPr();
		if (tablePr == null) {
			tablePr = factory.createTblPr();
		}
		Jc jc = tablePr.getJc();
		if (jc == null) {
			jc = new Jc();
		}
		jc.setVal(jcEnumeration);
		tablePr.setJc(jc);
		table.setTblPr(tablePr);
	}

	// 表格增加边框
	public void addBorders(Tbl table, String borderSize) {
		table.setTblPr(new TblPr());
		CTBorder border = new CTBorder();
		border.setColor("auto");
		border.setSz(new BigInteger(borderSize));
		border.setSpace(new BigInteger("0"));
		border.setVal(STBorder.SINGLE);
		TblBorders borders = new TblBorders();
		borders.setBottom(border);
		borders.setLeft(border);
		borders.setRight(border);
		borders.setTop(border);
		borders.setInsideH(border);
		borders.setInsideV(border);
		table.getTblPr().setTblBorders(borders);
	}

	// 表格增加边框 可以设置上下左右四个边框样式以及横竖水平线样式
	public void addBorders(Tbl table, CTBorder topBorder,
			CTBorder bottomBorder, CTBorder leftBorder, CTBorder rightBorder,
			CTBorder hBorder, CTBorder vBorder) {
		table.setTblPr(new TblPr());
		TblBorders borders = new TblBorders();
		borders.setBottom(bottomBorder);
		borders.setLeft(leftBorder);
		borders.setRight(rightBorder);
		borders.setTop(bottomBorder);
		borders.setInsideH(hBorder);
		borders.setInsideV(vBorder);
		table.getTblPr().setTblBorders(borders);
	}

	// 设置tr高度
	public void setTableTrHeight(ObjectFactory factory, Tr tr, String heigth) {
		TrPr trPr = tr.getTrPr();
		if (trPr == null) {
			trPr = factory.createTrPr();
		}
		CTHeight ctHeight = new CTHeight();
		ctHeight.setVal(new BigInteger(heigth));
		TrHeight trHeight = new TrHeight(ctHeight);
		trHeight.set(trPr);
		tr.setTrPr(trPr);
	}

	// 新增单元格
	public void addTableCell(ObjectFactory factory,
			WordprocessingMLPackage wordMLPackage, Tr tableRow, String content,
			RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor,
			String backgroudColor) {
		Tc tableCell = factory.createTc();
		P p = factory.createP();
		setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null,
				null, true, "240", STLineSpacingRule.AUTO);
		Text t = factory.createText();
		t.setValue(content);
		R run = factory.createR();
		// 设置表格内容字体样式
		run.setRPr(rpr);

		TcPr tcPr = tableCell.getTcPr();
		if (tcPr == null) {
			tcPr = factory.createTcPr();
		}

		CTVerticalJc valign = factory.createCTVerticalJc();
		valign.setVal(STVerticalJc.CENTER);
		tcPr.setVAlign(valign);

		run.getContent().add(t);
		p.getContent().add(run);
		tableCell.getContent().add(p);
		if (hasBgColor) {
			CTShd shd = tcPr.getShd();
			if (shd == null) {
				shd = factory.createCTShd();
			}
			shd.setColor("auto");
			shd.setFill(backgroudColor);
			tcPr.setShd(shd);
		}
		tableCell.setTcPr(tcPr);
		tableRow.getContent().add(tableCell);
	}

	// 新增单元格
	public void addTableTitleCell(ObjectFactory factory,
			WordprocessingMLPackage wordMLPackage, Tbl table,
			List<String> titleList, RPr rpr, JcEnumeration jcEnumeration,
			boolean hasBgColor, String backgroudColor) {
		Tr firstTr = factory.createTr();
		Tr secordTr = factory.createTr();
		setTableTrHeight(factory, firstTr, "200");
		setTableTrHeight(factory, secordTr, "200");
		table.getContent().add(firstTr);
		table.getContent().add(secordTr);
		for (String str : titleList) {
			if (str.indexOf("|") == -1) {
				createNormalCell(factory, firstTr, str, rpr, jcEnumeration,
						hasBgColor, backgroudColor, false, "restart");
				createNormalCell(factory, secordTr, "", rpr, jcEnumeration,
						hasBgColor, backgroudColor, false, null);
			} else {
				String[] cols = str.split("\\|");
				createNormalCell(factory, firstTr, cols[0], rpr, jcEnumeration,
						hasBgColor, backgroudColor, true, "restart");
				for (int i = 1; i < cols.length - 1; i++) {
					createNormalCell(factory, firstTr, "", rpr, jcEnumeration,
							hasBgColor, backgroudColor, true, null);
				}
				for (int i = 1; i < cols.length; i++) {
					createNormalCell(factory, secordTr, cols[i], rpr,
							jcEnumeration, hasBgColor, backgroudColor, true,
							null);
				}
			}
		}
	}

	public void createNormalCell(ObjectFactory factory, Tr tr, String content,
			RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor,
			String backgroudColor, boolean isHMerger, String mergeVal) {
		Tc tableCell = factory.createTc();
		P p = factory.createP();
		setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null,
				null, true, "240", STLineSpacingRule.AUTO);
		Text t = factory.createText();
		t.setValue(content);
		R run = factory.createR();
		// 设置表格内容字体样式
		run.setRPr(rpr);
		TcPr tcPr = tableCell.getTcPr();
		if (tcPr == null) {
			tcPr = factory.createTcPr();
		}
		CTVerticalJc valign = factory.createCTVerticalJc();
		valign.setVal(STVerticalJc.CENTER);
		tcPr.setVAlign(valign);
		if (isHMerger) {
			HMerge merge = new HMerge();
			if (mergeVal != null) {
				merge.setVal(mergeVal);
			}
			tcPr.setHMerge(merge);
		} else {
			VMerge merge = new VMerge();
			if (mergeVal != null) {
				merge.setVal(mergeVal);
			}
			tcPr.setVMerge(merge);
		}
		run.getContent().add(t);
		p.getContent().add(run);
		tableCell.getContent().add(p);
		if (hasBgColor) {
			CTShd shd = tcPr.getShd();
			if (shd == null) {
				shd = factory.createCTShd();
			}
			shd.setColor("auto");
			shd.setFill(backgroudColor);
			tcPr.setShd(shd);
		}
		tableCell.setTcPr(tcPr);
		tr.getContent().add(tableCell);
	}

	  public void createParagraghLine(WordprocessingMLPackage wordMLPackage,
				MainDocumentPart t, ObjectFactory factory,P p,CTBorder topBorder,CTBorder bottomBorder,CTBorder leftBorder,CTBorder rightBorder){
			PPr ppr=new PPr();
			PBdr pBdr=new PBdr();
			pBdr.setTop(topBorder);
			pBdr.setBottom(bottomBorder);
			pBdr.setLeft(leftBorder);
			pBdr.setRight(rightBorder);
			ppr.setPBdr(pBdr);
			p.setPPr(ppr);
		}
	 
	public void createHyperlink(WordprocessingMLPackage wordMLPackage,
			MainDocumentPart t, ObjectFactory factory,P paragraph, String url,
			String value, String fontName, String fontSize,JcEnumeration jcEnumeration) throws Exception {
		org.docx4j.relationships.ObjectFactory reFactory = new org.docx4j.relationships.ObjectFactory();
		org.docx4j.relationships.Relationship rel = reFactory
				.createRelationship();
		rel.setType(Namespaces.HYPERLINK);
		rel.setTarget(url);
		rel.setTargetMode("External");
		t.getRelationshipsPart().addRelationship(rel);
		StringBuffer sb = new StringBuffer();
		// addRelationship sets the rel's @Id
		sb.append("<w:hyperlink r:id=\"");
		sb.append(rel.getId());
		sb.append("\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ");
		sb.append("xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >");
		sb.append("<w:r><w:rPr><w:rStyle w:val=\"Hyperlink\" />");
		sb.append("<w:rFonts  w:ascii=\"");
		sb.append(fontName);
		sb.append("\"  w:hAnsi=\"");
		sb.append(fontName);
		sb.append("\"  w:eastAsia=\"");
		sb.append(fontName);
		sb.append("\" w:hint=\"eastAsia\"/>");
		sb.append("<w:sz w:val=\"");
		sb.append(fontSize);
		sb.append("\"/><w:szCs w:val=\"");
		sb.append(fontSize);
		sb.append("\"/></w:rPr><w:t>");
		sb.append(value);
		sb.append("</w:t></w:r></w:hyperlink>");

		Hyperlink link = (Hyperlink) XmlUtils.unmarshalString(sb.toString());
		paragraph.getContent().add(link);
		setParagraphSpacing(factory, paragraph, jcEnumeration, true, "0",
				"0", null, null, true, "240", STLineSpacingRule.AUTO);
		PPr ppr = paragraph.getPPr();
		if (ppr == null) {
			ppr = factory.createPPr();
		}
		RFonts fonts = new RFonts();
		fonts.setAscii(fontName);
		fonts.setHAnsi(fontName);
		fonts.setEastAsia(fontName);
		fonts.setHint(STHint.EAST_ASIA);
		ParaRPr rpr = new ParaRPr();
		rpr.setRFonts(fonts);
		ppr.setRPr(rpr);
		paragraph.setPPr(ppr);
	}

	public WordprocessingMLPackage createWordprocessingMLPackage()
			throws Exception {
		return WordprocessingMLPackage.createPackage();
	}

	public void saveWordPackage(WordprocessingMLPackage wordPackage, File file)
			throws Exception {
		wordPackage.save(file);
	}

}

 

    转载请带上原链接,原链接http://53873039oycg.iteye.com/blog/2123815谢谢

    全文完

  

 

  • 大小: 61.6 KB
  • 大小: 46.7 KB
  • 大小: 100.1 KB
  • 大小: 73.6 KB
0
0
分享到:
评论

相关推荐

    docx4j操作word

    docx4j是一个功能强大的Java类库,用于创建和操作Microsoft Open XML文件,如Word docx、PowerPoint pptx和Excel xlsx。今天,我们将详细介绍如何使用docx4j操作Word文档。 1. Docx4j介绍 docx4j是一个开源的Java...

    使用docx4j 技术操作word的读写

    使用docx4J技术操作word的读写,使用docx4J技术操作word的读写,

    docx4j操作word2007

    本文将深入探讨如何使用docx4j来操作Word 2007(即.docx)文件,以及这个过程中的关键知识点。 1. **docx4j简介** docx4j是一个开源项目,其目标是提供一套强大的API,用于处理Open XML文档。它支持读取、写入和...

    Docx4j office word java

    2. **创建新文档**:使用`org.docx4j.jaxb.Context`类初始化上下文,然后通过`org.docx4j.Docx4J.createDocument()`方法创建一个新的Word文档。 3. **操作文档内容**:可以使用`org.docx4j.model.content.Body`和`...

    docx4j生成word文档

    最近在做一个出卷系统,需要通过试卷模板从数据库中抽取题目,然后按照模板的样式生成完整的试卷,包括试卷格式的排版和图片的写入。这是用docx4j生成word文档。里面包括完整代码和所需jar包。

    docx4j根据书签替换word中的内容

    本篇将详细介绍如何使用`docx4j`根据书签来替换Word文档中的内容。 首先,我们需要理解什么是书签。在Word文档中,书签是一种标记,可以用来标识文本或段落,以便稍后进行引用或操作。在`docx4j`中,我们可以通过...

    用docx4j操作word书签,在word书签中插入文本

    用docx4j,jar出去word书签,在word书签中插入文本 源码可以通过以下链接查看: https://github.com/xulp-mzl/xlp-core https://github.com/xulp-mzl/xlp-third ...实例: public static void main(String[] args) ...

    使用docx4j编程创建复杂的.docx格式的word文档

    这篇博客文章“使用docx4j编程创建复杂的.docx格式的word文档”提供了关于如何使用这个库的详细指南。 docx4j是一个开源项目,它的主要优点在于提供了一套丰富的API,用于操作文档的内容、样式、表格、图表、图片等...

    docx4j所需jar包全

    - 在使用docx4j时,确保你的项目已经正确地添加了所有必要的jar包,包括docx4j的主库和任何额外的转换模块。 - 由于docx4j依赖于OpenJDK的JavaFX库来进行PDF和HTML的生成,所以你需要确认Java环境配置正确且包含这些...

    docx4j 动态生成表格 (一 )

    这篇博客“docx4j 动态生成表格 (一 )”将引导我们深入理解如何利用docx4j库来创建和操作Word文档中的表格。 首先,让我们了解docx4j的基本概念。docx4j是一个开源项目,它允许开发者通过编程方式创建、修改和...

    最新 docx4j-master

    最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master...

    docx4j及其依赖包

    `docx4j`是一个强大的Java库,专门用于创建、修改和转换Microsoft Office Open XML (OOXML)文件,尤其是`.docx`格式的Word文档。这个库允许开发者在程序中对Word文档进行深度操作,如替换文本、插入图片、设置样式等...

    docx4j项目(javadoc文档、源码及示例)

    docx4j是一款强大的Java库,专为处理Microsoft Office 2007及更高版本的文件格式而设计,包括Word(.docx)、Excel(.xlsx)和PowerPoint(.pptx)文档。它允许开发者在Java应用程序中创建、修改和转换这些文件,...

    java实现多个docx文档合并(基于docx4j)

    本文将详细介绍如何使用Java和docx4j库来实现这一功能。docx4j是一个强大的开源库,专门用于处理Microsoft Office Open XML (OOXML) 文件格式,如.docx。 首先,你需要确保你的开发环境是JDK 1.7或更高版本,并且...

    使用docx4j+docx4j-ImportXHTML实现将html转成word

    html模板填充生成pdf和word

    docx4j所有jar包

    使用docx4j时,开发者可以通过创建和操作`org.docx4j.model`包下的对象来构建或修改文档内容。例如,可以使用`WordprocessingMLPackage`来处理.docx文件,`MainDocumentPart`来获取或设置文档的主要内容,`Paragraph...

    docx4j相关jar包

    5. **测试和示例**:压缩包中可能还包括了测试用例和示例代码,帮助开发者理解和使用docx4j,例如`docx4j-samples.jar`。 在实际开发中,将这些jar包添加到项目的类路径(classpath)是至关重要的,这样才能确保...

    docx4j全部jar包,包含docx4j-3.2.1.jar、poi-3.14.jar等60个jar包。.zip

    使用docx4j,开发者可以执行以下任务: - 创建新的.docx文档,或者基于现有模板创建新文档。 - 修改现有文档,包括添加、删除或编辑段落、表格、图片等元素。 - 导出文档到其他格式,如PDF、HTML或纯文本。 - 转换....

    docx4j-3.3.3.zip

    《docx4j-3.3.3:Java处理Word文档的强大工具》 在Java开发中,处理Microsoft Office格式的文档是一项常见的需求,尤其是docx格式。为了方便开发者,一个名为docx4j的开源库应运而生,它允许程序员以编程方式创建、...

    使用docx4j将图片签名插入到word中书签位置

    压缩包里包含了必要的docx4j.jar、commons-io-1.4.jar、slf4j-api-1.7.17.jar、freemrkker.jar等架包,导入到到项目后重新build path,然后运行InsertPicture.java文件,(ps:文件的参数替换成自己本地的),便可以...

Global site tag (gtag.js) - Google Analytics