直接上代码:
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, 谢谢。
全文完。
相关推荐
docx4j是一个功能强大的Java类库,用于创建和操作Microsoft Open XML文件,如Word docx、PowerPoint pptx和Excel xlsx。今天,我们将详细介绍如何使用docx4j操作Word文档。 1. Docx4j介绍 docx4j是一个开源的Java...
**docx4j操作word2007** 在IT领域,docx4j是一个非常实用的Java库,专为处理Microsoft Office Open XML (OOXML) 文件格式,如.docx、.xlsx和.pptx而设计。它允许开发人员在Java应用程序中创建、修改和转换这些文档...
下面是一个简单的示例,演示如何使用`docx4j`根据书签替换Word文档中的内容。首先,你需要在你的项目中引入`docx4j`库,可以通过Maven或者Gradle等构建工具添加依赖。 ```xml <groupId>org.docx4j <artifactId>...
最近在做一个出卷系统,需要通过试卷模板从数据库中抽取题目,然后按照模板的样式生成完整的试卷,包括试卷格式的排版和图片的写入。这是用docx4j生成word文档。里面包括完整代码和所需jar包。
2. **创建新文档**:使用`org.docx4j.jaxb.Context`类初始化上下文,然后通过`org.docx4j.Docx4J.createDocument()`方法创建一个新的Word文档。 3. **操作文档内容**:可以使用`org.docx4j.model.content.Body`和`...
使用docx4J技术操作word的读写,使用docx4J技术操作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) ...
Docx是Microsoft Office Word 2007及以后版本中使用的默认文件格式,而PDF则是通用的、跨平台的文档格式,常用于保证文件的外观在不同设备上的一致性。本篇文章将详细介绍如何利用Java库docx4j将.docx文档转换为.pdf...
这篇博客“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提供了将Word文档(.docx)转换为PDF的能力。这一过程通常涉及解析OOXML文档结构,然后根据这些结构生成PDF文档。docx4j通过内部集成的PDF渲染引擎来完成这个任务,确保转换后的PDF尽可能地保留原始Word文档的...
在IT行业中,创建复杂文档是常见的需求,尤其是对于...总之,docx4j是一个强大的工具,它使得在Java环境中生成和编辑复杂Word文档变得简单。通过深入学习和实践,开发者可以充分利用这个库,满足各种文档生成的需求。
`docx4j`是一个强大的Java库,专门用于创建、修改和转换Microsoft Office Open XML (OOXML)文件,尤其是`.docx`格式的Word文档。这个库允许开发者在程序中对Word文档进行深度操作,如替换文本、插入图片、设置样式等...
使用docx4j时,开发者可以通过创建和操作`org.docx4j.model`包下的对象来构建或修改文档内容。例如,可以使用`WordprocessingMLPackage`来处理.docx文件,`MainDocumentPart`来获取或设置文档的主要内容,`Paragraph...
docx4j是一款强大的Java库,专为处理Microsoft Office 2007及更高版本的文件格式而设计,包括Word(.docx)、Excel(.xlsx)和PowerPoint(.pptx)文档。它允许开发者在Java应用程序中创建、修改和转换这些文件,...
**docx4j相关jar包** docx4j是一款强大的Java库,专门用于处理Microsoft Office Open XML(OOXML)格式的文档,如.docx、.xlsx和.pptx。这个库使得开发者能够在Java应用程序中创建、修改和转换这些文档,而无需依赖...
《docx4j-3.3.3:Java处理Word文档的强大工具》 在Java开发中,处理Microsoft Office格式的文档是一项常见的需求,尤其是docx格式。为了方便开发者,一个名为docx4j的开源库应运而生,它允许程序员以编程方式创建、...
html模板填充生成pdf和word
赠送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...