您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

XSSFTextParagraph.java 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import org.apache.poi.util.Internal;
  17. import org.apache.poi.util.Units;
  18. import org.apache.poi.xssf.usermodel.TextAlign;
  19. import org.apache.poi.xssf.model.ParagraphPropertyFetcher;
  20. import org.apache.xmlbeans.XmlObject;
  21. import org.openxmlformats.schemas.drawingml.x2006.main.*;
  22. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape;
  23. import java.awt.Color;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. /**
  28. * Represents a paragraph of text within the containing text body.
  29. * The paragraph is the highest level text separation mechanism.
  30. */
  31. public class XSSFTextParagraph implements Iterable<XSSFTextRun>{
  32. private final CTTextParagraph _p;
  33. private final CTShape _shape;
  34. private final List<XSSFTextRun> _runs;
  35. XSSFTextParagraph(CTTextParagraph p, CTShape ctShape){
  36. _p = p;
  37. _shape = ctShape;
  38. _runs = new ArrayList<XSSFTextRun>();
  39. for(XmlObject ch : _p.selectPath("*")){
  40. if(ch instanceof CTRegularTextRun){
  41. CTRegularTextRun r = (CTRegularTextRun)ch;
  42. _runs.add(new XSSFTextRun(r, this));
  43. } else if (ch instanceof CTTextLineBreak){
  44. CTTextLineBreak br = (CTTextLineBreak)ch;
  45. CTRegularTextRun r = CTRegularTextRun.Factory.newInstance();
  46. r.setRPr(br.getRPr());
  47. r.setT("\n");
  48. _runs.add(new XSSFTextRun(r, this));
  49. } else if (ch instanceof CTTextField){
  50. CTTextField f = (CTTextField)ch;
  51. CTRegularTextRun r = CTRegularTextRun.Factory.newInstance();
  52. r.setRPr(f.getRPr());
  53. r.setT(f.getT());
  54. _runs.add(new XSSFTextRun(r, this));
  55. }
  56. }
  57. }
  58. public String getText(){
  59. StringBuilder out = new StringBuilder();
  60. for (XSSFTextRun r : _runs) {
  61. out.append(r.getText());
  62. }
  63. return out.toString();
  64. }
  65. @Internal
  66. public CTTextParagraph getXmlObject(){
  67. return _p;
  68. }
  69. @Internal
  70. public CTShape getParentShape(){
  71. return _shape;
  72. }
  73. public List<XSSFTextRun> getTextRuns(){
  74. return _runs;
  75. }
  76. public Iterator<XSSFTextRun> iterator(){
  77. return _runs.iterator();
  78. }
  79. /**
  80. * Add a new run of text
  81. *
  82. * @return a new run of text
  83. */
  84. public XSSFTextRun addNewTextRun(){
  85. CTRegularTextRun r = _p.addNewR();
  86. CTTextCharacterProperties rPr = r.addNewRPr();
  87. rPr.setLang("en-US");
  88. XSSFTextRun run = new XSSFTextRun(r, this);
  89. _runs.add(run);
  90. return run;
  91. }
  92. /**
  93. * Insert a line break
  94. *
  95. * @return text run representing this line break ('\n')
  96. */
  97. public XSSFTextRun addLineBreak(){
  98. CTTextLineBreak br = _p.addNewBr();
  99. CTTextCharacterProperties brProps = br.addNewRPr();
  100. if(_runs.size() > 0){
  101. // by default line break has the font size of the last text run
  102. CTTextCharacterProperties prevRun = _runs.get(_runs.size() - 1).getRPr();
  103. brProps.set(prevRun);
  104. }
  105. CTRegularTextRun r = CTRegularTextRun.Factory.newInstance();
  106. r.setRPr(brProps);
  107. r.setT("\n");
  108. XSSFTextRun run = new XSSFLineBreak(r, this, brProps);
  109. _runs.add(run);
  110. return run;
  111. }
  112. /**
  113. * Returns the alignment that is applied to the paragraph.
  114. *
  115. * If this attribute is omitted, then a value of left is implied.
  116. * @return alignment that is applied to the paragraph
  117. */
  118. public TextAlign getTextAlign(){
  119. ParagraphPropertyFetcher<TextAlign> fetcher = new ParagraphPropertyFetcher<TextAlign>(getLevel()){
  120. public boolean fetch(CTTextParagraphProperties props){
  121. if(props.isSetAlgn()){
  122. TextAlign val = TextAlign.values()[props.getAlgn().intValue() - 1];
  123. setValue(val);
  124. return true;
  125. }
  126. return false;
  127. }
  128. };
  129. fetchParagraphProperty(fetcher);
  130. return fetcher.getValue() == null ? TextAlign.LEFT : fetcher.getValue();
  131. }
  132. /**
  133. * Specifies the alignment that is to be applied to the paragraph.
  134. * Possible values for this include left, right, centered, justified and distributed,
  135. * see {@link org.apache.poi.xssf.usermodel.TextAlign}.
  136. *
  137. * @param align text align
  138. */
  139. public void setTextAlign(TextAlign align){
  140. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  141. if(align == null) {
  142. if(pr.isSetAlgn()) pr.unsetAlgn();
  143. } else {
  144. pr.setAlgn(STTextAlignType.Enum.forInt(align.ordinal() + 1));
  145. }
  146. }
  147. /**
  148. * Returns where vertically on a line of text the actual words are positioned. This deals
  149. * with vertical placement of the characters with respect to the baselines.
  150. *
  151. * If this attribute is omitted, then a value of baseline is implied.
  152. * @return alignment that is applied to the paragraph
  153. */
  154. public TextFontAlign getTextFontAlign(){
  155. ParagraphPropertyFetcher<TextFontAlign> fetcher = new ParagraphPropertyFetcher<TextFontAlign>(getLevel()){
  156. public boolean fetch(CTTextParagraphProperties props){
  157. if(props.isSetFontAlgn()){
  158. TextFontAlign val = TextFontAlign.values()[props.getFontAlgn().intValue() - 1];
  159. setValue(val);
  160. return true;
  161. }
  162. return false;
  163. }
  164. };
  165. fetchParagraphProperty(fetcher);
  166. return fetcher.getValue() == null ? TextFontAlign.BASELINE : fetcher.getValue();
  167. }
  168. /**
  169. * Determines where vertically on a line of text the actual words are positioned. This deals
  170. * with vertical placement of the characters with respect to the baselines. For instance
  171. * having text anchored to the top baseline, anchored to the bottom baseline, centered in
  172. * between, etc.
  173. *
  174. * @param align text font align
  175. */
  176. public void setTextFontAlign(TextFontAlign align){
  177. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  178. if(align == null) {
  179. if(pr.isSetFontAlgn()) pr.unsetFontAlgn();
  180. } else {
  181. pr.setFontAlgn(STTextFontAlignType.Enum.forInt(align.ordinal() + 1));
  182. }
  183. }
  184. /**
  185. * @return the font to be used on bullet characters within a given paragraph
  186. */
  187. public String getBulletFont(){
  188. ParagraphPropertyFetcher<String> fetcher = new ParagraphPropertyFetcher<String>(getLevel()){
  189. public boolean fetch(CTTextParagraphProperties props){
  190. if(props.isSetBuFont()){
  191. setValue(props.getBuFont().getTypeface());
  192. return true;
  193. }
  194. return false;
  195. }
  196. };
  197. fetchParagraphProperty(fetcher);
  198. return fetcher.getValue();
  199. }
  200. public void setBulletFont(String typeface){
  201. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  202. CTTextFont font = pr.isSetBuFont() ? pr.getBuFont() : pr.addNewBuFont();
  203. font.setTypeface(typeface);
  204. }
  205. /**
  206. * @return the character to be used in place of the standard bullet point
  207. */
  208. public String getBulletCharacter(){
  209. ParagraphPropertyFetcher<String> fetcher = new ParagraphPropertyFetcher<String>(getLevel()){
  210. public boolean fetch(CTTextParagraphProperties props){
  211. if(props.isSetBuChar()){
  212. setValue(props.getBuChar().getChar());
  213. return true;
  214. }
  215. return false;
  216. }
  217. };
  218. fetchParagraphProperty(fetcher);
  219. return fetcher.getValue();
  220. }
  221. public void setBulletCharacter(String str){
  222. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  223. CTTextCharBullet c = pr.isSetBuChar() ? pr.getBuChar() : pr.addNewBuChar();
  224. c.setChar(str);
  225. }
  226. /**
  227. *
  228. * @return the color of bullet characters within a given paragraph.
  229. * A <code>null</code> value means to use the text font color.
  230. */
  231. public Color getBulletFontColor(){
  232. ParagraphPropertyFetcher<Color> fetcher = new ParagraphPropertyFetcher<Color>(getLevel()){
  233. public boolean fetch(CTTextParagraphProperties props){
  234. if(props.isSetBuClr()){
  235. if(props.getBuClr().isSetSrgbClr()){
  236. CTSRgbColor clr = props.getBuClr().getSrgbClr();
  237. byte[] rgb = clr.getVal();
  238. setValue(new Color(0xFF & rgb[0], 0xFF & rgb[1], 0xFF & rgb[2]));
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. };
  245. fetchParagraphProperty(fetcher);
  246. return fetcher.getValue();
  247. }
  248. /**
  249. * Set the color to be used on bullet characters within a given paragraph.
  250. *
  251. * @param color the bullet color
  252. */
  253. public void setBulletFontColor(Color color){
  254. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  255. CTColor c = pr.isSetBuClr() ? pr.getBuClr() : pr.addNewBuClr();
  256. CTSRgbColor clr = c.isSetSrgbClr() ? c.getSrgbClr() : c.addNewSrgbClr();
  257. clr.setVal(new byte[]{(byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue()});
  258. }
  259. /**
  260. * Returns the bullet size that is to be used within a paragraph.
  261. * This may be specified in two different ways, percentage spacing and font point spacing:
  262. * <p>
  263. * If bulletSize >= 0, then bulletSize is a percentage of the font size.
  264. * If bulletSize < 0, then it specifies the size in points
  265. * </p>
  266. *
  267. * @return the bullet size
  268. */
  269. public double getBulletFontSize(){
  270. ParagraphPropertyFetcher<Double> fetcher = new ParagraphPropertyFetcher<Double>(getLevel()){
  271. public boolean fetch(CTTextParagraphProperties props){
  272. if(props.isSetBuSzPct()){
  273. setValue(props.getBuSzPct().getVal() * 0.001);
  274. return true;
  275. }
  276. if(props.isSetBuSzPts()){
  277. setValue( - props.getBuSzPts().getVal() * 0.01);
  278. return true;
  279. }
  280. return false;
  281. }
  282. };
  283. fetchParagraphProperty(fetcher);
  284. return fetcher.getValue() == null ? 100 : fetcher.getValue();
  285. }
  286. /**
  287. * Sets the bullet size that is to be used within a paragraph.
  288. * This may be specified in two different ways, percentage spacing and font point spacing:
  289. * <p>
  290. * If bulletSize >= 0, then bulletSize is a percentage of the font size.
  291. * If bulletSize < 0, then it specifies the size in points
  292. * </p>
  293. */
  294. public void setBulletFontSize(double bulletSize){
  295. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  296. if(bulletSize >= 0) {
  297. // percentage
  298. CTTextBulletSizePercent pt = pr.isSetBuSzPct() ? pr.getBuSzPct() : pr.addNewBuSzPct();
  299. pt.setVal((int)(bulletSize*1000));
  300. // unset points if percentage is now set
  301. if(pr.isSetBuSzPts()) pr.unsetBuSzPts();
  302. } else {
  303. // points
  304. CTTextBulletSizePoint pt = pr.isSetBuSzPts() ? pr.getBuSzPts() : pr.addNewBuSzPts();
  305. pt.setVal((int)(-bulletSize*100));
  306. // unset percentage if points is now set
  307. if(pr.isSetBuSzPct()) pr.unsetBuSzPct();
  308. }
  309. }
  310. /**
  311. * Specifies the indent size that will be applied to the first line of text in the paragraph.
  312. *
  313. * @param value the indent in points, -1 to unset indent and use the default of 0.
  314. */
  315. public void setIndent(double value){
  316. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  317. if(value == -1) {
  318. if(pr.isSetIndent()) pr.unsetIndent();
  319. } else {
  320. pr.setIndent(Units.toEMU(value));
  321. }
  322. }
  323. /**
  324. *
  325. * @return the indent applied to the first line of text in the paragraph.
  326. */
  327. public double getIndent(){
  328. ParagraphPropertyFetcher<Double> fetcher = new ParagraphPropertyFetcher<Double>(getLevel()){
  329. public boolean fetch(CTTextParagraphProperties props){
  330. if(props.isSetIndent()){
  331. setValue(Units.toPoints(props.getIndent()));
  332. return true;
  333. }
  334. return false;
  335. }
  336. };
  337. fetchParagraphProperty(fetcher);
  338. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  339. }
  340. /**
  341. * Specifies the left margin of the paragraph. This is specified in addition to the text body
  342. * inset and applies only to this text paragraph. That is the text body inset and the LeftMargin
  343. * attributes are additive with respect to the text position.
  344. *
  345. * @param value the left margin of the paragraph, -1 to clear the margin and use the default of 0.
  346. */
  347. public void setLeftMargin(double value){
  348. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  349. if(value == -1) {
  350. if(pr.isSetMarL()) pr.unsetMarL();
  351. } else {
  352. pr.setMarL(Units.toEMU(value));
  353. }
  354. }
  355. /**
  356. *
  357. * @return the left margin of the paragraph
  358. */
  359. public double getLeftMargin(){
  360. ParagraphPropertyFetcher<Double> fetcher = new ParagraphPropertyFetcher<Double>(getLevel()){
  361. public boolean fetch(CTTextParagraphProperties props){
  362. if(props.isSetMarL()){
  363. double val = Units.toPoints(props.getMarL());
  364. setValue(val);
  365. return true;
  366. }
  367. return false;
  368. }
  369. };
  370. fetchParagraphProperty(fetcher);
  371. // if the marL attribute is omitted, then a value of 347663 is implied
  372. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  373. }
  374. /**
  375. * Specifies the right margin of the paragraph. This is specified in addition to the text body
  376. * inset and applies only to this text paragraph. That is the text body inset and the marR
  377. * attributes are additive with respect to the text position.
  378. *
  379. * @param value the right margin of the paragraph, -1 to clear the margin and use the default of 0.
  380. */
  381. public void setRightMargin(double value){
  382. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  383. if(value == -1) {
  384. if(pr.isSetMarR()) pr.unsetMarR();
  385. } else {
  386. pr.setMarR(Units.toEMU(value));
  387. }
  388. }
  389. /**
  390. *
  391. * @return the right margin of the paragraph
  392. */
  393. public double getRightMargin(){
  394. ParagraphPropertyFetcher<Double> fetcher = new ParagraphPropertyFetcher<Double>(getLevel()){
  395. public boolean fetch(CTTextParagraphProperties props){
  396. if(props.isSetMarR()){
  397. double val = Units.toPoints(props.getMarR());
  398. setValue(val);
  399. return true;
  400. }
  401. return false;
  402. }
  403. };
  404. fetchParagraphProperty(fetcher);
  405. // if the marL attribute is omitted, then a value of 347663 is implied
  406. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  407. }
  408. /**
  409. *
  410. * @return the default size for a tab character within this paragraph in points
  411. */
  412. public double getDefaultTabSize(){
  413. ParagraphPropertyFetcher<Double> fetcher = new ParagraphPropertyFetcher<Double>(getLevel()){
  414. public boolean fetch(CTTextParagraphProperties props){
  415. if(props.isSetDefTabSz()){
  416. double val = Units.toPoints(props.getDefTabSz());
  417. setValue(val);
  418. return true;
  419. }
  420. return false;
  421. }
  422. };
  423. fetchParagraphProperty(fetcher);
  424. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  425. }
  426. public double getTabStop(final int idx){
  427. ParagraphPropertyFetcher<Double> fetcher = new ParagraphPropertyFetcher<Double>(getLevel()){
  428. public boolean fetch(CTTextParagraphProperties props){
  429. if(props.isSetTabLst()){
  430. CTTextTabStopList tabStops = props.getTabLst();
  431. if(idx < tabStops.sizeOfTabArray() ) {
  432. CTTextTabStop ts = tabStops.getTabArray(idx);
  433. double val = Units.toPoints(ts.getPos());
  434. setValue(val);
  435. return true;
  436. }
  437. }
  438. return false;
  439. }
  440. };
  441. fetchParagraphProperty(fetcher);
  442. return fetcher.getValue() == null ? 0. : fetcher.getValue();
  443. }
  444. /**
  445. * Add a single tab stop to be used on a line of text when there are one or more tab characters
  446. * present within the text.
  447. *
  448. * @param value the position of the tab stop relative to the left margin
  449. */
  450. public void addTabStop(double value){
  451. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  452. CTTextTabStopList tabStops = pr.isSetTabLst() ? pr.getTabLst() : pr.addNewTabLst();
  453. tabStops.addNewTab().setPos(Units.toEMU(value));
  454. }
  455. /**
  456. * This element specifies the vertical line spacing that is to be used within a paragraph.
  457. * This may be specified in two different ways, percentage spacing and font point spacing:
  458. * <p>
  459. * If linespacing >= 0, then linespacing is a percentage of normal line height
  460. * If linespacing < 0, the absolute value of linespacing is the spacing in points
  461. * </p>
  462. * Examples:
  463. * <pre><code>
  464. * // spacing will be 120% of the size of the largest text on each line
  465. * paragraph.setLineSpacing(120);
  466. *
  467. * // spacing will be 200% of the size of the largest text on each line
  468. * paragraph.setLineSpacing(200);
  469. *
  470. * // spacing will be 48 points
  471. * paragraph.setLineSpacing(-48.0);
  472. * </code></pre>
  473. *
  474. * @param linespacing the vertical line spacing
  475. */
  476. public void setLineSpacing(double linespacing){
  477. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  478. CTTextSpacing spc = CTTextSpacing.Factory.newInstance();
  479. if(linespacing >= 0) spc.addNewSpcPct().setVal((int)(linespacing*1000));
  480. else spc.addNewSpcPts().setVal((int)(-linespacing*100));
  481. pr.setLnSpc(spc);
  482. }
  483. /**
  484. * Returns the vertical line spacing that is to be used within a paragraph.
  485. * This may be specified in two different ways, percentage spacing and font point spacing:
  486. * <p>
  487. * If linespacing >= 0, then linespacing is a percentage of normal line height.
  488. * If linespacing < 0, the absolute value of linespacing is the spacing in points
  489. * </p>
  490. *
  491. * @return the vertical line spacing.
  492. */
  493. public double getLineSpacing(){
  494. ParagraphPropertyFetcher<Double> fetcher = new ParagraphPropertyFetcher<Double>(getLevel()){
  495. public boolean fetch(CTTextParagraphProperties props){
  496. if(props.isSetLnSpc()){
  497. CTTextSpacing spc = props.getLnSpc();
  498. if(spc.isSetSpcPct()) setValue( spc.getSpcPct().getVal()*0.001 );
  499. else if (spc.isSetSpcPts()) setValue( -spc.getSpcPts().getVal()*0.01 );
  500. return true;
  501. }
  502. return false;
  503. }
  504. };
  505. fetchParagraphProperty(fetcher);
  506. double lnSpc = fetcher.getValue() == null ? 100 : fetcher.getValue();
  507. if(lnSpc > 0) {
  508. // check if the percentage value is scaled
  509. CTTextNormalAutofit normAutofit = _shape.getTxBody().getBodyPr().getNormAutofit();
  510. if(normAutofit != null) {
  511. double scale = 1 - (double)normAutofit.getLnSpcReduction() / 100000;
  512. lnSpc *= scale;
  513. }
  514. }
  515. return lnSpc;
  516. }
  517. /**
  518. * Set the amount of vertical white space that will be present before the paragraph.
  519. * This space is specified in either percentage or points:
  520. * <p>
  521. * If spaceBefore >= 0, then space is a percentage of normal line height.
  522. * If spaceBefore < 0, the absolute value of linespacing is the spacing in points
  523. * </p>
  524. * Examples:
  525. * <pre><code>
  526. * // The paragraph will be formatted to have a spacing before the paragraph text.
  527. * // The spacing will be 200% of the size of the largest text on each line
  528. * paragraph.setSpaceBefore(200);
  529. *
  530. * // The spacing will be a size of 48 points
  531. * paragraph.setSpaceBefore(-48.0);
  532. * </code></pre>
  533. *
  534. * @param spaceBefore the vertical white space before the paragraph.
  535. */
  536. public void setSpaceBefore(double spaceBefore){
  537. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  538. CTTextSpacing spc = CTTextSpacing.Factory.newInstance();
  539. if(spaceBefore >= 0) spc.addNewSpcPct().setVal((int)(spaceBefore*1000));
  540. else spc.addNewSpcPts().setVal((int)(-spaceBefore*100));
  541. pr.setSpcBef(spc);
  542. }
  543. /**
  544. * The amount of vertical white space before the paragraph
  545. * This may be specified in two different ways, percentage spacing and font point spacing:
  546. * <p>
  547. * If spaceBefore >= 0, then space is a percentage of normal line height.
  548. * If spaceBefore < 0, the absolute value of linespacing is the spacing in points
  549. * </p>
  550. *
  551. * @return the vertical white space before the paragraph
  552. */
  553. public double getSpaceBefore(){
  554. ParagraphPropertyFetcher<Double> fetcher = new ParagraphPropertyFetcher<Double>(getLevel()){
  555. public boolean fetch(CTTextParagraphProperties props){
  556. if(props.isSetSpcBef()){
  557. CTTextSpacing spc = props.getSpcBef();
  558. if(spc.isSetSpcPct()) setValue( spc.getSpcPct().getVal()*0.001 );
  559. else if (spc.isSetSpcPts()) setValue( -spc.getSpcPts().getVal()*0.01 );
  560. return true;
  561. }
  562. return false;
  563. }
  564. };
  565. fetchParagraphProperty(fetcher);
  566. double spcBef = fetcher.getValue() == null ? 0 : fetcher.getValue();
  567. return spcBef;
  568. }
  569. /**
  570. * Set the amount of vertical white space that will be present after the paragraph.
  571. * This space is specified in either percentage or points:
  572. * <p>
  573. * If spaceAfter >= 0, then space is a percentage of normal line height.
  574. * If spaceAfter < 0, the absolute value of linespacing is the spacing in points
  575. * </p>
  576. * Examples:
  577. * <pre><code>
  578. * // The paragraph will be formatted to have a spacing after the paragraph text.
  579. * // The spacing will be 200% of the size of the largest text on each line
  580. * paragraph.setSpaceAfter(200);
  581. *
  582. * // The spacing will be a size of 48 points
  583. * paragraph.setSpaceAfter(-48.0);
  584. * </code></pre>
  585. *
  586. * @param spaceAfter the vertical white space after the paragraph.
  587. */
  588. public void setSpaceAfter(double spaceAfter){
  589. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  590. CTTextSpacing spc = CTTextSpacing.Factory.newInstance();
  591. if(spaceAfter >= 0) spc.addNewSpcPct().setVal((int)(spaceAfter*1000));
  592. else spc.addNewSpcPts().setVal((int)(-spaceAfter*100));
  593. pr.setSpcAft(spc);
  594. }
  595. /**
  596. * The amount of vertical white space after the paragraph
  597. * This may be specified in two different ways, percentage spacing and font point spacing:
  598. * <p>
  599. * If spaceBefore >= 0, then space is a percentage of normal line height.
  600. * If spaceBefore < 0, the absolute value of linespacing is the spacing in points
  601. * </p>
  602. *
  603. * @return the vertical white space after the paragraph
  604. */
  605. public double getSpaceAfter(){
  606. ParagraphPropertyFetcher<Double> fetcher = new ParagraphPropertyFetcher<Double>(getLevel()){
  607. public boolean fetch(CTTextParagraphProperties props){
  608. if(props.isSetSpcAft()){
  609. CTTextSpacing spc = props.getSpcAft();
  610. if(spc.isSetSpcPct()) setValue( spc.getSpcPct().getVal()*0.001 );
  611. else if (spc.isSetSpcPts()) setValue( -spc.getSpcPts().getVal()*0.01 );
  612. return true;
  613. }
  614. return false;
  615. }
  616. };
  617. fetchParagraphProperty(fetcher);
  618. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  619. }
  620. /**
  621. * Specifies the particular level text properties that this paragraph will follow.
  622. * The value for this attribute formats the text according to the corresponding level
  623. * paragraph properties defined in the list of styles associated with the body of text
  624. * that this paragraph belongs to (therefore in the parent shape).
  625. * <p>
  626. * Note that the closest properties object to the text is used, therefore if there is
  627. * a conflict between the text paragraph properties and the list style properties for
  628. * this level then the text paragraph properties will take precedence.
  629. * </p>
  630. *
  631. * @param level the level (0 ... 4)
  632. */
  633. public void setLevel(int level){
  634. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  635. pr.setLvl(level);
  636. }
  637. /**
  638. * Returns the level of text properties that this paragraph will follow.
  639. *
  640. * @return the text level of this paragraph (0-based). Default is 0.
  641. */
  642. public int getLevel(){
  643. CTTextParagraphProperties pr = _p.getPPr();
  644. if(pr == null) return 0;
  645. return pr.getLvl();
  646. }
  647. /**
  648. * Returns whether this paragraph has bullets
  649. */
  650. public boolean isBullet() {
  651. ParagraphPropertyFetcher<Boolean> fetcher = new ParagraphPropertyFetcher<Boolean>(getLevel()){
  652. public boolean fetch(CTTextParagraphProperties props){
  653. if (props.isSetBuNone()) {
  654. setValue(false);
  655. return true;
  656. }
  657. if (props.isSetBuFont()) {
  658. if (props.isSetBuChar() || props.isSetBuAutoNum()) {
  659. setValue(true);
  660. return true;
  661. } else {
  662. // Excel treats text with buFont but no char/autonum
  663. // as not bulleted
  664. // Possibly the font is just used if bullets turned on again?
  665. }
  666. }
  667. return false;
  668. }
  669. };
  670. fetchParagraphProperty(fetcher);
  671. return fetcher.getValue() == null ? false : fetcher.getValue();
  672. }
  673. /**
  674. * Set or unset this paragraph as a bullet point
  675. *
  676. * @param flag whether text in this paragraph has bullets
  677. */
  678. public void setBullet(boolean flag) {
  679. if(isBullet() == flag) return;
  680. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  681. if(!flag) {
  682. pr.addNewBuNone();
  683. if(pr.isSetBuAutoNum()) pr.unsetBuAutoNum();
  684. if(pr.isSetBuBlip()) pr.unsetBuBlip();
  685. if(pr.isSetBuChar()) pr.unsetBuChar();
  686. if(pr.isSetBuClr()) pr.unsetBuClr();
  687. if(pr.isSetBuClrTx()) pr.unsetBuClrTx();
  688. if(pr.isSetBuFont()) pr.unsetBuFont();
  689. if(pr.isSetBuFontTx()) pr.unsetBuFontTx();
  690. if(pr.isSetBuSzPct()) pr.unsetBuSzPct();
  691. if(pr.isSetBuSzPts()) pr.unsetBuSzPts();
  692. if(pr.isSetBuSzTx()) pr.unsetBuSzTx();
  693. } else {
  694. if(pr.isSetBuNone()) pr.unsetBuNone();
  695. if(!pr.isSetBuFont()) pr.addNewBuFont().setTypeface("Arial");
  696. if(!pr.isSetBuAutoNum()) pr.addNewBuChar().setChar("\u2022");
  697. }
  698. }
  699. /**
  700. * Set this paragraph as an automatic numbered bullet point
  701. *
  702. * @param scheme type of auto-numbering
  703. * @param startAt the number that will start number for a given sequence of automatically
  704. * numbered bullets (1-based).
  705. */
  706. public void setBullet(ListAutoNumber scheme, int startAt) {
  707. if(startAt < 1) throw new IllegalArgumentException("Start Number must be greater or equal that 1") ;
  708. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  709. CTTextAutonumberBullet lst = pr.isSetBuAutoNum() ? pr.getBuAutoNum() : pr.addNewBuAutoNum();
  710. lst.setType(STTextAutonumberScheme.Enum.forInt(scheme.ordinal() + 1));
  711. lst.setStartAt(startAt);
  712. if(!pr.isSetBuFont()) pr.addNewBuFont().setTypeface("Arial");
  713. if(pr.isSetBuNone()) pr.unsetBuNone();
  714. // remove these elements if present as it results in invalid content when opening in Excel.
  715. if(pr.isSetBuBlip()) pr.unsetBuBlip();
  716. if(pr.isSetBuChar()) pr.unsetBuChar();
  717. }
  718. /**
  719. * Set this paragraph as an automatic numbered bullet point
  720. *
  721. * @param scheme type of auto-numbering
  722. */
  723. public void setBullet(ListAutoNumber scheme) {
  724. CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
  725. CTTextAutonumberBullet lst = pr.isSetBuAutoNum() ? pr.getBuAutoNum() : pr.addNewBuAutoNum();
  726. lst.setType(STTextAutonumberScheme.Enum.forInt(scheme.ordinal() + 1));
  727. if(!pr.isSetBuFont()) pr.addNewBuFont().setTypeface("Arial");
  728. if(pr.isSetBuNone()) pr.unsetBuNone();
  729. // remove these elements if present as it results in invalid content when opening in Excel.
  730. if(pr.isSetBuBlip()) pr.unsetBuBlip();
  731. if(pr.isSetBuChar()) pr.unsetBuChar();
  732. }
  733. /**
  734. * Returns whether this paragraph has automatic numbered bullets
  735. */
  736. public boolean isBulletAutoNumber() {
  737. ParagraphPropertyFetcher<Boolean> fetcher = new ParagraphPropertyFetcher<Boolean>(getLevel()){
  738. public boolean fetch(CTTextParagraphProperties props){
  739. if(props.isSetBuAutoNum()) {
  740. setValue(true);
  741. return true;
  742. }
  743. return false;
  744. }
  745. };
  746. fetchParagraphProperty(fetcher);
  747. return fetcher.getValue() == null ? false : fetcher.getValue();
  748. }
  749. /**
  750. * Returns the starting number if this paragraph has automatic numbered bullets, otherwise returns 0
  751. */
  752. public int getBulletAutoNumberStart() {
  753. ParagraphPropertyFetcher<Integer> fetcher = new ParagraphPropertyFetcher<Integer>(getLevel()){
  754. public boolean fetch(CTTextParagraphProperties props){
  755. if(props.isSetBuAutoNum() && props.getBuAutoNum().isSetStartAt()) {
  756. setValue(props.getBuAutoNum().getStartAt());
  757. return true;
  758. }
  759. return false;
  760. }
  761. };
  762. fetchParagraphProperty(fetcher);
  763. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  764. }
  765. /**
  766. * Returns the auto number scheme if this paragraph has automatic numbered bullets, otherwise returns ListAutoNumber.ARABIC_PLAIN
  767. */
  768. public ListAutoNumber getBulletAutoNumberScheme() {
  769. ParagraphPropertyFetcher<ListAutoNumber> fetcher = new ParagraphPropertyFetcher<ListAutoNumber>(getLevel()){
  770. public boolean fetch(CTTextParagraphProperties props){
  771. if(props.isSetBuAutoNum()) {
  772. setValue(ListAutoNumber.values()[props.getBuAutoNum().getType().intValue() - 1]);
  773. return true;
  774. }
  775. return false;
  776. }
  777. };
  778. fetchParagraphProperty(fetcher);
  779. // Note: documentation does not define a default, return ListAutoNumber.ARABIC_PLAIN (1,2,3...)
  780. return fetcher.getValue() == null ? ListAutoNumber.ARABIC_PLAIN : fetcher.getValue();
  781. }
  782. @SuppressWarnings("rawtypes")
  783. private boolean fetchParagraphProperty(ParagraphPropertyFetcher visitor){
  784. boolean ok = false;
  785. if(_p.isSetPPr()) ok = visitor.fetch(_p.getPPr());
  786. if(!ok) {
  787. ok = visitor.fetch(_shape);
  788. }
  789. return ok;
  790. }
  791. @Override
  792. public String toString(){
  793. return "[" + getClass() + "]" + getText();
  794. }
  795. }