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

[简单]docx4j创建word 2007批注(comment)

 
阅读更多

        直接上代码:

       

import java.io.File;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.CTShd;
import org.docx4j.wml.CTVerticalAlignRun;
import org.docx4j.wml.Color;
import org.docx4j.wml.CommentRangeEnd;
import org.docx4j.wml.CommentRangeStart;
import org.docx4j.wml.Comments;
import org.docx4j.wml.Comments.Comment;
import org.docx4j.wml.Highlight;
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.PPr;
import org.docx4j.wml.PPrBase.Spacing;
import org.docx4j.wml.PPrBase.TextAlignment;
import org.docx4j.wml.R;
import org.docx4j.wml.RFonts;
import org.docx4j.wml.RPr;
import org.docx4j.wml.STHint;
import org.docx4j.wml.STLineSpacingRule;
import org.docx4j.wml.STShd;
import org.docx4j.wml.Text;
import org.docx4j.wml.U;
import org.docx4j.wml.UnderlineEnumeration;

public class Docx4j_创建批注_S3_Test {
	public static void main(String[] args) throws Exception {
		Docx4j_创建批注_S3_Test t = new Docx4j_创建批注_S3_Test();
		WordprocessingMLPackage wordMLPackage = t
				.createWordprocessingMLPackage();
		MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
		ObjectFactory factory = Context.getWmlObjectFactory();
		t.testCreateComment(wordMLPackage, mp, factory);
		t.saveWordPackage(wordMLPackage, new File("f:/saveFile/temp/sys_"
				+ System.currentTimeMillis() + ".docx"));
	}

	public void testCreateComment(WordprocessingMLPackage wordMLPackage,
			MainDocumentPart t, ObjectFactory factory) throws Exception {
		P p = factory.createP();
		setParagraphSpacing(factory, p, true, "0",
				"0",true, null, "100", true, "240", STLineSpacingRule.AUTO);
		t.addObject(p);
		RPr fontRPr = getRPrStyle(factory, "微软雅黑", "000000", "20",
				STHint.EAST_ASIA, false, false, false, true, UnderlineEnumeration.SINGLE,  "B61CD2",
				true, "darkYellow", false, null, null, null);
		RPr commentRPr = getRPrStyle(factory, "微软雅黑", "41A62D", "18",
				STHint.EAST_ASIA, true, true, false, false, null, null, false,
				null, false, null, null, null);
		Comments comments = addDocumentCommentsPart(wordMLPackage, factory);
		BigInteger commentId = BigInteger.valueOf(1);
		
		createCommentEnd(factory, p, "测试", "这是官网Demo", fontRPr, commentRPr, commentId, comments);
		commentId = commentId.add(BigInteger.ONE);

		createCommentRound(factory, p, "批注", "这是批注comment", fontRPr, commentRPr, commentId, comments);
		commentId = commentId.add(BigInteger.ONE);
		
		p = factory.createP();
		setParagraphSpacing(factory, p, true, "0",
				"0",true, null, "100", true, "240", STLineSpacingRule.AUTO);
		t.addObject(p);
		createCommentRound(factory, p, "批注2", "这是批注comment2", fontRPr, commentRPr, commentId, comments);
		commentId = commentId.add(BigInteger.ONE);
		
		createCommentEnd(factory, p, "测试2", "这是官网Demo", fontRPr, commentRPr, commentId, comments);
		commentId = commentId.add(BigInteger.ONE);
	}

	public void createCommentEnd(ObjectFactory factory, P p, String pContent,
			String commentContent, RPr fontRPr, RPr commentRPr,
			BigInteger commentId, Comments comments) throws Exception{
		Text txt = factory.createText();
		txt.setValue(pContent);
		R run = factory.createR();
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		p.getContent().add(run);
		Comment commentOne = createComment(factory, commentId, "系统管理员",new Date(), commentContent, commentRPr);
		comments.getComment().add(commentOne);
		p.getContent().add(createRunCommentReference(factory, commentId));
	}
	
	//创建批注(选定范围)
	public void createCommentRound(ObjectFactory factory, P p, String pContent,
			String commentContent, RPr fontRPr, RPr commentRPr,
			BigInteger commentId, Comments comments) throws Exception {
		CommentRangeStart startComment = factory.createCommentRangeStart();
		startComment.setId(commentId);
		p.getContent().add(startComment);
		R run = factory.createR();
		Text txt = factory.createText();
		txt.setValue(pContent);
		run.getContent().add(txt);
		run.setRPr(fontRPr);
		p.getContent().add(run);
		CommentRangeEnd endComment = factory.createCommentRangeEnd();
		endComment.setId(commentId);
		p.getContent().add(endComment);
		Comment commentOne = createComment(factory, commentId, "系统管理员",
				new Date(), commentContent, commentRPr);
		comments.getComment().add(commentOne);
		p.getContent().add(createRunCommentReference(factory, commentId));
	}

	public Comments addDocumentCommentsPart(
			WordprocessingMLPackage wordMLPackage, ObjectFactory factory)
			throws Exception {
		CommentsPart cp = new CommentsPart();
		wordMLPackage.getMainDocumentPart().addTargetPart(cp);
		Comments comments = factory.createComments();
		cp.setJaxbElement(comments);
		return comments;
	}

	public Comments.Comment createComment(ObjectFactory factory,
			BigInteger commentId, String author, Date date,
			String commentContent, RPr commentRPr) throws Exception {
		Comments.Comment comment = factory.createCommentsComment();
		comment.setId(commentId);
		if (author != null) {
			comment.setAuthor(author);
		}
		if (date != null) {
			comment.setDate(toXMLCalendar(date));
		}
		P commentP = factory.createP();
		comment.getEGBlockLevelElts().add(commentP);
		R commentR = factory.createR();
		commentP.getContent().add(commentR);
		Text commentText = factory.createText();
		commentR.getContent().add(commentText);
		commentR.setRPr(commentRPr);
		commentText.setValue(commentContent);
		return comment;
	}

	public R createRunCommentReference(ObjectFactory factory,
			BigInteger commentId) {
		R run = factory.createR();
		R.CommentReference commentRef = factory.createRCommentReference();
		run.getContent().add(commentRef);
		commentRef.setId(commentId);
		return run;
	}

	public XMLGregorianCalendar toXMLCalendar(Date d) throws Exception {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(d);
		XMLGregorianCalendar xml = DatatypeFactory.newInstance()
				.newXMLGregorianCalendar();
		xml.setYear(gc.get(Calendar.YEAR));
		xml.setMonth(gc.get(Calendar.MONTH) + 1);
		xml.setDay(gc.get(Calendar.DAY_OF_MONTH));
		xml.setHour(gc.get(Calendar.HOUR_OF_DAY));
		xml.setMinute(gc.get(Calendar.MINUTE));
		xml.setSecond(gc.get(Calendar.SECOND));
		return xml;
	}

	// 字体样式
	public RPr getRPrStyle(ObjectFactory factory, String fontFamily,
			String colorVal, String fontSize, STHint sTHint, boolean isBlod,
			boolean isItalic, boolean isStrike, boolean isUnderLine,
			UnderlineEnumeration underLineStyle, String underLineColor,
			boolean isHightLight, String hightLightValue, boolean isShd,
			STShd shdValue, String shdColor, CTVerticalAlignRun stRunEnum) {
		RPr rPr = factory.createRPr();
		RFonts rf = new RFonts();
		if (sTHint != null) {
			rf.setHint(sTHint);
		}
		if (fontFamily != null) {
			rf.setAscii(fontFamily);
			rf.setEastAsia(fontFamily);
			rf.setHAnsi(fontFamily);
		}
		rPr.setRFonts(rf);
		if (colorVal != null) {
			Color color = new Color();
			color.setVal(colorVal);
			rPr.setColor(color);
		}
		if (fontSize != null) {
			HpsMeasure sz = new HpsMeasure();
			sz.setVal(new BigInteger(fontSize));
			rPr.setSz(sz);
			rPr.setSzCs(sz);
		}

		BooleanDefaultTrue bdt = factory.createBooleanDefaultTrue();
		if (isBlod) {
			rPr.setB(bdt);
		}
		if (isItalic) {
			rPr.setI(bdt);
		}
		if (isStrike) {
			rPr.setStrike(bdt);
		}
		if (isUnderLine) {
			U underline = new U();
			if (underLineStyle != null) {
				underline.setVal(underLineStyle);
			}
			if (underLineColor != null) {
				underline.setColor(underLineColor);
			}
			rPr.setU(underline);
		}
		if (isHightLight) {
			Highlight hight = new Highlight();
			hight.setVal(hightLightValue);
			rPr.setHighlight(hight);
		}
		if (isShd) {
			CTShd shd = new CTShd();
			if (shdColor != null) {
				shd.setColor(shdColor);
			}
			if (shdValue != null) {
				shd.setVal(shdValue);
			}
			rPr.setShd(shd);
		}
		if (stRunEnum != null) {
			rPr.setVertAlign(stRunEnum);
		}
		return rPr;
	}

	// 段落底纹
	public void setParagraphShdStyle(ObjectFactory factory, P p, boolean isShd,
			STShd shdValue, String shdColor) {
		if (isShd) {
			PPr ppr = factory.createPPr();
			CTShd shd = new CTShd();
			if (shdColor != null) {
				shd.setColor(shdColor);
			}
			if (shdValue != null) {
				shd.setVal(shdValue);
			}
			ppr.setShd(shd);
			p.setPPr(ppr);
		}
	}

	// 段落间距
	public void setParagraphSpacing(ObjectFactory factory, P p,
			boolean isSpace, String before, String after, boolean isLines,
			String beforeLines, String afterLines, boolean isLineRule,
			String lineValue, STLineSpacingRule sTLineSpacingRule) {
		PPr pPr = p.getPPr();
		if (pPr == null) {
			pPr = factory.createPPr();
		}
		Spacing spacing = new Spacing();
		if (isSpace) {
			if (before != null) {
				// 段前磅数
				spacing.setBefore(new BigInteger(before));
			}
			if (after != null) {
				// 段后磅数
				spacing.setAfter(new BigInteger(after));
			}
		}
		if (isLines) {
			if (beforeLines != null) {
				// 段前行数
				spacing.setBeforeLines(new BigInteger(beforeLines));
			}
			if (afterLines != null) {
				// 段后行数
				spacing.setAfterLines(new BigInteger(afterLines));
			}
		}
		if (isLineRule) {
			if (lineValue != null) {
				spacing.setLine(new BigInteger(lineValue));
			}
			spacing.setLineRule(sTLineSpacingRule);
		}
		pPr.setSpacing(spacing);
		p.setPPr(pPr);
	}

	// 段落对齐方式
	public void setParagraphAlign(ObjectFactory factory, P p,
			JcEnumeration jcEnumeration, TextAlignment textAlign) {
		PPr pPr = p.getPPr();
		if (pPr == null) {
			pPr = factory.createPPr();
		}
		if (jcEnumeration != null) {
			Jc jc = pPr.getJc();
			if (jc == null) {
				jc = new Jc();
			}
			jc.setVal(jcEnumeration);
			pPr.setJc(jc);
		}
		if (textAlign != null) {
			pPr.setTextAlignment(textAlign);
		}
		p.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/2158829,  谢谢。

      全文完

 

  • 大小: 39.9 KB
0
0
分享到:
评论

相关推荐

    docx4j操作word

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

    docx4j操作word2007

    **docx4j操作word2007** 在IT领域,docx4j是一个非常实用的Java库,专为处理Microsoft Office Open XML (OOXML) 文件格式,如.docx、.xlsx和.pptx而设计。它允许开发人员在Java应用程序中创建、修改和转换这些文档...

    docx4j生成word文档

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

    Docx4j office word java

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

    用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 技术操作word的读写

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

    最新 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编程创建复杂的.docx格式的word文档

    在IT行业中,创建复杂文档是常见的需求,尤其是对于...总之,docx4j是一个强大的工具,它使得在Java环境中生成和编辑复杂Word文档变得简单。通过深入学习和实践,开发者可以充分利用这个库,满足各种文档生成的需求。

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

    docx4j提供了一系列API来操作docx文档,包括读取、创建、修改和合并。要实现多文档合并,你需要做以下步骤: 1. **读取docx文件**:使用`org.docx4j.openpackaging.packages.WordprocessingMLPackage`类加载每个...

    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应用程序中创建、修改和转换这些文件,...

    docx4j相关jar包

    **docx4j相关jar包** docx4j是一款强大的Java库,专门用于处理Microsoft Office Open XML(OOXML)格式的文档,如.docx、.xlsx和.pptx。这个库使得开发者能够在Java应用程序中创建、修改和转换这些文档,而无需依赖...

    docx4j-3.3.3.zip

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

    docx4j-3.3.5-API文档-中英对照版.zip

    赠送jar包:docx4j-3.3.5.jar; 赠送原API文档:docx4j-3.3.5-javadoc.jar; 赠送源代码:docx4j-3.3.5-sources.jar; 赠送Maven依赖信息文件:docx4j-3.3.5.pom; 包含翻译后的API文档:docx4j-3.3.5-javadoc-API...

    docx4j-3.3.5-API文档-中文版.zip

    赠送jar包:docx4j-3.3.5.jar; 赠送原API文档:docx4j-3.3.5-javadoc.jar; 赠送源代码:docx4j-3.3.5-sources.jar; 赠送Maven依赖信息文件:docx4j-3.3.5.pom; 包含翻译后的API文档:docx4j-3.3.5-javadoc-API...

    docx4j word转pdf

    标题中的"docx4j word转pdf"是指使用docx4j库将Microsoft Word的.docx格式文档转换为PDF格式。docx4j是一个开源Java库,专门用于处理OpenXML格式,如.docx、.pptx和.xlsx。这个库提供了丰富的API,允许开发者在程序...

    Demo-docx4j-word.zip

    docx4j解析word导入试题入库,支持图片解析,mathType公式解析,word自带数学公式解析

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

    html模板填充生成pdf和word

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

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

    docx4j 替换文本

    在文档处理领域,docx4j是一个非常实用的Java库,它专门用于创建、修改和转换Open XML格式的文档,如.docx、.pptx等。这个库的强大之处在于,它可以让我们在程序中高效地操作Word文档,而无需手动编辑每个细节。在本...

Global site tag (gtag.js) - Google Analytics