org.docx4j.openpackaging.parts.relationships.Namespaces Java Examples

The following examples show how to use org.docx4j.openpackaging.parts.relationships.Namespaces. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CoordinatesWalker.java    From docx-stamper with MIT License 6 votes vote down vote up
public void walk() {

        RelationshipsPart relationshipsPart = document.getMainDocumentPart().getRelationshipsPart();

        // walk through elements in headers
        List<Relationship> headerRelationships = getRelationshipsOfType(document, Namespaces.HEADER);
        for (Relationship header : headerRelationships) {
            HeaderPart headerPart = (HeaderPart) relationshipsPart.getPart(header.getId());
            walkContent(headerPart.getContent());
        }

        // walk through elements in main document part
        walkContent(document.getMainDocumentPart().getContent());

        // walk through elements in headers
        List<Relationship> footerRelationships = getRelationshipsOfType(document, Namespaces.FOOTER);
        for (Relationship footer : footerRelationships) {
            FooterPart footerPart = (FooterPart) relationshipsPart.getPart(footer.getId());
            walkContent(footerPart.getContent());
        }
    }
 
Example #2
Source File: Docx4j_替换模板.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/** 
 * 替换图表数据 
 */  
private void replacePieChartData(WordprocessingMLPackage wordMLPackage, String[] chartArr) throws Docx4JException {  
    RelationshipsPart rp = wordMLPackage.getMainDocumentPart().getRelationshipsPart();  
    Relationship rel = rp.getRelationshipByType(Namespaces.SPREADSHEETML_CHART);  
    Chart chart = (Chart) rp.getPart(rel);  
    CTChartSpace chartSpace = chart.getContents();  
    List<Object> charObjList = chartSpace.getChart().getPlotArea().getAreaChartOrArea3DChartOrLineChart();  
    CTPieChart pieChart = (CTPieChart) charObjList.get(0);  
    List<CTPieSer> serList = pieChart.getSer();  
    CTNumDataSource serVal = serList.get(0).getVal();  
    List<CTNumVal> ptList = serVal.getNumRef().getNumCache().getPt();  
    ptList.get(0).setV(chartArr[0]);  
    ptList.get(1).setV(chartArr[1]);  
}
 
Example #3
Source File: AbstractInliner.java    From yarg with Apache License 2.0 5 votes vote down vote up
private void putImage(WorksheetPart worksheetPart, SpreadsheetMLPackage pkg, BinaryPartAbstractImage imagePart, CTOneCellAnchor anchor) throws Docx4JException {
    PartName drawingPart = new PartName(StringUtils.replaceIgnoreCase(worksheetPart.getPartName().getName(),
            "worksheets/sheet", "drawings/drawing"));
    String imagePartName = imagePart.getPartName().getName();
    Part part = pkg.getParts().get(drawingPart);
    if (part != null && !(part instanceof Drawing))
        throw new ReportFormattingException("Wrong Class: not a Drawing");
    Drawing drawing = (Drawing) part;
    int currentId = 0;
    if (drawing == null) {
        drawing = new Drawing(drawingPart);
        drawing.setContents(new org.docx4j.dml.spreadsheetdrawing.CTDrawing());
        Relationship relationship = worksheetPart.addTargetPart(drawing);
        org.xlsx4j.sml.CTDrawing smlDrawing = new org.xlsx4j.sml.CTDrawing();
        smlDrawing.setId(relationship.getId());
        smlDrawing.setParent(worksheetPart.getContents());
        worksheetPart.getContents().setDrawing(smlDrawing);
    } else {
        currentId = drawing.getContents().getEGAnchor().size();
    }

    CTPicture picture = new CTPicture();

    CTBlipFillProperties blipFillProperties = new CTBlipFillProperties();
    blipFillProperties.setStretch(new CTStretchInfoProperties());
    blipFillProperties.getStretch().setFillRect(new CTRelativeRect());
    blipFillProperties.setBlip(new CTBlip());
    blipFillProperties.getBlip().setEmbed("rId" + (currentId + 1));
    blipFillProperties.getBlip().setCstate(STBlipCompression.PRINT);

    picture.setBlipFill(blipFillProperties);

    CTNonVisualDrawingProps nonVisualDrawingProps = new CTNonVisualDrawingProps();
    nonVisualDrawingProps.setId(currentId + 2);
    nonVisualDrawingProps.setName(imagePartName.substring(imagePartName.lastIndexOf("/") + 1));
    nonVisualDrawingProps.setDescr(nonVisualDrawingProps.getName());

    CTNonVisualPictureProperties nonVisualPictureProperties = new CTNonVisualPictureProperties();
    nonVisualPictureProperties.setPicLocks(new CTPictureLocking());
    nonVisualPictureProperties.getPicLocks().setNoChangeAspect(true);
    CTPictureNonVisual nonVisualPicture = new CTPictureNonVisual();

    nonVisualPicture.setCNvPr(nonVisualDrawingProps);
    nonVisualPicture.setCNvPicPr(nonVisualPictureProperties);

    picture.setNvPicPr(nonVisualPicture);

    CTShapeProperties shapeProperties = new CTShapeProperties();
    CTTransform2D transform2D = new CTTransform2D();
    transform2D.setOff(new CTPoint2D());
    transform2D.setExt(new CTPositiveSize2D());
    shapeProperties.setXfrm(transform2D);
    shapeProperties.setPrstGeom(new CTPresetGeometry2D());
    shapeProperties.getPrstGeom().setPrst(STShapeType.RECT);
    shapeProperties.getPrstGeom().setAvLst(new CTGeomGuideList());

    picture.setSpPr(shapeProperties);

    anchor.setPic(picture);
    anchor.setClientData(new CTAnchorClientData());

    drawing.getContents().getEGAnchor().add(anchor);

    Relationship rel = new Relationship();
    rel.setId("rId" + (currentId + 1));
    rel.setType(Namespaces.IMAGE);
    rel.setTarget(imagePartName);

    drawing.getRelationshipsPart().addRelationship(rel);
    RelationshipsPart relPart = drawing.getRelationshipsPart();
    pkg.getParts().remove(relPart.getPartName());
    pkg.getParts().put(relPart);
    pkg.getParts().remove(drawing.getPartName());
    pkg.getParts().put(drawing);
}
 
Example #4
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
/**
 * @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);
}
 
Example #5
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
/**
 * @Description:创建表格(默认水平居中,垂直居中)
 */
public static Tbl createTable(int rowNum, int colsNum, int[] widthArr) throws Exception {
    colsNum = Math.max(1, Math.min(colsNum, widthArr.length));
    rowNum = Math.max(1, rowNum);
    Tbl tbl = new Tbl();
    StringBuffer tblSb = new StringBuffer();
    tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION).append(">");
    tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>");
    tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>");
    // 上边框
    tblSb.append("<w:tblBorders>");
    tblSb.append("<w:top w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 左边框
    tblSb.append("<w:left w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 下边框
    tblSb.append("<w:bottom w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 右边框
    tblSb.append("<w:right w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("<w:insideH w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("<w:insideV w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("</w:tblBorders>");
    tblSb.append("</w:tblPr>");
    TblPr tblPr = null;
    tblPr = (TblPr) XmlUtils.unmarshalString(tblSb.toString());
    Jc jc = new Jc();
    // 单元格居中对齐
    jc.setVal(JcEnumeration.CENTER);
    tblPr.setJc(jc);

    tbl.setTblPr(tblPr);

    // 设定各单元格宽度
    TblGrid tblGrid = new TblGrid();
    tbl.setTblGrid(tblGrid);
    for (int i = 0; i < colsNum; i++) {
        TblGridCol gridCol = new TblGridCol();
        gridCol.setW(BigInteger.valueOf(widthArr[i]));
        tblGrid.getGridCol().add(gridCol);
    }
    // 新增行
    for (int j = 0; j < rowNum; j++) {
        Tr tr = new Tr();
        tbl.getContent().add(tr);
        // 列
        for (int i = 0; i < colsNum; i++) {
            Tc tc = new Tc();
            tr.getContent().add(tc);

            TcPr tcPr = new TcPr();
            TblWidth cellWidth = new TblWidth();
            cellWidth.setType("dxa");
            cellWidth.setW(BigInteger.valueOf(widthArr[i]));
            tcPr.setTcW(cellWidth);
            tc.setTcPr(tcPr);

            // 垂直居中
            setTcVAlign(tc, STVerticalJc.CENTER);
            P p = new P();
            PPr pPr = new PPr();
            pPr.setJc(jc);
            p.setPPr(pPr);
            R run = new R();
            p.getContent().add(run);
            tc.getContent().add(p);
        }
    }
    return tbl;
}
 
Example #6
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
/**
 * @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);
}
 
Example #7
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
/**
 * @Description:创建表格(默认水平居中,垂直居中)
 */
public Tbl createTable(int rowNum, int colsNum, int[] widthArr)
        throws Exception {
    colsNum = Math.max(1, Math.min(colsNum, widthArr.length));
    rowNum = Math.max(1, rowNum);
    Tbl tbl = new Tbl();
    StringBuffer tblSb = new StringBuffer();
    tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION)
            .append(">");
    tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>");
    tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>");
    // 上边框
    tblSb.append("<w:tblBorders>");
    tblSb.append("<w:top w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 左边框
    tblSb.append("<w:left w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 下边框
    tblSb.append("<w:bottom w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 右边框
    tblSb.append("<w:right w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("<w:insideH w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("<w:insideV w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("</w:tblBorders>");
    tblSb.append("</w:tblPr>");
    TblPr tblPr = null;
    tblPr = (TblPr) XmlUtils.unmarshalString(tblSb.toString());
    Jc jc = new Jc();
    // 单元格居中对齐
    jc.setVal(JcEnumeration.CENTER);
    tblPr.setJc(jc);

    tbl.setTblPr(tblPr);

    // 设定各单元格宽度
    TblGrid tblGrid = new TblGrid();
    tbl.setTblGrid(tblGrid);
    for (int i = 0; i < colsNum; i++) {
        TblGridCol gridCol = new TblGridCol();
        gridCol.setW(BigInteger.valueOf(widthArr[i]));
        tblGrid.getGridCol().add(gridCol);
    }
    // 新增行
    for (int j = 0; j < rowNum; j++) {
        Tr tr = new Tr();
        tbl.getContent().add(tr);
        // 列
        for (int i = 0; i < colsNum; i++) {
            Tc tc = new Tc();
            tr.getContent().add(tc);

            TcPr tcPr = new TcPr();
            TblWidth cellWidth = new TblWidth();
            cellWidth.setType("dxa");
            cellWidth.setW(BigInteger.valueOf(widthArr[i]));
            tcPr.setTcW(cellWidth);
            tc.setTcPr(tcPr);

            // 垂直居中
            setTcVAlign(tc, STVerticalJc.CENTER);
            P p = new P();
            PPr pPr = new PPr();
            pPr.setJc(jc);
            p.setPPr(pPr);
            R run = new R();
            p.getContent().add(run);
            tc.getContent().add(p);
        }
    }
    return tbl;
}
 
Example #8
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example #9
Source File: DocxBuilder.java    From TranskribusCore with GNU General Public License v3.0 4 votes vote down vote up
public Hyperlink createHyperlink(MainDocumentPart mdp, String url) {
		
		try {

			// We need to add a relationship to word/_rels/document.xml.rels
			// but since its external, we don't use the 
			// usual wordMLPackage.getMainDocumentPart().addTargetPart
			// mechanism
			org.docx4j.relationships.ObjectFactory factory =
				new org.docx4j.relationships.ObjectFactory();
			
			org.docx4j.relationships.Relationship rel = factory.createRelationship();
			rel.setType( Namespaces.HYPERLINK  );
			rel.setTarget(url);
			rel.setTargetMode("External");  
									
			mdp.getRelationshipsPart().addRelationship(rel);
			
			// addRelationship sets the rel's @Id
			
			String hpl = "<w:hyperlink r:id=\"" + rel.getId() + "\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
            "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >" +
            "<w:r>" +
            "<w:rPr>" +
            "<w:rStyle w:val=\"Hyperlink\" />" +  // TODO: enable this style in the document!
            "</w:rPr>" +
            "<w:t>Link</w:t>" +
            "</w:r>" +
            "</w:hyperlink>";

//			return (Hyperlink)XmlUtils.unmarshalString(hpl, Context.jc, P.Hyperlink.class);
			return (Hyperlink)XmlUtils.unmarshalString(hpl);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
	}
 
Example #10
Source File: ExpressionReplacementInHeaderAndFooterTest.java    From docx-stamper with MIT License 4 votes vote down vote up
private HeaderPart getHeaderPart(WordprocessingMLPackage document) {
    RelationshipsPart relPart = document.getMainDocumentPart().getRelationshipsPart();
    Relationship rel = relPart.getRelationshipByType(Namespaces.HEADER);
    return (HeaderPart) relPart.getPart(rel);
}
 
Example #11
Source File: ExpressionReplacementInHeaderAndFooterTest.java    From docx-stamper with MIT License 4 votes vote down vote up
private FooterPart getFooterPart(WordprocessingMLPackage document) {
    RelationshipsPart relPart = document.getMainDocumentPart().getRelationshipsPart();
    Relationship rel = relPart.getRelationshipByType(Namespaces.FOOTER);
    return (FooterPart) relPart.getPart(rel);
}
 
Example #12
Source File: AddingTableOfContent.java    From docx4j-template with Apache License 2.0 2 votes vote down vote up
/**
    *  创建包含给定复杂域字符的JAXBElement的便利方法.
    *
    *  @param fldchar
    *  @return
    */
   @SuppressWarnings({ "unchecked", "rawtypes" })
public static JAXBElement getWrappedFldChar(FldChar fldchar) {
       return new JAXBElement(new QName(Namespaces.NS_WORD12, "fldChar"), FldChar.class, fldchar);
   }