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]);
}
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);
}
}
Aggregations