Search in sources :

Example 26 with Relationship

use of org.docx4j.relationships.Relationship in project docx4j-template by vindell.

the class Docx4j_替换模板 method replacePieChartData.

/**
 * 替换图表数据
 */
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]);
}
Also used : CTPieChart(org.docx4j.dml.chart.CTPieChart) CTPieSer(org.docx4j.dml.chart.CTPieSer) CTNumDataSource(org.docx4j.dml.chart.CTNumDataSource) CTChartSpace(org.docx4j.dml.chart.CTChartSpace) Relationship(org.docx4j.relationships.Relationship) CTNumVal(org.docx4j.dml.chart.CTNumVal) RelationshipsPart(org.docx4j.openpackaging.parts.relationships.RelationshipsPart) Chart(org.docx4j.openpackaging.parts.DrawingML.Chart) CTPieChart(org.docx4j.dml.chart.CTPieChart)

Example 27 with Relationship

use of org.docx4j.relationships.Relationship in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method renderURL.

private void renderURL(final BasedSequence urlSrc, final DocxRendererContext docx, final String linkUrl, final Attributes attributes, final Runnable runnable) {
    P p = docx.getP();
    String linkTitle = attributes != null && attributes.contains(Attribute.TITLE_ATTR) ? attributes.getValue(Attribute.TITLE_ATTR) : "";
    final P.Hyperlink hyperlink = docx.getFactory().createPHyperlink();
    JAXBElement<P.Hyperlink> wrappedHyperlink = docx.getFactory().createPHyperlink(hyperlink);
    p.getContent().add(wrappedHyperlink);
    String highlightMissing = "";
    String linkTooltipText = linkTitle;
    String linkUrlText = linkUrl;
    if (linkUrl.startsWith("#")) {
        // local, we use anchor
        // see if we need to adjust it
        final String nodeId = linkUrl.substring(1);
        if (docx.getNodeFromId(nodeId) == null) {
            highlightMissing = docx.getRenderingOptions().localHyperlinkMissingHighlight;
            linkTooltipText = String.format(docx.getRenderingOptions().localHyperlinkMissingFormat, nodeId);
            if (docx.getDocxRendererOptions().errorsToStdErr) {
                // output details of error
                final Pair<Integer, Integer> atIndex = urlSrc.getBaseSequence().getLineColumnAtIndex(urlSrc.getStartOffset());
                String sourceFile = docx.getDocxRendererOptions().errorSourceFile;
                if (sourceFile.isEmpty()) {
                    sourceFile = "on line ";
                } else {
                    sourceFile = String.format("in %s:", sourceFile);
                }
                System.err.println(String.format("    WARN: Invalid anchor target '%s' %s%d:%d", nodeId, sourceFile, atIndex.getFirst() + 1, atIndex.getSecond() + 2));
            }
        }
        final String anchor = docx.getValidBookmarkName(nodeId) + options.localHyperlinkSuffix;
        hyperlink.setAnchor(anchor);
        linkUrlText = String.format("#%s", anchor);
    } else {
        Relationship rel = docx.getHyperlinkRelationship(linkUrl);
        // Create object for hyperlink (wrapped in JAXBElement)
        hyperlink.setId(rel.getId());
    }
    if (linkTooltipText != null && !linkTooltipText.isEmpty()) {
        hyperlink.setTooltip(linkTooltipText);
    }
    docx.setRunFormatProvider(new RunFormatProviderBase<Node>(docx, docx.getDocxRendererOptions().HYPERLINK_STYLE, options.noCharacterStyles, highlightMissing));
    docx.setRunContainer(new RunContainer() {

        @Override
        public void addR(final R r) {
            hyperlink.getContent().add(r);
        }

        @Override
        public R getLastR() {
            final List<Object> content = hyperlink.getContent();
            if (content == null || content.size() == 0)
                return null;
            final Object o = content.get(content.size() - 1);
            return o instanceof R ? (R) o : null;
        }
    });
    if (linkTitle != null && !linkTitle.isEmpty()) {
        // Create object for instrText (wrapped in JAXBElement)
        // Create object for r
        R r = docx.getFactory().createR();
        hyperlink.getContent().add(r);
        // Create object for fldChar (wrapped in JAXBElement)
        FldChar fldchar = docx.getFactory().createFldChar();
        JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped = docx.getFactory().createRFldChar(fldchar);
        r.getContent().add(fldcharWrapped);
        fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
        // Create object for r
        R r2 = docx.getFactory().createR();
        hyperlink.getContent().add(r2);
        // Create object for instrText (wrapped in JAXBElement)
        org.docx4j.wml.Text text = docx.getFactory().createText();
        JAXBElement<org.docx4j.wml.Text> textWrapped = docx.getFactory().createRInstrText(text);
        r2.getContent().add(textWrapped);
        text.setValue(String.format(" HYPERLINK \"%s\" \\o \"%s\" ", linkUrlText, linkTitle));
        text.setSpace(RunFormatProvider.SPACE_PRESERVE);
        // Create object for r
        R r3 = docx.getFactory().createR();
        hyperlink.getContent().add(r3);
        // Create object for fldChar (wrapped in JAXBElement)
        FldChar fldchar2 = docx.getFactory().createFldChar();
        JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped2 = docx.getFactory().createRFldChar(fldchar2);
        r3.getContent().add(fldcharWrapped2);
        fldchar2.setFldCharType(org.docx4j.wml.STFldCharType.SEPARATE);
    }
    runnable.run();
    if (linkTitle != null && !linkTitle.isEmpty()) {
        // Create object for r
        R r3 = docx.getFactory().createR();
        hyperlink.getContent().add(r3);
        // Create object for fldChar (wrapped in JAXBElement)
        FldChar fldchar2 = docx.getFactory().createFldChar();
        JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped2 = docx.getFactory().createRFldChar(fldchar2);
        r3.getContent().add(fldcharWrapped2);
        fldchar2.setFldCharType(STFldCharType.END);
    }
}
Also used : AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode) EnumeratedReferenceText(com.vladsch.flexmark.ext.enumerated.reference.EnumeratedReferenceText) Text(com.vladsch.flexmark.ast.Text) BigInteger(java.math.BigInteger) Relationship(org.docx4j.relationships.Relationship) org.docx4j.wml(org.docx4j.wml)

Aggregations

Relationship (org.docx4j.relationships.Relationship)27 FooterPart (org.docx4j.openpackaging.parts.WordprocessingML.FooterPart)7 HeaderPart (org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart)7 File (java.io.File)6 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)4 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)3 PartName (org.docx4j.openpackaging.parts.PartName)3 ObjectFactory (org.docx4j.wml.ObjectFactory)3 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 ContentType (org.docx4j.openpackaging.contenttype.ContentType)2 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)2 AlternativeFormatInputPart (org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart)2 CTAltChunk (org.docx4j.wml.CTAltChunk)2 Text (com.vladsch.flexmark.ast.Text)1 AttributesNode (com.vladsch.flexmark.ext.attributes.AttributesNode)1 EnumeratedReferenceText (com.vladsch.flexmark.ext.enumerated.reference.EnumeratedReferenceText)1 FileNotFoundException (java.io.FileNotFoundException)1 JAXBException (javax.xml.bind.JAXBException)1 CTChartSpace (org.docx4j.dml.chart.CTChartSpace)1