Search in sources :

Example 1 with Hyperlink

use of org.docx4j.wml.P.Hyperlink 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);
}
Also used : PPr(org.docx4j.wml.PPr) ObjectFactory(org.docx4j.wml.ObjectFactory) Relationship(org.docx4j.relationships.Relationship) ParaRPr(org.docx4j.wml.ParaRPr) RFonts(org.docx4j.wml.RFonts) Hyperlink(org.docx4j.wml.P.Hyperlink)

Example 2 with Hyperlink

use of org.docx4j.wml.P.Hyperlink 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);
}
Also used : ObjectFactory(org.docx4j.wml.ObjectFactory) Hyperlink(org.docx4j.wml.P.Hyperlink)

Aggregations

ObjectFactory (org.docx4j.wml.ObjectFactory)2 Hyperlink (org.docx4j.wml.P.Hyperlink)2 Relationship (org.docx4j.relationships.Relationship)1 PPr (org.docx4j.wml.PPr)1 ParaRPr (org.docx4j.wml.ParaRPr)1 RFonts (org.docx4j.wml.RFonts)1