use of org.docx4j.wml.ObjectFactory in project docx4j-template by vindell.
the class Docx4J_例子2 method createHyperlink.
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("微软雅黑");
fonts.setHAnsi("微软雅黑");
fonts.setEastAsia("微软雅黑");
fonts.setHint(STHint.EAST_ASIA);
ParaRPr rpr = new ParaRPr();
rpr.setRFonts(fonts);
ppr.setRPr(rpr);
paragraph.setPPr(ppr);
}
use of org.docx4j.wml.ObjectFactory in project docx4j-template by vindell.
the class Docx4J_例子2 method main.
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"));
}
use of org.docx4j.wml.ObjectFactory in project docx4j-template by vindell.
the class Docx4j_工具类_S3_Test method createHyperlink.
/*------------------------------------other--------------------------------------------------- */
/**
* @Description:新增超链接
*/
public void createHyperlink(WordprocessingMLPackage wordMLPackage, MainDocumentPart mainPart, ObjectFactory factory, P paragraph, String url, String value, String cnFontName, String enFontName, String fontSize) throws Exception {
if (StringUtils.isBlank(enFontName)) {
enFontName = "Times New Roman";
}
if (StringUtils.isBlank(cnFontName)) {
cnFontName = "微软雅黑";
}
if (StringUtils.isBlank(fontSize)) {
fontSize = "22";
}
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");
mainPart.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(enFontName);
sb.append("\" w:hAnsi=\"");
sb.append(enFontName);
sb.append("\" w:eastAsia=\"");
sb.append(cnFontName);
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);
}
use of org.docx4j.wml.ObjectFactory in project docx4j-template by vindell.
the class AddingAnInlineImage method addInlineImageToParagraph.
/**
* 创建一个对象工厂并用它创建一个段落和一个可运行块R.
* 然后将可运行块添加到段落中. 接下来创建一个图画并将其添加到可运行块R中. 最后我们将内联
* 对象添加到图画中并返回段落对象.
*
* @param inline 包含图片的内联对象.
* @return 包含图片的段落
*/
private static P addInlineImageToParagraph(Inline inline) {
// 添加内联对象到一个段落中
ObjectFactory factory = new ObjectFactory();
P paragraph = factory.createP();
R run = factory.createR();
paragraph.getContent().add(run);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return paragraph;
}
use of org.docx4j.wml.ObjectFactory in project docx4j-template by vindell.
the class AddingAnInlineImageToTable method addInlineImageToParagraph.
/**
* 向新的段落中添加内联图片并返回这个段落.
* 这个方法与前面例子中的方法没有区别.
* @param inline
* @return
*/
private static P addInlineImageToParagraph(Inline inline) {
// Now add the in-line image to a paragraph
ObjectFactory factory = new ObjectFactory();
P paragraph = factory.createP();
R run = factory.createR();
paragraph.getContent().add(run);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return paragraph;
}
Aggregations