import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
-import org.apache.poi.xslf.usermodel.XSLFChart;
-import org.apache.poi.xslf.usermodel.XSLFFactory;
-import org.apache.poi.xslf.usermodel.XSLFRelation;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.apache.poi.xwpf.usermodel.XWPFFactory;
-import org.apache.poi.xwpf.usermodel.XWPFRelation;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarChart;
* @since POI 4.0.0
*/
private PackagePart createWorksheetPart(POIXMLRelation chartRelation, POIXMLRelation chartWorkbookRelation, POIXMLFactory chartFactory) throws InvalidFormatException {
- PackageRelationship xlsx = createRelationshipInChart(XSLFRelation.WORKBOOK_RELATIONSHIP, XSLFFactory.getInstance(), chartIndex);
+ PackageRelationship xlsx = createRelationshipInChart(chartWorkbookRelation, chartFactory, chartIndex);
this.setExternalId(xlsx.getId());
return getTargetPart(xlsx);
}
public void saveWorkbook(XSSFWorkbook workbook) throws IOException, InvalidFormatException {
PackagePart worksheetPart = getWorksheetPart(true);
if (worksheetPart == null) {
- POIXMLRelation chartRelation = null;
- POIXMLRelation chartWorkbookRelation = null;
- POIXMLFactory chartFactory = null;
- if (this instanceof XSLFChart) {
- chartRelation = XSLFRelation.CHART;
- chartWorkbookRelation = XSLFRelation.WORKBOOK_RELATIONSHIP;
- chartFactory = XSLFFactory.getInstance();
- } else {
- chartRelation = XWPFRelation.CHART;
- chartRelation = XWPFRelation.WORKBOOK_RELATIONSHIP;
- chartFactory = XWPFFactory.getInstance();
+ POIXMLRelation chartRelation = getChartRelation();
+ POIXMLRelation chartWorkbookRelation = getChartWorkbookRelation();
+ POIXMLFactory chartFactory = getChartFactory();
+ if (chartRelation != null
+ && chartWorkbookRelation != null
+ && chartFactory != null) {
+ worksheetPart = createWorksheetPart(chartRelation, chartWorkbookRelation, chartFactory);
}
- worksheetPart = createWorksheetPart(chartRelation, chartWorkbookRelation, chartFactory);
}
try (OutputStream xlsOut = worksheetPart.getOutputStream()) {
workbook.write(xlsOut);
}
}
+ /**
+ *
+ * @return the chart relation in the implementing subclass.
+ * @since POI 4.0.0
+ */
+ protected abstract POIXMLRelation getChartRelation();
+
+ /**
+ *
+ * @return the chart workbook relation in the implementing subclass.
+ * @since POI 4.0.0
+ */
+ protected abstract POIXMLRelation getChartWorkbookRelation();
+
+ /**
+ *
+ * @return the chart factory in the implementing subclass.
+ * @since POI 4.0.0
+ */
+ protected abstract POIXMLFactory getChartFactory();
+
/**
* this method writes the data into sheet
*
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
-import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
public XWPFChart createChart(int width, int height) throws InvalidFormatException, IOException {
//get chart number
- int chartNumber = getPackagePart().getPackage().
- getPartsByContentType(XWPFRelation.CHART.getContentType()).size() + 1;
+ int chartNumber = getNextPartNumber(XWPFRelation.CHART, charts.size() + 1);
//create relationship in document for new chart
RelationPart rp = createRelationship(
XWPFRelation.CHART, XWPFFactory.getInstance(), chartNumber, false);
- //get chart relationship id
- String chartId = rp.getRelationship().getId();
-
- //create paragraph and run object
- XWPFRun xRun = this.createParagraph().createRun();
-
- CTInline inline = xRun.addChart(width, height, chartId);
-
- //get package part of xwpfchart object
+ // initialize xwpfchart object
XWPFChart xwpfChart = rp.getDocumentPart();
-
xwpfChart.setChartIndex(chartNumber);
-
- //set in line object into xwpfchart object
- xwpfChart.setAttachTo(inline);
+ xwpfChart.attach(rp.getRelationship().getId(), createParagraph().createRun());
+ xwpfChart.setChartBoundingBox(width, height);
//add chart object to chart list
charts.add(xwpfChart);
import org.apache.xmlbeans.XmlToken;
import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
-import org.openxmlformats.schemas.drawingml.x2006.chart.CTRelId;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
NodeList kids = t.getDomNode().getChildNodes();
for (int n = 0; n < kids.getLength(); n++) {
if (kids.item(n) instanceof Text) {
- if (text.length() > 0)
+ if (text.length() > 0) {
text.append("\n");
+ }
text.append(kids.item(n).getNodeValue());
}
}
/**
* @deprecated Use {@link XWPFRun#XWPFRun(CTR, IRunBody)}
*/
+ @Deprecated
public XWPFRun(CTR r, XWPFParagraph p) {
this(r, (IRunBody) p);
}
*
* @deprecated use {@link XWPFRun#getParent()} instead
*/
+ @Deprecated
public XWPFParagraph getParagraph() {
- if (parent instanceof XWPFParagraph)
+ if (parent instanceof XWPFParagraph) {
return (XWPFParagraph) parent;
+ }
return null;
}
* For isBold, isItalic etc
*/
private static boolean isCTOnOff(CTOnOff onoff) {
- if (!onoff.isSetVal())
+ if (!onoff.isSetVal()) {
return true;
+ }
final STOnOff.Enum val = onoff.getVal();
return (
(STOnOff.TRUE == val) ||
*
* @return <code>true</code> if the bold property is applied
*/
+ @Override
public boolean isBold() {
CTRPr pr = run.getRPr();
return pr != null && pr.isSetB() && isCTOnOff(pr.getB());
* @param value <code>true</code> if the bold property is applied to
* this run
*/
+ @Override
public void setBold(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff bold = pr.isSetB() ? pr.getB() : pr.addNewB();
* @param pos - position in the text array (NB: 0 based)
*/
public void setText(String value, int pos) {
- if (pos > run.sizeOfTArray())
+ if (pos > run.sizeOfTArray()) {
throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)");
+ }
CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT();
t.setStringValue(value);
preserveSpaces(t);
*
* @return <code>true</code> if the italic property is applied
*/
+ @Override
public boolean isItalic() {
CTRPr pr = run.getRPr();
return pr != null && pr.isSetI() && isCTOnOff(pr.getI());
* @param value <code>true</code> if the italic property is applied to
* this run
*/
+ @Override
public void setItalic(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff italic = pr.isSetI() ? pr.getI() : pr.addNewI();
*
* @return <code>true</code> if the strike property is applied
*/
+ @Override
public boolean isStrikeThrough() {
CTRPr pr = run.getRPr();
return pr != null && pr.isSetStrike() && isCTOnOff(pr.getStrike());
* @param value <code>true</code> if the strike property is applied to
* this run
*/
+ @Override
public void setStrikeThrough(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff strike = pr.isSetStrike() ? pr.getStrike() : pr.addNewStrike();
*
* @return <code>true</code> if the double strike property is applied
*/
+ @Override
public boolean isDoubleStrikeThrough() {
CTRPr pr = run.getRPr();
return pr != null && pr.isSetDstrike() && isCTOnOff(pr.getDstrike());
*
* @see #setStrikeThrough(boolean) for the rules about this
*/
+ @Override
public void setDoubleStrikethrough(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff dstrike = pr.isSetDstrike() ? pr.getDstrike() : pr.addNewDstrike();
dstrike.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
}
+ @Override
public boolean isSmallCaps() {
CTRPr pr = run.getRPr();
return pr != null && pr.isSetSmallCaps() && isCTOnOff(pr.getSmallCaps());
}
+ @Override
public void setSmallCaps(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff caps = pr.isSetSmallCaps() ? pr.getSmallCaps() : pr.addNewSmallCaps();
caps.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
}
+ @Override
public boolean isCapitalized() {
CTRPr pr = run.getRPr();
return pr != null && pr.isSetCaps() && isCTOnOff(pr.getCaps());
}
+ @Override
public void setCapitalized(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff caps = pr.isSetCaps() ? pr.getCaps() : pr.addNewCaps();
caps.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
}
+ @Override
public boolean isShadowed() {
CTRPr pr = run.getRPr();
return pr != null && pr.isSetShadow() && isCTOnOff(pr.getShadow());
}
+ @Override
public void setShadow(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff shadow = pr.isSetShadow() ? pr.getShadow() : pr.addNewShadow();
shadow.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
}
+ @Override
public boolean isImprinted() {
CTRPr pr = run.getRPr();
return pr != null && pr.isSetImprint() && isCTOnOff(pr.getImprint());
}
+ @Override
public void setImprinted(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff imprinted = pr.isSetImprint() ? pr.getImprint() : pr.addNewImprint();
imprinted.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
}
+ @Override
public boolean isEmbossed() {
CTRPr pr = run.getRPr();
return pr != null && pr.isSetEmboss() && isCTOnOff(pr.getEmboss());
}
+ @Override
public void setEmbossed(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff emboss = pr.isSetEmboss() ? pr.getEmboss() : pr.addNewEmboss();
ctValign.setVal(STVerticalAlignRun.Enum.forInt(valign.getValue()));
}
+ @Override
public int getKerning() {
CTRPr pr = run.getRPr();
- if (pr == null || !pr.isSetKern())
+ if (pr == null || !pr.isSetKern()) {
return 0;
+ }
return pr.getKern().getVal().intValue();
}
+ @Override
public void setKerning(int kern) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTHpsMeasure kernmes = pr.isSetKern() ? pr.getKern() : pr.addNewKern();
kernmes.setVal(BigInteger.valueOf(kern));
}
+ @Override
public boolean isHighlighted() {
CTRPr pr = run.getRPr();
- if (pr == null || !pr.isSetHighlight())
+ if (pr == null || !pr.isSetHighlight()) {
return false;
- if (pr.getHighlight().getVal() == STHighlightColor.NONE)
+ }
+ if (pr.getHighlight().getVal() == STHighlightColor.NONE) {
return false;
+ }
return true;
}
// TODO Provide a wrapper round STHighlightColor, then expose getter/setter
// for the highlight colour. Ideally also then add to CharacterRun interface
+ @Override
public int getCharacterSpacing() {
CTRPr pr = run.getRPr();
- if (pr == null || !pr.isSetSpacing())
+ if (pr == null || !pr.isSetSpacing()) {
return 0;
+ }
return pr.getSpacing().getVal().intValue();
}
+ @Override
public void setCharacterSpacing(int twips) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTSignedTwipsMeasure spc = pr.isSetSpacing() ? pr.getSpacing() : pr.addNewSpacing();
/**
* Alias for {@link #getFontFamily()}
*/
+ @Override
public String getFontName() {
return getFontFamily();
}
*/
public String getFontFamily(FontCharRange fcr) {
CTRPr pr = run.getRPr();
- if (pr == null || !pr.isSetRFonts()) return null;
+ if (pr == null || !pr.isSetRFonts()) {
+ return null;
+ }
CTFonts fonts = pr.getRFonts();
switch (fcr == null ? FontCharRange.ascii : fcr) {
*
* @return value representing the font size
*/
+ @Override
public int getFontSize() {
CTRPr pr = run.getRPr();
return (pr != null && pr.isSetSz()) ? pr.getSz().getVal().divide(new BigInteger("2")).intValue() : -1;
*
* @param size The font size as number of point measurements.
*/
+ @Override
public void setFontSize(int size) {
BigInteger bint = new BigInteger("" + size);
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
/**
* this method add chart template into document
*
- * @param width set width of chart object
- * @param height set height of chart object
* @param chartRelId relation id of chart in document relation file
* @throws InvalidFormatException
* @throws IOException
* @since POI 4.0.0
*/
@Internal
- public CTInline addChart(int width, int height, String chartRelId)
+ public CTInline addChart(String chartRelId)
throws InvalidFormatException, IOException {
try {
CTInline inline = run.addNewDrawing().addNewInline();
//This name is not visible in Word anywhere.
docPr.setName("chart " + id);
- CTPositiveSize2D extent = inline.addNewExtent();
- //set hegiht and width of drawaing object;
- extent.setCx(width);
- extent.setCy(height);
-
return inline;
} catch (XmlException e) {
throw new IllegalStateException(e);
/**
* Returns the string version of the text and the phonetic string
*/
+ @Override
public String toString() {
String phonetic = getPhonetic();
if (phonetic.length() > 0) {
* Returns the string version of the text, with tabs and
* carriage returns in place of their xml equivalents.
*/
+ @Override
public String text() {
StringBuilder text = new StringBuilder(64);