Java源码示例:org.docx4j.wml.Text
示例1
/**
* @Description: 添加图片到段落
*/
public static void addImageToPara(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, P paragraph,
String filePath, String content, RPr rpr, String altText, int id1, int id2) throws Exception {
R run = factory.createR();
if (content != null) {
Text text = factory.createText();
text.setValue(content);
text.setSpace("preserve");
run.setRPr(rpr);
run.getContent().add(text);
}
InputStream is = new FileInputStream(filePath);
byte[] bytes = IOUtils.toByteArray(is);
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
Inline inline = imagePart.createImageInline(filePath, altText, id1, id2, false);
Drawing drawing = factory.createDrawing();
drawing.getAnchorOrInline().add(inline);
run.getContent().add(drawing);
paragraph.getContent().add(run);
}
示例2
public void addStyling(Tc tableCell, String content, boolean bold, String fontSize) {
P paragraph = factory.createP();
Text text = factory.createText();
text.setValue(content);
R run = factory.createR();
run.getContent().add(text);
paragraph.getContent().add(run);
RPr runProperties = factory.createRPr();
if (bold) {
addBoldStyle(runProperties);
}
if (fontSize != null && !fontSize.isEmpty()) {
setFontSize(runProperties, fontSize);
}
run.setRPr(runProperties);
tableCell.getContent().add(paragraph);
}
示例3
/**
* @Description: 添加图片到段落
*/
public void addImageToPara(WordprocessingMLPackage wordMLPackage,
ObjectFactory factory, P paragraph, String filePath,
String content, RPr rpr, String altText, int id1, int id2)
throws Exception {
R run = factory.createR();
if (content != null) {
Text text = factory.createText();
text.setValue(content);
text.setSpace("preserve");
run.setRPr(rpr);
run.getContent().add(text);
}
InputStream is = new FileInputStream(filePath);
byte[] bytes = IOUtils.toByteArray(is);
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
.createImagePart(wordMLPackage, bytes);
Inline inline = imagePart.createImageInline(filePath, altText, id1,
id2, false);
Drawing drawing = factory.createDrawing();
drawing.getAnchorOrInline().add(inline);
run.getContent().add(drawing);
paragraph.getContent().add(run);
}
示例4
/**
* 设置单元格内容
*
* @param tc
* @param content
*/
public static void setNewTcContent(Tc tc, String content) {
P p = factory.createP();
tc.getContent().add(p);
R run = factory.createR();
p.getContent().add(run);
if (content != null) {
String[] contentArr = content.split("\n");
Text text = factory.createText();
text.setSpace("preserve");
text.setValue(contentArr[0]);
run.getContent().add(text);
for (int i = 1, len = contentArr.length; i < len; i++) {
Br br = factory.createBr();
run.getContent().add(br);// 换行
text = factory.createText();
text.setSpace("preserve");
text.setValue(contentArr[i]);
run.getContent().add(text);
}
}
}
示例5
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));
}
示例6
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;
}
示例7
public Ftr getTextFtr(WordprocessingMLPackage wordprocessingMLPackage,
ObjectFactory factory, Part sourcePart, String content,
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);
ftr.getContent().add(footerP);
return ftr;
}
示例8
/**
* 这里我们添加实际的样式信息, 首先创建一个段落, 然后创建以单元格内容作为值的文本对象;
* 第三步, 创建一个被称为运行块的对象, 它是一块或多块拥有共同属性的文本的容器, 并将文本对象添加
* 到其中. 随后我们将运行块R添加到段落内容中.
* 直到现在我们所做的还没有添加任何样式, 为了达到目标, 我们创建运行块属性对象并给它添加各种样式.
* 这些运行块的属性随后被添加到运行块. 最后段落被添加到表格的单元格中.
*/
private static void addStyling(Tc tableCell, String content, boolean bold, String fontSize) {
P paragraph = factory.createP();
Text text = factory.createText();
text.setValue(content);
R run = factory.createR();
run.getContent().add(text);
paragraph.getContent().add(run);
RPr runProperties = factory.createRPr();
if (bold) {
addBoldStyle(runProperties);
}
if (fontSize != null && !fontSize.isEmpty()) {
setFontSize(runProperties, fontSize);
}
run.setRPr(runProperties);
tableCell.getContent().add(paragraph);
}
示例9
public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage,
ObjectFactory factory, Part sourcePart, String content,
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);
headP.setPPr(pPr);
hdr.getContent().add(headP);
return hdr;
}
示例10
public Ftr getTextFtr(WordprocessingMLPackage wordprocessingMLPackage,
ObjectFactory factory, Part sourcePart, String content,
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);
ftr.getContent().add(footerP);
return ftr;
}
示例11
private void getFormattedTextForLineElement(List<WordType> words, P p, MainDocumentPart mdp) throws Exception{
int wordCount = 0;
int nrWords = words.size();
for (WordType word : words){
getFormattedTextForShapeElement((ITrpShapeType) word, p, mdp);
//add empty space after each word
if (wordCount < nrWords-1){
org.docx4j.wml.Text t = factory.createText();
t.setValue(" ");
t.setSpace("preserve");
org.docx4j.wml.R run = factory.createR();
p.getContent().add(run);
run.getContent().add(t);
}
wordCount++;
}
}
示例12
private org.docx4j.wml.Comments.Comment createComment(java.math.BigInteger commentId,
String author, Calendar date, String message) {
org.docx4j.wml.Comments.Comment comment = factory.createCommentsComment();
comment.setId( commentId );
if (author!=null) {
comment.setAuthor(author);
}
if (date!=null) {
// String dateString = RFC3339_FORMAT.format(date.getTime()) ;
// comment.setDate(value)
// TODO - at present this is XMLGregorianCalendar
}
org.docx4j.wml.P commentP = factory.createP();
comment.getEGBlockLevelElts().add(commentP);
org.docx4j.wml.R commentR = factory.createR();
commentP.getContent().add(commentR);
org.docx4j.wml.Text commentText = factory.createText();
commentR.getContent().add(commentText);
commentText.setValue(message);
return comment;
}
示例13
public Set<Text> mergeMatchedTexts() {
for (Object paragraphContentObject : paragraph.getContent()) {
if (paragraphContentObject instanceof R) {
R currentRun = (R) paragraphContentObject;
for (Object runContentObject : currentRun.getContent()) {
Object unwrappedRunContentObject = XmlUtils.unwrap(runContentObject);
if (unwrappedRunContentObject instanceof Text) {
handleText((Text) unwrappedRunContentObject);
}
}
}
}
removeUnnecessaryTexts();
return resultingTexts;
}
示例14
protected void handleMatchedText() {
resultingTexts.add(startText);
startText.setValue(mergedTextsString.toString());
for (Text mergedText : mergedTexts) {
if (mergedText != startText) {
mergedText.setValue("");
textsToRemove.add(mergedText);
}
}
if (!containsStartOfRegexp(startText.getValue().replaceAll(regexp, ""))) {
startText = null;
mergedTexts = null;
mergedTextsString = null;
} else {
mergedTexts = new HashSet<Text>();
mergedTexts.add(startText);
mergedTextsString = new StringBuilder(startText.getValue());
}
}
示例15
protected Part resolveTextPartForDOCX(Text text, WordprocessingMLPackage wordPackage) {
java.util.List<SectionWrapper> sectionWrappers = wordPackage.getDocumentModel().getSections();
for (SectionWrapper sw : sectionWrappers) {
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
List<Part> parts = Arrays.asList(hfp.getFirstHeader(), hfp.getDefaultHeader(), hfp.getEvenHeader(),
hfp.getFirstFooter(), hfp.getDefaultFooter(), hfp.getEvenFooter());
for (Part part : parts) {
TextMatchCallback callback = new TextMatchCallback(text);
new TraversalUtil(part, callback);
if (callback.matched) {
return part;
}
}
}
return wordPackage.getMainDocumentPart();
}
示例16
public static void replacePlaceholder(MainDocumentPart documentPart, String placeholder, String content ) {
// 获取文档对象中所有的Text类型对象
List<Text> texts = WmlElementUtils.getTargetElements(documentPart, Text.class);
// 循环Text类型对象集合
for (Text text : texts) {
Text textElement = (Text) text;
if (textElement.getValue().equals(placeholder)) {
textElement.setValue(content);
}
}
}
示例17
public static Tbl getTable(List<Tbl> tables, String placeholder) throws Docx4JException {
for (Iterator<Tbl> iterator = tables.iterator(); iterator.hasNext();) {
Tbl tbl = iterator.next();
//查找当前table下面的text对象
List<Text> textElements = getTargetElements(tbl, Text.class);
for (Text text : textElements) {
Text textElement = (Text) text;
//
if (textElement.getValue() != null && textElement.getValue().equals(placeholder)) {
return (Tbl) tbl;
}
}
}
return null;
}
示例18
public static void addRowToTable(Tbl reviewtable, Tr templateRow, Map<String, String> replacements) {
Tr workingRow = (Tr) XmlUtils.deepCopy(templateRow);
List<?> textElements = getTargetElements(workingRow, Text.class);
for (Object object : textElements) {
Text text = (Text) object;
String replacementValue = (String) replacements.get(text.getValue());
if (replacementValue != null)
text.setValue(replacementValue);
}
reviewtable.getContent().add(workingRow);
}
示例19
public static Ftr createFooter(String content) {
Ftr footer = factory.createFtr();
P paragraph = factory.createP();
R run = factory.createR();
Text text = new Text();
text.setValue(content);
run.getContent().add(text);
paragraph.getContent().add(run);
footer.getContent().add(paragraph);
return footer;
}
示例20
/**
* 设置段落内容
*/
private static void setParagraphContent(P p, RPr rpr,String content) {
Text t = Docx4j_Helper.factory.createText();
t.setSpace("preserve");
t.setValue(content);
R run = Docx4j_Helper.factory.createR();
run.setRPr(rpr);
run.getContent().add(t);
p.getContent().add(run);
}
示例21
/**
* 替换单元格内容
*/
private void replaceTcContent(Tc tc, String value) {
List<Object> rtnList = getAllElementFromObject(tc, Text.class);
if (rtnList == null || rtnList.size() == 0) {
return;
}
Text textElement = (Text) rtnList.get(0);
textElement.setValue(value);
}
示例22
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);
}
示例23
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);
}
示例24
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);
}
示例25
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;
}
示例26
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);
}
示例27
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));
}
示例28
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);
}
示例29
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);
}
示例30
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);
}