import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
-\r
+
/**
* XWPFRun object defines a region of text with a common set of properties
*/
private CTR run;
private String pictureText;
private IRunBody parent;
- private List<XWPFPicture> pictures;\r
-\r
- /**\r
- * @param r the CTR bean which holds the run attributes\r
- * @param p the parent paragraph\r
- */\r
+ private List<XWPFPicture> pictures;
+
+ /**
+ * @param r the CTR bean which holds the run attributes
+ * @param p the parent paragraph
+ */
public XWPFRun(CTR r, IRunBody p) {
this.run = r;
this.parent = p;
// Look for any text in any of our pictures or drawings
StringBuilder text = new StringBuilder();
- List<XmlObject> pictTextObjs = new ArrayList<XmlObject>();\r
- pictTextObjs.addAll(Arrays.asList(r.getPictArray()));\r
- pictTextObjs.addAll(Arrays.asList(r.getDrawingArray()));\r
- for (XmlObject o : pictTextObjs) {\r
- XmlObject[] ts = o.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t");\r
- for (XmlObject t : ts) {\r
- NodeList kids = t.getDomNode().getChildNodes();\r
+ List<XmlObject> pictTextObjs = new ArrayList<XmlObject>();
+ pictTextObjs.addAll(Arrays.asList(r.getPictArray()));
+ pictTextObjs.addAll(Arrays.asList(r.getDrawingArray()));
+ for (XmlObject o : pictTextObjs) {
+ XmlObject[] ts = o.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t");
+ for (XmlObject t : ts) {
+ NodeList kids = t.getDomNode().getChildNodes();
for (int n = 0; n < kids.getLength(); n++) {
if (kids.item(n) instanceof Text) {
if (text.length() > 0)
}
pictureText = text.toString();
- // Do we have any embedded pictures?\r
- // (They're a different CTPicture, under the drawingml namespace)\r
- pictures = new ArrayList<XWPFPicture>();\r
- for (XmlObject o : pictTextObjs) {\r
- for (CTPicture pict : getCTPictures(o)) {\r
- XWPFPicture picture = new XWPFPicture(pict, this);\r
- pictures.add(picture);\r
- }\r
- }\r
- }\r
-\r
- ;\r
-\r
- /**\r
- * @deprecated Use {@link XWPFRun#XWPFRun(CTR, IRunBody)}\r
- */\r
- public XWPFRun(CTR r, XWPFParagraph p) {\r
- this(r, (IRunBody) p);\r
- }\r
-\r
- /**\r
- * Add the xml:spaces="preserve" attribute if the string has leading or trailing white spaces\r
- *\r
- * @param xs the string to check\r
- */\r
- static void preserveSpaces(XmlString xs) {\r
- String text = xs.getStringValue();\r
- if (text != null && (text.startsWith(" ") || text.endsWith(" "))) {\r
- XmlCursor c = xs.newCursor();\r
- c.toNextToken();\r
- c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve");\r
- c.dispose();\r
- }\r
- }\r
-\r
- private List<CTPicture> getCTPictures(XmlObject o) {\r
- List<CTPicture> pics = new ArrayList<CTPicture>();\r
- XmlObject[] picts = o.selectPath("declare namespace pic='" + CTPicture.type.getName().getNamespaceURI() + "' .//pic:pic");\r
- for (XmlObject pict : picts) {\r
- if (pict instanceof XmlAnyTypeImpl) {\r
- // Pesky XmlBeans bug - see Bugzilla #49934\r
- try {\r
- pict = CTPicture.Factory.parse(pict.toString(), DEFAULT_XML_OPTIONS);\r
- } catch (XmlException e) {\r
- throw new POIXMLException(e);\r
- }\r
- }\r
- if (pict instanceof CTPicture) {\r
- pics.add((CTPicture) pict);\r
- }\r
- }\r
- return pics;\r
- }
-\r
- /**\r
- * Get the currently used CTR object\r
- *\r
- * @return ctr object\r
- */\r
- @Internal\r
+ // Do we have any embedded pictures?
+ // (They're a different CTPicture, under the drawingml namespace)
+ pictures = new ArrayList<XWPFPicture>();
+ for (XmlObject o : pictTextObjs) {
+ for (CTPicture pict : getCTPictures(o)) {
+ XWPFPicture picture = new XWPFPicture(pict, this);
+ pictures.add(picture);
+ }
+ }
+ }
+
+ ;
+
+ /**
+ * @deprecated Use {@link XWPFRun#XWPFRun(CTR, IRunBody)}
+ */
+ public XWPFRun(CTR r, XWPFParagraph p) {
+ this(r, (IRunBody) p);
+ }
+
+ /**
+ * Add the xml:spaces="preserve" attribute if the string has leading or trailing white spaces
+ *
+ * @param xs the string to check
+ */
+ static void preserveSpaces(XmlString xs) {
+ String text = xs.getStringValue();
+ if (text != null && (text.startsWith(" ") || text.endsWith(" "))) {
+ XmlCursor c = xs.newCursor();
+ c.toNextToken();
+ c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve");
+ c.dispose();
+ }
+ }
+
+ private List<CTPicture> getCTPictures(XmlObject o) {
+ List<CTPicture> pics = new ArrayList<CTPicture>();
+ XmlObject[] picts = o.selectPath("declare namespace pic='" + CTPicture.type.getName().getNamespaceURI() + "' .//pic:pic");
+ for (XmlObject pict : picts) {
+ if (pict instanceof XmlAnyTypeImpl) {
+ // Pesky XmlBeans bug - see Bugzilla #49934
+ try {
+ pict = CTPicture.Factory.parse(pict.toString(), DEFAULT_XML_OPTIONS);
+ } catch (XmlException e) {
+ throw new POIXMLException(e);
+ }
+ }
+ if (pict instanceof CTPicture) {
+ pics.add((CTPicture) pict);
+ }
+ }
+ return pics;
+ }
+
+ /**
+ * Get the currently used CTR object
+ *
+ * @return ctr object
+ */
+ @Internal
public CTR getCTR() {
return run;
}
-\r
- /**\r
- * Get the currently referenced paragraph/SDT object\r
- *\r
- * @return current parent\r
- */\r
- public IRunBody getParent() {\r
- return parent;\r
- }\r
-\r
- /**\r
- * Get the currently referenced paragraph, or null if a SDT object\r
- *\r
- * @deprecated use {@link XWPFRun#getParent()} instead\r
- */\r
- public XWPFParagraph getParagraph() {\r
- if (parent instanceof XWPFParagraph)\r
- return (XWPFParagraph) parent;\r
- return null;\r
- }\r
-\r
- /**\r
- * @return The {@link XWPFDocument} instance, this run belongs to, or\r
- * <code>null</code> if parent structure (paragraph > document) is not properly set.\r
- */\r
- public XWPFDocument getDocument() {\r
- if (parent != null) {\r
+
+ /**
+ * Get the currently referenced paragraph/SDT object
+ *
+ * @return current parent
+ */
+ public IRunBody getParent() {
+ return parent;
+ }
+
+ /**
+ * Get the currently referenced paragraph, or null if a SDT object
+ *
+ * @deprecated use {@link XWPFRun#getParent()} instead
+ */
+ public XWPFParagraph getParagraph() {
+ if (parent instanceof XWPFParagraph)
+ return (XWPFParagraph) parent;
+ return null;
+ }
+
+ /**
+ * @return The {@link XWPFDocument} instance, this run belongs to, or
+ * <code>null</code> if parent structure (paragraph > document) is not properly set.
+ */
+ public XWPFDocument getDocument() {
+ if (parent != null) {
return parent.getDocument();
}
return null;
}
/**
- * For isBold, isItalic etc\r
- */\r
- private boolean isCTOnOff(CTOnOff onoff) {\r
- if (!onoff.isSetVal())\r
- return true;\r
- if (onoff.getVal() == STOnOff.ON)\r
- return true;\r
- if (onoff.getVal() == STOnOff.TRUE)\r
- return true;\r
- return false;\r
- }\r
+ * For isBold, isItalic etc
+ */
+ private boolean isCTOnOff(CTOnOff onoff) {
+ if (!onoff.isSetVal())
+ return true;
+ if (onoff.getVal() == STOnOff.ON)
+ return true;
+ if (onoff.getVal() == STOnOff.TRUE)
+ return true;
+ return false;
+ }
/**
* Whether the bold property shall be applied to all non-complex script
* characters in the contents of this run when displayed in a document
*
* @return <code>true</code> if the bold property is applied
- */\r
- public boolean isBold() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetB()) {\r
- return false;\r
- }\r
- return isCTOnOff(pr.getB());\r
- }
-\r
- /**\r
- * Whether the bold property shall be applied to all non-complex script\r
- * characters in the contents of this run when displayed in a document.\r
- * <p>\r
- * This formatting property is a toggle property, which specifies that its\r
- * behavior differs between its use within a style definition and its use as\r
+ */
+ public boolean isBold() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetB()) {
+ return false;
+ }
+ return isCTOnOff(pr.getB());
+ }
+
+ /**
+ * Whether the bold property shall be applied to all non-complex script
+ * characters in the contents of this run when displayed in a document.
+ * <p>
+ * This formatting property is a toggle property, which specifies that its
+ * behavior differs between its use within a style definition and its use as
* direct formatting. When used as part of a style definition, setting this
* property shall toggle the current state of that property as specified up
* to this point in the hierarchy (i.e. applied to not applied, and vice
}
/**
- * Get text color. The returned value is a string in the hex form "RRGGBB".\r
- */\r
- public String getColor() {\r
- String color = null;\r
- if (run.isSetRPr()) {\r
- CTRPr pr = run.getRPr();\r
- if (pr.isSetColor()) {\r
- CTColor clr = pr.getColor();\r
- color = clr.xgetVal().getStringValue();\r
- }\r
- }\r
- return color;\r
- }\r
-\r
- /**\r
- * Set text color.\r
- *\r
- * @param rgbStr - the desired color, in the hex form "RRGGBB".\r
- */\r
- public void setColor(String rgbStr) {\r
+ * Get text color. The returned value is a string in the hex form "RRGGBB".
+ */
+ public String getColor() {
+ String color = null;
+ if (run.isSetRPr()) {
+ CTRPr pr = run.getRPr();
+ if (pr.isSetColor()) {
+ CTColor clr = pr.getColor();
+ color = clr.xgetVal().getStringValue();
+ }
+ }
+ return color;
+ }
+
+ /**
+ * Set text color.
+ *
+ * @param rgbStr - the desired color, in the hex form "RRGGBB".
+ */
+ public void setColor(String rgbStr) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTColor color = pr.isSetColor() ? pr.getColor() : pr.addNewColor();
color.setVal(rgbStr);
/**
* Sets the text of this text run
*
- * @param value the literal text which shall be displayed in the document\r
- */\r
- public void setText(String value) {\r
- setText(value, run.sizeOfTArray());\r
- }\r
-\r
- /**\r
- * Sets the text of this text run in the\r
- *\r
- * @param value the literal text which shall be displayed in the document\r
- * @param pos - position in the text array (NB: 0 based)\r
- */\r
- public void setText(String value, int pos) {\r
- if (pos > run.sizeOfTArray())\r
- throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)");\r
- CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT();\r
- t.setStringValue(value);\r
- preserveSpaces(t);\r
+ * @param value the literal text which shall be displayed in the document
+ */
+ public void setText(String value) {
+ setText(value, run.sizeOfTArray());
+ }
+
+ /**
+ * Sets the text of this text run in the
+ *
+ * @param value the literal text which shall be displayed in the document
+ * @param pos - position in the text array (NB: 0 based)
+ */
+ public void setText(String value, int pos) {
+ 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);
}
/**
* characters in the contents of this run when displayed in a document.
*
* @return <code>true</code> if the italic property is applied
- */\r
- public boolean isItalic() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetI())\r
- return false;\r
- return isCTOnOff(pr.getI());\r
- }\r
+ */
+ public boolean isItalic() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetI())
+ return false;
+ return isCTOnOff(pr.getI());
+ }
/**
* Whether the bold property shall be applied to all non-complex script
*
* @return the Underline pattern applyed to this run
* @see UnderlinePatterns
- */\r
- public UnderlinePatterns getUnderline() {\r
- CTRPr pr = run.getRPr();\r
- return (pr != null && pr.isSetU() && pr.getU().getVal() != null)\r
- ? UnderlinePatterns.valueOf(pr.getU().getVal().intValue())\r
- : UnderlinePatterns.NONE;\r
- }\r
-\r
+ */
+ public UnderlinePatterns getUnderline() {
+ CTRPr pr = run.getRPr();
+ return (pr != null && pr.isSetU() && pr.getU().getVal() != null)
+ ? UnderlinePatterns.valueOf(pr.getU().getVal().intValue())
+ : UnderlinePatterns.NONE;
+ }
+
/**
* Specifies that the contents of this run should be displayed along with an
* underline appearing directly below the character heigh
* horizontal line through the center of the line.
*
* @return <code>true</code> if the strike property is applied
- */\r
- public boolean isStrikeThrough() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetStrike())\r
- return false;\r
- return isCTOnOff(pr.getStrike());\r
- }\r
-\r
- /**\r
- * Specifies that the contents of this run shall be displayed with a single\r
+ */
+ public boolean isStrikeThrough() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetStrike())
+ return false;
+ return isCTOnOff(pr.getStrike());
+ }
+
+ /**
+ * Specifies that the contents of this run shall be displayed with a single
* horizontal line through the center of the line.
* <p/>
* This formatting property is a toggle property, which specifies that its
*/
public void setStrikeThrough(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
- CTOnOff strike = pr.isSetStrike() ? pr.getStrike() : pr.addNewStrike();\r
- strike.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);\r
- }\r
-\r
- @Deprecated\r
- public boolean isStrike() {\r
- return isStrikeThrough();\r
- }\r
-\r
- @Deprecated\r
- public void setStrike(boolean value) {\r
- setStrikeThrough(value);\r
- }\r
-\r
- /**\r
- * Specifies that the contents of this run shall be displayed with a double\r
- * horizontal line through the center of the line.\r
- *\r
- * @return <code>true</code> if the double strike property is applied\r
- */\r
- public boolean isDoubleStrikeThrough() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetDstrike())\r
- return false;\r
- return isCTOnOff(pr.getDstrike());\r
- }\r
-\r
- /**\r
- * Specifies that the contents of this run shall be displayed with a\r
- * double horizontal line through the center of the line.\r
- *\r
- * @see #setStrikeThrough(boolean) for the rules about this\r
- */\r
- public void setDoubleStrikethrough(boolean value) {\r
+ CTOnOff strike = pr.isSetStrike() ? pr.getStrike() : pr.addNewStrike();
+ strike.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
+ }
+
+ @Deprecated
+ public boolean isStrike() {
+ return isStrikeThrough();
+ }
+
+ @Deprecated
+ public void setStrike(boolean value) {
+ setStrikeThrough(value);
+ }
+
+ /**
+ * Specifies that the contents of this run shall be displayed with a double
+ * horizontal line through the center of the line.
+ *
+ * @return <code>true</code> if the double strike property is applied
+ */
+ public boolean isDoubleStrikeThrough() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetDstrike())
+ return false;
+ return isCTOnOff(pr.getDstrike());
+ }
+
+ /**
+ * Specifies that the contents of this run shall be displayed with a
+ * double horizontal line through the center of the line.
+ *
+ * @see #setStrikeThrough(boolean) for the rules about this
+ */
+ 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);
}
-\r
- public boolean isSmallCaps() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetSmallCaps())\r
- return false;\r
- return isCTOnOff(pr.getSmallCaps());\r
- }\r
-\r
- public void setSmallCaps(boolean value) {\r
- CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();\r
- CTOnOff caps = pr.isSetSmallCaps() ? pr.getSmallCaps() : pr.addNewSmallCaps();\r
- caps.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);\r
- }\r
-\r
- public boolean isCapitalized() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetCaps())\r
- return false;\r
- return isCTOnOff(pr.getCaps());\r
- }\r
-\r
- public void setCapitalized(boolean value) {\r
- CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();\r
- CTOnOff caps = pr.isSetCaps() ? pr.getCaps() : pr.addNewCaps();\r
- caps.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);\r
- }\r
-\r
- public boolean isShadowed() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetShadow())\r
- return false;\r
- return isCTOnOff(pr.getShadow());\r
- }\r
-\r
- public void setShadow(boolean value) {\r
- CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();\r
- CTOnOff shadow = pr.isSetShadow() ? pr.getShadow() : pr.addNewShadow();\r
- shadow.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);\r
- }\r
-\r
- public boolean isImprinted() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetImprint())\r
- return false;\r
- return isCTOnOff(pr.getImprint());\r
- }\r
-\r
- public void setImprinted(boolean value) {\r
- CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();\r
- CTOnOff imprinted = pr.isSetImprint() ? pr.getImprint() : pr.addNewImprint();\r
- imprinted.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);\r
- }\r
-\r
- public boolean isEmbossed() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetEmboss())\r
- return false;\r
- return isCTOnOff(pr.getEmboss());\r
- }\r
-\r
- public void setEmbossed(boolean value) {\r
- CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();\r
- CTOnOff emboss = pr.isSetEmboss() ? pr.getEmboss() : pr.addNewEmboss();\r
- emboss.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);\r
- }\r
-\r
- /**\r
- * Specifies the alignment which shall be applied to the contents of this\r
- * run in relation to the default appearance of the run's text.\r
+
+ public boolean isSmallCaps() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetSmallCaps())
+ return false;
+ return isCTOnOff(pr.getSmallCaps());
+ }
+
+ 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);
+ }
+
+ public boolean isCapitalized() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetCaps())
+ return false;
+ return isCTOnOff(pr.getCaps());
+ }
+
+ 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);
+ }
+
+ public boolean isShadowed() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetShadow())
+ return false;
+ return isCTOnOff(pr.getShadow());
+ }
+
+ 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);
+ }
+
+ public boolean isImprinted() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetImprint())
+ return false;
+ return isCTOnOff(pr.getImprint());
+ }
+
+ 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);
+ }
+
+ public boolean isEmbossed() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetEmboss())
+ return false;
+ return isCTOnOff(pr.getEmboss());
+ }
+
+ public void setEmbossed(boolean value) {
+ CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
+ CTOnOff emboss = pr.isSetEmboss() ? pr.getEmboss() : pr.addNewEmboss();
+ emboss.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
+ }
+
+ /**
+ * Specifies the alignment which shall be applied to the contents of this
+ * run in relation to the default appearance of the run's text.
* This allows the text to be repositioned as subscript or superscript without
* altering the font size of the run properties.
*
CTVerticalAlignRun ctValign = pr.isSetVertAlign() ? pr.getVertAlign() : pr.addNewVertAlign();
ctValign.setVal(STVerticalAlignRun.Enum.forInt(valign.getValue()));
}
-\r
- public int getKerning() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetKern())\r
- return 0;\r
- return pr.getKern().getVal().intValue();\r
- }\r
-\r
- public void setKerning(int kern) {\r
- CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();\r
- CTHpsMeasure kernmes = pr.isSetKern() ? pr.getKern() : pr.addNewKern();\r
- kernmes.setVal(BigInteger.valueOf(kern));\r
- }\r
-\r
+
+ public int getKerning() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetKern())
+ return 0;
+ return pr.getKern().getVal().intValue();
+ }
+
+ 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));
+ }
+
public boolean isHighlighted() {
CTRPr pr = run.getRPr();
if (pr == null || !pr.isSetHighlight())
// TODO Provide a wrapper round STHighlightColor, then expose getter/setter
// for the highlight colour. Ideally also then add to CharacterRun interface
- public int getCharacterSpacing() {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetSpacing())\r
- return 0;\r
- return pr.getSpacing().getVal().intValue();\r
- }\r
-\r
- public void setCharacterSpacing(int twips) {\r
- CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();\r
- CTSignedTwipsMeasure spc = pr.isSetSpacing() ? pr.getSpacing() : pr.addNewSpacing();\r
- spc.setVal(BigInteger.valueOf(twips));\r
- }\r
-\r
- /**\r
- * Gets the fonts which shall be used to display the text contents of\r
- * this run. Specifies a font which shall be used to format all characters\r
+ public int getCharacterSpacing() {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetSpacing())
+ return 0;
+ return pr.getSpacing().getVal().intValue();
+ }
+
+ public void setCharacterSpacing(int twips) {
+ CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
+ CTSignedTwipsMeasure spc = pr.isSetSpacing() ? pr.getSpacing() : pr.addNewSpacing();
+ spc.setVal(BigInteger.valueOf(twips));
+ }
+
+ /**
+ * Gets the fonts which shall be used to display the text contents of
+ * this run. Specifies a font which shall be used to format all characters
* in the ASCII range (0 - 127) within the parent run
*
* @return a string representing the font family
*/
- public String getFontFamily() {\r
- return getFontFamily(null);\r
- }\r
-\r
- /**\r
- * Specifies the fonts which shall be used to display the text contents of\r
- * this run. Specifies a font which shall be used to format all characters\r
- * in the ASCII range (0 - 127) within the parent run.\r
- * <p/>\r
- * Also sets the other font ranges, if they haven't been set before\r
- *\r
- * @param fontFamily\r
- * @see FontCharRange\r
- */\r
- public void setFontFamily(String fontFamily) {\r
- setFontFamily(fontFamily, null);\r
- }\r
-\r
- /**\r
- * Alias for {@link #getFontFamily()}\r
- */\r
- public String getFontName() {\r
+ public String getFontFamily() {
+ return getFontFamily(null);
+ }
+
+ /**
+ * Specifies the fonts which shall be used to display the text contents of
+ * this run. Specifies a font which shall be used to format all characters
+ * in the ASCII range (0 - 127) within the parent run.
+ * <p/>
+ * Also sets the other font ranges, if they haven't been set before
+ *
+ * @param fontFamily
+ * @see FontCharRange
+ */
+ public void setFontFamily(String fontFamily) {
+ setFontFamily(fontFamily, null);
+ }
+
+ /**
+ * Alias for {@link #getFontFamily()}
+ */
+ public String getFontName() {
return getFontFamily();
}
/**
* Gets the font family for the specified font char range.
- * If fcr is null, the font char range "ascii" is used\r
- *\r
- * @param fcr the font char range, defaults to "ansi"\r
- * @return a string representing the font famil\r
- */\r
- public String getFontFamily(FontCharRange fcr) {\r
- CTRPr pr = run.getRPr();\r
- if (pr == null || !pr.isSetRFonts()) return null;\r
-\r
- CTFonts fonts = pr.getRFonts();\r
- switch (fcr == null ? FontCharRange.ascii : fcr) {\r
- default:\r
- case ascii:\r
- return fonts.getAscii();\r
- case cs:\r
- return fonts.getCs();\r
- case eastAsia:\r
- return fonts.getEastAsia();\r
- case hAnsi:\r
- return fonts.getHAnsi();\r
- }\r
- }\r
-\r
- /**\r
- * Specifies the fonts which shall be used to display the text contents of\r
- * this run. The default handling for fcr == null is to overwrite the\r
- * ascii font char range with the given font family and also set all not\r
- * specified font ranges\r
- *\r
- * @param fontFamily\r
- * @param fcr FontCharRange or null for default handling\r
- */\r
- public void setFontFamily(String fontFamily, FontCharRange fcr) {\r
- CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();\r
- CTFonts fonts = pr.isSetRFonts() ? pr.getRFonts() : pr.addNewRFonts();\r
-\r
- if (fcr == null) {\r
- fonts.setAscii(fontFamily);\r
- if (!fonts.isSetHAnsi()) {\r
+ * If fcr is null, the font char range "ascii" is used
+ *
+ * @param fcr the font char range, defaults to "ansi"
+ * @return a string representing the font famil
+ */
+ public String getFontFamily(FontCharRange fcr) {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetRFonts()) return null;
+
+ CTFonts fonts = pr.getRFonts();
+ switch (fcr == null ? FontCharRange.ascii : fcr) {
+ default:
+ case ascii:
+ return fonts.getAscii();
+ case cs:
+ return fonts.getCs();
+ case eastAsia:
+ return fonts.getEastAsia();
+ case hAnsi:
+ return fonts.getHAnsi();
+ }
+ }
+
+ /**
+ * Specifies the fonts which shall be used to display the text contents of
+ * this run. The default handling for fcr == null is to overwrite the
+ * ascii font char range with the given font family and also set all not
+ * specified font ranges
+ *
+ * @param fontFamily
+ * @param fcr FontCharRange or null for default handling
+ */
+ public void setFontFamily(String fontFamily, FontCharRange fcr) {
+ CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
+ CTFonts fonts = pr.isSetRFonts() ? pr.getRFonts() : pr.addNewRFonts();
+
+ if (fcr == null) {
+ fonts.setAscii(fontFamily);
+ if (!fonts.isSetHAnsi()) {
fonts.setHAnsi(fontFamily);
}
if (!fonts.isSetCs()) {
}
if (!fonts.isSetEastAsia()) {
fonts.setEastAsia(fontFamily);
- }\r
- } else {\r
- switch (fcr) {\r
- case ascii:\r
- fonts.setAscii(fontFamily);\r
- break;\r
- case cs:\r
- fonts.setCs(fontFamily);\r
- break;\r
- case eastAsia:\r
- fonts.setEastAsia(fontFamily);\r
- break;\r
- case hAnsi:\r
- fonts.setHAnsi(fontFamily);\r
- break;\r
- }\r
- }\r
- }\r
+ }
+ } else {
+ switch (fcr) {
+ case ascii:
+ fonts.setAscii(fontFamily);
+ break;
+ case cs:
+ fonts.setCs(fontFamily);
+ break;
+ case eastAsia:
+ fonts.setEastAsia(fontFamily);
+ break;
+ case hAnsi:
+ fonts.setHAnsi(fontFamily);
+ break;
+ }
+ }
+ }
/**
* Specifies the font size which shall be applied to all non complex script
* be used for non complex script characters.
* </p>
*
- * @param size\r
- */\r
- public void setFontSize(int size) {\r
- BigInteger bint = new BigInteger("" + size);\r
- CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();\r
- CTHpsMeasure ctSize = pr.isSetSz() ? pr.getSz() : pr.addNewSz();\r
- ctSize.setVal(bint.multiply(new BigInteger("2")));\r
+ * @param size
+ */
+ public void setFontSize(int size) {
+ BigInteger bint = new BigInteger("" + size);
+ CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
+ CTHpsMeasure ctSize = pr.isSetSz() ? pr.getSz() : pr.addNewSz();
+ ctSize.setVal(bint.multiply(new BigInteger("2")));
}
/**
* contents of this run.
* </p>
*
- * @param val\r
- */\r
- public void setTextPosition(int val) {\r
- BigInteger bint = new BigInteger("" + val);\r
- CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();\r
- CTSignedHpsMeasure position = pr.isSetPosition() ? pr.getPosition() : pr.addNewPosition();\r
- position.setVal(bint);\r
- }\r
-\r
- /**\r
- *\r
- */\r
- public void removeBreak() {\r
- // TODO\r
- }
-\r
- /**\r
- * Specifies that a break shall be placed at the current location in the run\r
- * content.\r
- * A break is a special character which is used to override the\r
- * normal line breaking that would be performed based on the normal layout\r
- * of the document's contents.\r
- *\r
- * @see #addCarriageReturn()\r
- */\r
- public void addBreak() {\r
- run.addNewBr();\r
- }\r
-\r
- /**\r
- * Specifies that a break shall be placed at the current location in the run\r
+ * @param val
+ */
+ public void setTextPosition(int val) {
+ BigInteger bint = new BigInteger("" + val);
+ CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
+ CTSignedHpsMeasure position = pr.isSetPosition() ? pr.getPosition() : pr.addNewPosition();
+ position.setVal(bint);
+ }
+
+ /**
+ *
+ */
+ public void removeBreak() {
+ // TODO
+ }
+
+ /**
+ * Specifies that a break shall be placed at the current location in the run
+ * content.
+ * A break is a special character which is used to override the
+ * normal line breaking that would be performed based on the normal layout
+ * of the document's contents.
+ *
+ * @see #addCarriageReturn()
+ */
+ public void addBreak() {
+ run.addNewBr();
+ }
+
+ /**
+ * Specifies that a break shall be placed at the current location in the run
* content.
* A break is a special character which is used to override the
* normal line breaking that would be performed based on the normal layout
* of the document's contents.
* <p>
* The behavior of this break character (the
- * location where text shall be restarted after this break) shall be\r
- * determined by its type values.\r
- * </p>\r
- *\r
- * @see BreakType\r
- */\r
- public void addBreak(BreakType type) {\r
- CTBr br = run.addNewBr();\r
- br.setType(STBrType.Enum.forInt(type.getValue()));\r
- }\r
-\r
+ * location where text shall be restarted after this break) shall be
+ * determined by its type values.
+ * </p>
+ *
+ * @see BreakType
+ */
+ public void addBreak(BreakType type) {
+ CTBr br = run.addNewBr();
+ br.setType(STBrType.Enum.forInt(type.getValue()));
+ }
+
/**
* Specifies that a break shall be placed at the current location in the run
* content. A break is a special character which is used to override the
* of the document's contents.
* <p>
* The behavior of this break character (the
- * location where text shall be restarted after this break) shall be\r
- * determined by its type (in this case is BreakType.TEXT_WRAPPING as default) and clear attribute values.\r
- * </p>\r
- *\r
- * @see BreakClear\r
- */\r
- public void addBreak(BreakClear clear) {\r
- CTBr br = run.addNewBr();\r
- br.setType(STBrType.Enum.forInt(BreakType.TEXT_WRAPPING.getValue()));\r
- br.setClear(STBrClear.Enum.forInt(clear.getValue()));\r
- }\r
-\r
- /**\r
- * Specifies that a tab shall be placed at the current location in\r
- * the run content.\r
- */\r
- public void addTab() {\r
- run.addNewTab();\r
- }
-\r
- public void removeTab() {\r
- //TODO\r
- }\r
-\r
- /**\r
- * Specifies that a carriage return shall be placed at the\r
+ * location where text shall be restarted after this break) shall be
+ * determined by its type (in this case is BreakType.TEXT_WRAPPING as default) and clear attribute values.
+ * </p>
+ *
+ * @see BreakClear
+ */
+ public void addBreak(BreakClear clear) {
+ CTBr br = run.addNewBr();
+ br.setType(STBrType.Enum.forInt(BreakType.TEXT_WRAPPING.getValue()));
+ br.setClear(STBrClear.Enum.forInt(clear.getValue()));
+ }
+
+ /**
+ * Specifies that a tab shall be placed at the current location in
+ * the run content.
+ */
+ public void addTab() {
+ run.addNewTab();
+ }
+
+ public void removeTab() {
+ //TODO
+ }
+
+ /**
+ * Specifies that a carriage return shall be placed at the
* current location in the run content.
* A carriage return is used to end the current line of text in
* Wordprocess.
public void addCarriageReturn() {
run.addNewCr();
}
-\r
- public void removeCarriageReturn() {\r
- //TODO\r
- }\r
-\r
- /**\r
- * Adds a picture to the run. This method handles\r
- * attaching the picture data to the overall file.\r
- *\r
- * @param pictureData The raw picture data\r
- * @param pictureType The type of the picture, eg {@link Document#PICTURE_TYPE_JPEG}\r
- * @param width width in EMUs. To convert to / from points use {@link org.apache.poi.util.Units}\r
- * @param height height in EMUs. To convert to / from points use {@link org.apache.poi.util.Units}\r
- * @throws org.apache.poi.openxml4j.exceptions.InvalidFormatException\r
- * @throws IOException\r
- * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_EMF\r
- * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_WMF\r
- * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PICT\r
- * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_JPEG\r
- * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PNG\r
- * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB\r
- */\r
- public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)\r
+
+ public void removeCarriageReturn() {
+ //TODO
+ }
+
+ /**
+ * Adds a picture to the run. This method handles
+ * attaching the picture data to the overall file.
+ *
+ * @param pictureData The raw picture data
+ * @param pictureType The type of the picture, eg {@link Document#PICTURE_TYPE_JPEG}
+ * @param width width in EMUs. To convert to / from points use {@link org.apache.poi.util.Units}
+ * @param height height in EMUs. To convert to / from points use {@link org.apache.poi.util.Units}
+ * @throws org.apache.poi.openxml4j.exceptions.InvalidFormatException
+ * @throws IOException
+ * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_EMF
+ * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_WMF
+ * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PICT
+ * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_JPEG
+ * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PNG
+ * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
+ */
+ public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)
throws InvalidFormatException, IOException {
String relationId;
XWPFPictureData picData;
try {
CTDrawing drawing = run.addNewDrawing();
CTInline inline = drawing.addNewInline();
-\r
- // Do the fiddly namespace bits on the inline\r
- // (We need full control of what goes where and as what)\r
- String xml =\r
- "<a:graphic xmlns:a=\"" + CTGraphicalObject.type.getName().getNamespaceURI() + "\">" +\r
- "<a:graphicData uri=\"" + CTPicture.type.getName().getNamespaceURI() + "\">" +\r
- "<pic:pic xmlns:pic=\"" + CTPicture.type.getName().getNamespaceURI() + "\" />" +\r
- "</a:graphicData>" +\r
+
+ // Do the fiddly namespace bits on the inline
+ // (We need full control of what goes where and as what)
+ String xml =
+ "<a:graphic xmlns:a=\"" + CTGraphicalObject.type.getName().getNamespaceURI() + "\">" +
+ "<a:graphicData uri=\"" + CTPicture.type.getName().getNamespaceURI() + "\">" +
+ "<pic:pic xmlns:pic=\"" + CTPicture.type.getName().getNamespaceURI() + "\" />" +
+ "</a:graphicData>" +
"</a:graphic>";
InputSource is = new InputSource(new StringReader(xml));
- org.w3c.dom.Document doc = DocumentHelper.readDocument(is);\r
- inline.set(XmlToken.Factory.parse(doc.getDocumentElement(), DEFAULT_XML_OPTIONS));\r
-\r
- // Setup the inline\r
+ org.w3c.dom.Document doc = DocumentHelper.readDocument(is);
+ inline.set(XmlToken.Factory.parse(doc.getDocumentElement(), DEFAULT_XML_OPTIONS));
+
+ // Setup the inline
inline.setDistT(0);
inline.setDistR(0);
inline.setDistB(0);
prstGeom.addNewAvLst();
// Finish up
- XWPFPicture xwpfPicture = new XWPFPicture(pic, this);\r
- pictures.add(xwpfPicture);\r
- return xwpfPicture;\r
- } catch (XmlException e) {\r
- throw new IllegalStateException(e);\r
+ XWPFPicture xwpfPicture = new XWPFPicture(pic, this);
+ pictures.add(xwpfPicture);
+ return xwpfPicture;
+ } catch (XmlException e) {
+ throw new IllegalStateException(e);
} catch (SAXException e) {
throw new IllegalStateException(e);
- }\r
- }\r
-\r
- /**\r
- * Returns the embedded pictures of the run. These\r
- * are pictures which reference an external,\r
- * embedded picture image such as a .png or .jpg\r
- */\r
- public List<XWPFPicture> getEmbeddedPictures() {\r
- return pictures;\r
- }\r
-\r
- /**\r
- * Returns the string version of the text\r
- */\r
- public String toString() {\r
- return text();\r
- }\r
-\r
- /**\r
- * Returns the string version of the text, with tabs and\r
- * carriage returns in place of their xml equivalents.\r
- */\r
- public String text() {\r
- StringBuffer text = new StringBuffer();\r
+ }
+ }
+
+ /**
+ * Returns the embedded pictures of the run. These
+ * are pictures which reference an external,
+ * embedded picture image such as a .png or .jpg
+ */
+ public List<XWPFPicture> getEmbeddedPictures() {
+ return pictures;
+ }
+
+ /**
+ * Returns the string version of the text
+ */
+ public String toString() {
+ return text();
+ }
+
+ /**
+ * Returns the string version of the text, with tabs and
+ * carriage returns in place of their xml equivalents.
+ */
+ public String text() {
+ StringBuffer text = new StringBuffer();
// Grab the text and tabs of the text run
// Do so in a way that preserves the ordering
// come up as instances of CTText, but we don't want them
// in the normal text output
if (!"w:instrText".equals(tagName)) {
- text.append(((CTText) o).getStringValue());\r
- }\r
- }\r
-\r
- // Complex type evaluation (currently only for extraction of check boxes)\r
- if (o instanceof CTFldChar) {\r
- CTFldChar ctfldChar = ((CTFldChar) o);\r
- if (ctfldChar.getFldCharType() == STFldCharType.BEGIN) {\r
- if (ctfldChar.getFfData() != null) {\r
- for (CTFFCheckBox checkBox : ctfldChar.getFfData().getCheckBoxList()) {\r
- if (checkBox.getDefault().getVal() == STOnOff.X_1) {\r
- text.append("|X|");\r
- } else {\r
- text.append("|_|");\r
- }\r
- }\r
- }\r
- }\r
- }\r
-\r
+ text.append(((CTText) o).getStringValue());
+ }
+ }
+
+ // Complex type evaluation (currently only for extraction of check boxes)
+ if (o instanceof CTFldChar) {
+ CTFldChar ctfldChar = ((CTFldChar) o);
+ if (ctfldChar.getFldCharType() == STFldCharType.BEGIN) {
+ if (ctfldChar.getFfData() != null) {
+ for (CTFFCheckBox checkBox : ctfldChar.getFfData().getCheckBoxList()) {
+ if (checkBox.getDefault().getVal() == STOnOff.X_1) {
+ text.append("|X|");
+ } else {
+ text.append("|_|");
+ }
+ }
+ }
+ }
+ }
+
if (o instanceof CTPTab) {
text.append("\t");
}
}
if ("w:cr".equals(tagName) || "cr".equals(tagName)) {
text.append("\n");
- }\r
- }\r
- if (o instanceof CTFtnEdnRef) {\r
- CTFtnEdnRef ftn = (CTFtnEdnRef) o;\r
- String footnoteRef = ftn.getDomNode().getLocalName().equals("footnoteReference") ?\r
- "[footnoteRef:" + ftn.getId().intValue() + "]" : "[endnoteRef:" + ftn.getId().intValue() + "]";\r
- text.append(footnoteRef);\r
- }\r
- }\r
-\r
- c.dispose();\r
-\r
- // Any picture text?\r
- if (pictureText != null && pictureText.length() > 0) {\r
- text.append("\n").append(pictureText);\r
- }\r
-\r
- return text.toString();\r
- }\r
-\r
- /**\r
- * @see <a href="http://msdn.microsoft.com/en-us/library/ff533743(v=office.12).aspx">[MS-OI29500] Run Fonts</a>\r
- */\r
- public static enum FontCharRange {\r
- ascii /* char 0-127 */,\r
- cs /* complex symbol */,\r
- eastAsia /* east asia */,\r
- hAnsi /* high ansi */\r
- }\r
-}\r
+ }
+ }
+ if (o instanceof CTFtnEdnRef) {
+ CTFtnEdnRef ftn = (CTFtnEdnRef) o;
+ String footnoteRef = ftn.getDomNode().getLocalName().equals("footnoteReference") ?
+ "[footnoteRef:" + ftn.getId().intValue() + "]" : "[endnoteRef:" + ftn.getId().intValue() + "]";
+ text.append(footnoteRef);
+ }
+ }
+
+ c.dispose();
+
+ // Any picture text?
+ if (pictureText != null && pictureText.length() > 0) {
+ text.append("\n").append(pictureText);
+ }
+
+ return text.toString();
+ }
+
+ /**
+ * @see <a href="http://msdn.microsoft.com/en-us/library/ff533743(v=office.12).aspx">[MS-OI29500] Run Fonts</a>
+ */
+ public static enum FontCharRange {
+ ascii /* char 0-127 */,
+ cs /* complex symbol */,
+ eastAsia /* east asia */,
+ hAnsi /* high ansi */
+ }
+}