You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XWPFDocument.java 66KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895
  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.xwpf.usermodel;
  16. import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.LinkedList;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Spliterator;
  30. import javax.xml.namespace.QName;
  31. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  32. import org.apache.logging.log4j.LogManager;
  33. import org.apache.logging.log4j.Logger;
  34. import org.apache.poi.common.usermodel.PictureType;
  35. import org.apache.poi.ooxml.POIXMLDocument;
  36. import org.apache.poi.ooxml.POIXMLDocumentPart;
  37. import org.apache.poi.ooxml.POIXMLException;
  38. import org.apache.poi.ooxml.POIXMLProperties;
  39. import org.apache.poi.ooxml.POIXMLRelation;
  40. import org.apache.poi.ooxml.util.IdentifierManager;
  41. import org.apache.poi.ooxml.util.PackageHelper;
  42. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  43. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  44. import org.apache.poi.openxml4j.opc.OPCPackage;
  45. import org.apache.poi.openxml4j.opc.PackagePart;
  46. import org.apache.poi.openxml4j.opc.PackagePartName;
  47. import org.apache.poi.openxml4j.opc.PackageRelationship;
  48. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  49. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  50. import org.apache.poi.openxml4j.opc.TargetMode;
  51. import org.apache.poi.poifs.crypt.HashAlgorithm;
  52. import org.apache.poi.util.IOUtils;
  53. import org.apache.poi.util.Internal;
  54. import org.apache.poi.wp.usermodel.HeaderFooterType;
  55. import org.apache.poi.xddf.usermodel.chart.XDDFChart;
  56. import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
  57. import org.apache.xmlbeans.XmlCursor;
  58. import org.apache.xmlbeans.XmlException;
  59. import org.apache.xmlbeans.XmlObject;
  60. import org.apache.xmlbeans.XmlOptions;
  61. import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1;
  62. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
  63. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
  64. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
  65. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
  66. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
  67. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
  68. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
  69. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
  70. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
  71. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
  72. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
  73. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument;
  74. import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
  75. import org.openxmlformats.schemas.wordprocessingml.x2006.main.EndnotesDocument;
  76. import org.openxmlformats.schemas.wordprocessingml.x2006.main.FootnotesDocument;
  77. import org.openxmlformats.schemas.wordprocessingml.x2006.main.NumberingDocument;
  78. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect;
  79. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr;
  80. import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument;
  81. /**
  82. * High(ish) level class for working with .docx files.
  83. * <p>
  84. * This class tries to hide some of the complexity
  85. * of the underlying file format, but as it's not a
  86. * mature and stable API yet, certain parts of the
  87. * XML structure come through. You'll therefore almost
  88. * certainly need to refer to the OOXML specifications
  89. * from
  90. * http://www.ecma-international.org/publications/standards/Ecma-376.htm
  91. * at some point in your use.
  92. */
  93. @SuppressWarnings("unused")
  94. public class XWPFDocument extends POIXMLDocument implements Document, IBody {
  95. private static final Logger LOG = LogManager.getLogger(XWPFDocument.class);
  96. protected List<XWPFFooter> footers = new ArrayList<>();
  97. protected List<XWPFHeader> headers = new ArrayList<>();
  98. protected List<XWPFHyperlink> hyperlinks = new ArrayList<>();
  99. protected List<XWPFParagraph> paragraphs = new ArrayList<>();
  100. protected List<XWPFTable> tables = new ArrayList<>();
  101. protected List<XWPFSDT> contentControls = new ArrayList<>();
  102. protected List<IBodyElement> bodyElements = new ArrayList<>();
  103. protected List<XWPFPictureData> pictures = new ArrayList<>();
  104. protected Map<Long, List<XWPFPictureData>> packagePictures = new HashMap<>();
  105. protected XWPFEndnotes endnotes;
  106. protected XWPFNumbering numbering;
  107. protected XWPFStyles styles;
  108. protected XWPFFootnotes footnotes;
  109. private CTDocument1 ctDocument;
  110. private XWPFSettings settings;
  111. private XWPFComments comments;
  112. protected final List<XWPFChart> charts = new ArrayList<>();
  113. /**
  114. * Keeps track on all id-values used in this document and included parts, like headers, footers, etc.
  115. */
  116. private final IdentifierManager drawingIdManager = new IdentifierManager(0L, 4294967295L);
  117. private final FootnoteEndnoteIdManager footnoteIdManager = new FootnoteEndnoteIdManager(this);
  118. /**
  119. * Handles the joy of different headers/footers for different pages
  120. */
  121. private XWPFHeaderFooterPolicy headerFooterPolicy;
  122. /**
  123. * @param pkg OPC package
  124. * @throws IOException If reading data from the package fails
  125. * @throws POIXMLException a RuntimeException that can be caused by invalid OOXML data
  126. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  127. * input format
  128. */
  129. public XWPFDocument(OPCPackage pkg) throws IOException {
  130. super(pkg);
  131. //build a tree of POIXMLDocumentParts, this document being the root
  132. load(XWPFFactory.getInstance());
  133. }
  134. /**
  135. * @param is The InputStream to read data from
  136. * @throws IOException If reading data from the stream fails
  137. * @throws POIXMLException a RuntimeException that can be caused by invalid OOXML data
  138. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  139. * input format
  140. */
  141. public XWPFDocument(InputStream is) throws IOException {
  142. super(PackageHelper.open(is));
  143. //build a tree of POIXMLDocumentParts, this workbook being the root
  144. load(XWPFFactory.getInstance());
  145. }
  146. public XWPFDocument() {
  147. super(newPackage());
  148. onDocumentCreate();
  149. }
  150. /**
  151. * Create a new WordProcessingML package and setup the default minimal content
  152. */
  153. protected static OPCPackage newPackage() {
  154. OPCPackage pkg = null;
  155. try {
  156. pkg = OPCPackage.create(new UnsynchronizedByteArrayOutputStream()); // NOSONAR - we do not want to close this here
  157. // Main part
  158. PackagePartName corePartName = PackagingURIHelper.createPartName(XWPFRelation.DOCUMENT.getDefaultFileName());
  159. // Create main part relationship
  160. pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
  161. // Create main document part
  162. pkg.createPart(corePartName, XWPFRelation.DOCUMENT.getContentType());
  163. pkg.getPackageProperties().setCreatorProperty(DOCUMENT_CREATOR);
  164. return pkg;
  165. } catch (Exception e) {
  166. IOUtils.closeQuietly(pkg);
  167. throw new POIXMLException(e);
  168. }
  169. }
  170. @SuppressWarnings("deprecation")
  171. @Override
  172. protected void onDocumentRead() throws IOException {
  173. try {
  174. DocumentDocument doc;
  175. try (InputStream stream = getPackagePart().getInputStream()) {
  176. doc = DocumentDocument.Factory.parse(stream, DEFAULT_XML_OPTIONS);
  177. ctDocument = doc.getDocument();
  178. }
  179. initFootnotes();
  180. // parse the document with cursor and add
  181. // the XmlObject to its lists
  182. try (XmlCursor docCursor = ctDocument.newCursor()) {
  183. docCursor.selectPath("./*");
  184. while (docCursor.toNextSelection()) {
  185. XmlObject o = docCursor.getObject();
  186. if (o instanceof CTBody) {
  187. try (XmlCursor bodyCursor = o.newCursor()) {
  188. bodyCursor.selectPath("./*");
  189. while (bodyCursor.toNextSelection()) {
  190. XmlObject bodyObj = bodyCursor.getObject();
  191. if (bodyObj instanceof CTP) {
  192. XWPFParagraph p = new XWPFParagraph((CTP) bodyObj, this);
  193. bodyElements.add(p);
  194. paragraphs.add(p);
  195. } else if (bodyObj instanceof CTTbl) {
  196. XWPFTable t = new XWPFTable((CTTbl) bodyObj, this);
  197. bodyElements.add(t);
  198. tables.add(t);
  199. } else if (bodyObj instanceof CTSdtBlock) {
  200. XWPFSDT c = new XWPFSDT((CTSdtBlock) bodyObj, this);
  201. bodyElements.add(c);
  202. contentControls.add(c);
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. // Sort out headers and footers
  210. if (doc.getDocument().getBody().getSectPr() != null) {
  211. headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
  212. }
  213. // Create for each XML-part in the Package a PartClass
  214. for (RelationPart rp : getRelationParts()) {
  215. POIXMLDocumentPart p = rp.getDocumentPart();
  216. String relation = rp.getRelationship().getRelationshipType();
  217. if (relation.equals(XWPFRelation.STYLES.getRelation())) {
  218. this.styles = (XWPFStyles) p;
  219. this.styles.onDocumentRead();
  220. } else if (relation.equals(XWPFRelation.NUMBERING.getRelation())) {
  221. this.numbering = (XWPFNumbering) p;
  222. this.numbering.onDocumentRead();
  223. } else if (relation.equals(XWPFRelation.FOOTER.getRelation())) {
  224. XWPFFooter footer = (XWPFFooter) p;
  225. footers.add(footer);
  226. footer.onDocumentRead();
  227. } else if (relation.equals(XWPFRelation.HEADER.getRelation())) {
  228. XWPFHeader header = (XWPFHeader) p;
  229. headers.add(header);
  230. header.onDocumentRead();
  231. } else if (relation.equals(XWPFRelation.COMMENT.getRelation())) {
  232. this.comments = (XWPFComments) p;
  233. this.comments.onDocumentRead();
  234. } else if (relation.equals(XWPFRelation.SETTINGS.getRelation())) {
  235. settings = (XWPFSettings) p;
  236. settings.onDocumentRead();
  237. } else if (relation.equals(XWPFRelation.IMAGES.getRelation())) {
  238. XWPFPictureData picData = (XWPFPictureData) p;
  239. picData.onDocumentRead();
  240. registerPackagePictureData(picData);
  241. pictures.add(picData);
  242. } else if (relation.equals(XWPFRelation.CHART.getRelation())) {
  243. //now we can use all methods to modify charts in XWPFDocument
  244. XWPFChart chartData = (XWPFChart) p;
  245. charts.add(chartData);
  246. } else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) {
  247. // We don't currently process the glossary itself
  248. // Until we do, we do need to load the glossary child parts of it
  249. for (POIXMLDocumentPart gp : p.getRelations()) {
  250. // Trigger the onDocumentRead for all the child parts
  251. // Otherwise we'll hit issues on Styles, Settings etc on save
  252. // TODO: Refactor this to not need to access protected method
  253. // from other package! Remove the static helper method once fixed!!!
  254. POIXMLDocumentPart._invokeOnDocumentRead(gp);
  255. }
  256. }
  257. }
  258. initHyperlinks();
  259. } catch (XmlException e) {
  260. throw new POIXMLException(e);
  261. }
  262. }
  263. private void initHyperlinks() {
  264. // Get the hyperlinks
  265. // TODO: make me optional/separated in private function
  266. try {
  267. hyperlinks = new ArrayList<>();
  268. for (PackageRelationship rel : getPackagePart().getRelationshipsByType(XWPFRelation.HYPERLINK.getRelation())) {
  269. hyperlinks.add(new XWPFHyperlink(rel.getId(), rel.getTargetURI().toString()));
  270. }
  271. } catch (InvalidFormatException e) {
  272. throw new POIXMLException(e);
  273. }
  274. }
  275. private void initFootnotes() throws XmlException, IOException {
  276. for (RelationPart rp : getRelationParts()) {
  277. POIXMLDocumentPart p = rp.getDocumentPart();
  278. String relation = rp.getRelationship().getRelationshipType();
  279. if (relation.equals(XWPFRelation.FOOTNOTE.getRelation())) {
  280. this.footnotes = (XWPFFootnotes) p;
  281. this.footnotes.onDocumentRead();
  282. this.footnotes.setIdManager(footnoteIdManager);
  283. } else if (relation.equals(XWPFRelation.ENDNOTE.getRelation())) {
  284. this.endnotes = (XWPFEndnotes) p;
  285. this.endnotes.onDocumentRead();
  286. this.endnotes.setIdManager(footnoteIdManager);
  287. }
  288. }
  289. }
  290. /**
  291. * Create a new CTWorkbook with all values set to default
  292. */
  293. @Override
  294. protected void onDocumentCreate() {
  295. ctDocument = CTDocument1.Factory.newInstance();
  296. ctDocument.addNewBody();
  297. settings = (XWPFSettings) createRelationship(XWPFRelation.SETTINGS, XWPFFactory.getInstance());
  298. POIXMLProperties.ExtendedProperties expProps = getProperties().getExtendedProperties();
  299. expProps.getUnderlyingProperties().setApplication(DOCUMENT_CREATOR);
  300. }
  301. /**
  302. * Returns the low level document base object
  303. */
  304. @Internal
  305. public CTDocument1 getDocument() {
  306. return ctDocument;
  307. }
  308. IdentifierManager getDrawingIdManager() {
  309. return drawingIdManager;
  310. }
  311. /**
  312. * returns an Iterator with paragraphs and tables
  313. */
  314. @Override
  315. public List<IBodyElement> getBodyElements() {
  316. return Collections.unmodifiableList(bodyElements);
  317. }
  318. public Iterator<IBodyElement> getBodyElementsIterator() {
  319. return bodyElements.iterator();
  320. }
  321. /**
  322. * returns a Spliterator with paragraphs and tables
  323. *
  324. * @since POI 5.2.0
  325. */
  326. public Spliterator<IBodyElement> getBodyElementsSpliterator() {
  327. return bodyElements.spliterator();
  328. }
  329. @Override
  330. public List<XWPFParagraph> getParagraphs() {
  331. return Collections.unmodifiableList(paragraphs);
  332. }
  333. @Override
  334. public List<XWPFTable> getTables() {
  335. return Collections.unmodifiableList(tables);
  336. }
  337. /**
  338. * @return list of XWPFCharts in this document
  339. */
  340. public List<XWPFChart> getCharts() {
  341. return Collections.unmodifiableList(charts);
  342. }
  343. @Override
  344. public XWPFTable getTableArray(int pos) {
  345. if (pos >= 0 && pos < tables.size()) {
  346. return tables.get(pos);
  347. }
  348. return null;
  349. }
  350. /**
  351. * @return the list of footers
  352. */
  353. public List<XWPFFooter> getFooterList() {
  354. return Collections.unmodifiableList(footers);
  355. }
  356. public XWPFFooter getFooterArray(int pos) {
  357. if (pos >= 0 && pos < footers.size()) {
  358. return footers.get(pos);
  359. }
  360. return null;
  361. }
  362. /**
  363. * @return the list of headers
  364. */
  365. public List<XWPFHeader> getHeaderList() {
  366. return Collections.unmodifiableList(headers);
  367. }
  368. public XWPFHeader getHeaderArray(int pos) {
  369. if (pos >= 0 && pos < headers.size()) {
  370. return headers.get(pos);
  371. }
  372. return null;
  373. }
  374. public String getTblStyle(XWPFTable table) {
  375. return table.getStyleID();
  376. }
  377. public XWPFHyperlink getHyperlinkByID(String id) {
  378. for (XWPFHyperlink link : hyperlinks) {
  379. if (link.getId().equals(id)) {
  380. return link;
  381. }
  382. }
  383. // If the link was not found, rebuild the list (maybe a new link was added into the document) and check again.
  384. initHyperlinks();
  385. for (XWPFHyperlink link : hyperlinks) {
  386. if (link.getId().equals(id)) {
  387. return link;
  388. }
  389. }
  390. // Link still not there? Giving up.
  391. return null;
  392. }
  393. public XWPFFootnote getFootnoteByID(int id) {
  394. if (footnotes == null) {
  395. return null;
  396. }
  397. return (XWPFFootnote)footnotes.getFootnoteById(id);
  398. }
  399. public XWPFEndnote getEndnoteByID(int id) {
  400. if (endnotes == null) {
  401. return null;
  402. }
  403. return endnotes.getFootnoteById(id);
  404. }
  405. public List<XWPFFootnote> getFootnotes() {
  406. if (footnotes == null) {
  407. return Collections.emptyList();
  408. }
  409. return footnotes.getFootnotesList();
  410. }
  411. public XWPFHyperlink[] getHyperlinks() {
  412. return hyperlinks.toArray(new XWPFHyperlink[0]);
  413. }
  414. /**
  415. * Get Comments
  416. *
  417. * @return comments
  418. */
  419. public XWPFComments getDocComments() {
  420. return comments;
  421. }
  422. public XWPFComment getCommentByID(String id) {
  423. if (null == comments) {
  424. return null;
  425. }
  426. return comments.getCommentByID(id);
  427. }
  428. public XWPFComment[] getComments() {
  429. if (null == comments) {
  430. return null;
  431. }
  432. return comments.getComments().toArray(new XWPFComment[0]);
  433. }
  434. /**
  435. * Get the document part that's defined as the
  436. * given relationship of the core document.
  437. */
  438. public PackagePart getPartById(String id) {
  439. try {
  440. PackagePart corePart = getCorePart();
  441. return corePart.getRelatedPart(corePart.getRelationship(id));
  442. } catch (InvalidFormatException e) {
  443. throw new IllegalArgumentException(e);
  444. }
  445. }
  446. /**
  447. * Returns the policy on headers and footers, which
  448. * also provides a way to get at them.
  449. */
  450. public XWPFHeaderFooterPolicy getHeaderFooterPolicy() {
  451. return headerFooterPolicy;
  452. }
  453. public XWPFHeaderFooterPolicy createHeaderFooterPolicy() {
  454. if (headerFooterPolicy == null) {
  455. // if (! ctDocument.getBody().isSetSectPr()) {
  456. // ctDocument.getBody().addNewSectPr();
  457. // }
  458. headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
  459. }
  460. return headerFooterPolicy;
  461. }
  462. /**
  463. * Create a header of the given type
  464. *
  465. * @param type {@link HeaderFooterType} enum
  466. * @return object of type {@link XWPFHeader}
  467. */
  468. public XWPFHeader createHeader(HeaderFooterType type) {
  469. XWPFHeaderFooterPolicy hfPolicy = createHeaderFooterPolicy();
  470. // TODO this needs to be migrated out into section code
  471. if (type == HeaderFooterType.FIRST) {
  472. CTSectPr ctSectPr = getSection();
  473. if (!ctSectPr.isSetTitlePg()) {
  474. CTOnOff titlePg = ctSectPr.addNewTitlePg();
  475. titlePg.setVal(STOnOff1.ON);
  476. }
  477. // } else if (type == HeaderFooterType.EVEN) {
  478. // TODO Add support for Even/Odd headings and footers
  479. }
  480. return hfPolicy.createHeader(STHdrFtr.Enum.forInt(type.toInt()));
  481. }
  482. /**
  483. * Create a footer of the given type
  484. *
  485. * @param type {@link HeaderFooterType} enum
  486. * @return object of type {@link XWPFFooter}
  487. */
  488. public XWPFFooter createFooter(HeaderFooterType type) {
  489. XWPFHeaderFooterPolicy hfPolicy = createHeaderFooterPolicy();
  490. // TODO this needs to be migrated out into section code
  491. if (type == HeaderFooterType.FIRST) {
  492. CTSectPr ctSectPr = getSection();
  493. if (!ctSectPr.isSetTitlePg()) {
  494. CTOnOff titlePg = ctSectPr.addNewTitlePg();
  495. titlePg.setVal(STOnOff1.ON);
  496. }
  497. // } else if (type == HeaderFooterType.EVEN) {
  498. // TODO Add support for Even/Odd headings and footers
  499. }
  500. return hfPolicy.createFooter(STHdrFtr.Enum.forInt(type.toInt()));
  501. }
  502. /**
  503. * Return the {@link CTSectPr} object that corresponds with the
  504. * last section in this document.
  505. *
  506. * @return {@link CTSectPr} object
  507. */
  508. private CTSectPr getSection() {
  509. CTBody ctBody = getDocument().getBody();
  510. return (ctBody.isSetSectPr() ?
  511. ctBody.getSectPr() :
  512. ctBody.addNewSectPr());
  513. }
  514. /**
  515. * Returns the styles object used
  516. */
  517. @Internal
  518. public CTStyles getStyle() throws XmlException, IOException {
  519. PackagePart[] parts;
  520. try {
  521. parts = getRelatedByType(XWPFRelation.STYLES.getRelation());
  522. } catch (InvalidFormatException e) {
  523. throw new IllegalStateException(e);
  524. }
  525. if (parts.length != 1) {
  526. throw new IllegalStateException("Expecting one Styles document part, but found " + parts.length);
  527. }
  528. try (InputStream stream = parts[0].getInputStream()) {
  529. StylesDocument sd = StylesDocument.Factory.parse(stream, DEFAULT_XML_OPTIONS);
  530. return sd.getStyles();
  531. }
  532. }
  533. /**
  534. * Get the document's embedded files.
  535. */
  536. @Override
  537. public List<PackagePart> getAllEmbeddedParts() throws OpenXML4JException {
  538. List<PackagePart> embedds = new LinkedList<>();
  539. // Get the embeddings for the workbook
  540. PackagePart part = getPackagePart();
  541. for (PackageRelationship rel : getPackagePart().getRelationshipsByType(OLE_OBJECT_REL_TYPE)) {
  542. embedds.add(part.getRelatedPart(rel));
  543. }
  544. for (PackageRelationship rel : getPackagePart().getRelationshipsByType(PACK_OBJECT_REL_TYPE)) {
  545. embedds.add(part.getRelatedPart(rel));
  546. }
  547. return embedds;
  548. }
  549. /**
  550. * Finds that for example the 2nd entry in the body list is the 1st paragraph
  551. */
  552. private int getBodyElementSpecificPos(int pos, List<? extends IBodyElement> list) {
  553. // If there's nothing to find, skip it
  554. if (list.isEmpty()) {
  555. return -1;
  556. }
  557. if (pos >= 0 && pos < bodyElements.size()) {
  558. // Ensure the type is correct
  559. IBodyElement needle = bodyElements.get(pos);
  560. if (needle.getElementType() != list.get(0).getElementType()) {
  561. // Wrong type
  562. return -1;
  563. }
  564. // Work back until we find it
  565. int startPos = Math.min(pos, list.size() - 1);
  566. for (int i = startPos; i >= 0; i--) {
  567. if (list.get(i) == needle) {
  568. return i;
  569. }
  570. }
  571. }
  572. // Couldn't be found
  573. return -1;
  574. }
  575. /**
  576. * Look up the paragraph at the specified position in the body elements list
  577. * and return this paragraphs position in the paragraphs list
  578. *
  579. * @param pos The position of the relevant paragraph in the body elements
  580. * list
  581. * @return the position of the paragraph in the paragraphs list, if there is
  582. * a paragraph at the position in the bodyelements list. Else it
  583. * will return -1
  584. */
  585. public int getParagraphPos(int pos) {
  586. return getBodyElementSpecificPos(pos, paragraphs);
  587. }
  588. /**
  589. * get with the position of a table in the bodyelement array list
  590. * the position of this table in the table array list
  591. *
  592. * @param pos position of the table in the bodyelement array list
  593. * @return if there is a table at the position in the bodyelement array list,
  594. * else it will return null.
  595. */
  596. public int getTablePos(int pos) {
  597. return getBodyElementSpecificPos(pos, tables);
  598. }
  599. /**
  600. * Add a new paragraph at position of the cursor. The cursor must be on the
  601. * {@link XmlCursor.TokenType#START} tag of an subelement
  602. * of the documents body. When this method is done, the cursor passed as
  603. * parameter points to the {@link XmlCursor.TokenType#END}
  604. * of the newly inserted paragraph.
  605. *
  606. * @param cursor The cursor-position where the new paragraph should be added.
  607. * @return the {@link XWPFParagraph} object representing the newly inserted
  608. * CTP object
  609. */
  610. @Override
  611. public XWPFParagraph insertNewParagraph(XmlCursor cursor) {
  612. if (isCursorInBody(cursor)) {
  613. String uri = CTP.type.getName().getNamespaceURI();
  614. /*
  615. * TODO DO not use a coded constant, find the constant in the OOXML
  616. * classes instead, as the child of type CT_Paragraph is defined in the
  617. * OOXML schema as 'p'
  618. */
  619. String localPart = "p";
  620. // creates a new Paragraph, cursor is positioned inside the new
  621. // element
  622. cursor.beginElement(localPart, uri);
  623. // move the cursor to the START token to the paragraph just created
  624. cursor.toParent();
  625. CTP p = (CTP) cursor.getObject();
  626. XWPFParagraph newP = new XWPFParagraph(p, this);
  627. XmlObject o = null;
  628. /*
  629. * move the cursor to the previous element until a) the next
  630. * paragraph is found or b) all elements have been passed
  631. */
  632. while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
  633. o = cursor.getObject();
  634. }
  635. /*
  636. * if the object that has been found is a) not a paragraph or b) is
  637. * the paragraph that has just been inserted, as the cursor in the
  638. * while loop above was not moved as there were no other siblings,
  639. * then the paragraph that was just inserted is the first paragraph
  640. * in the body. Otherwise, take the previous paragraph and calculate
  641. * the new index for the new paragraph.
  642. */
  643. if ((!(o instanceof CTP)) || o == p) {
  644. paragraphs.add(0, newP);
  645. } else {
  646. int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
  647. paragraphs.add(pos, newP);
  648. }
  649. /*
  650. * create a new cursor, that points to the START token of the just
  651. * inserted paragraph
  652. */
  653. try (XmlCursor newParaPos = p.newCursor()) {
  654. /*
  655. * Calculate the paragraphs index in the list of all body
  656. * elements
  657. */
  658. int i = 0;
  659. cursor.toCursor(newParaPos);
  660. while (cursor.toPrevSibling()) {
  661. o = cursor.getObject();
  662. if (o instanceof CTP || o instanceof CTTbl) {
  663. i++;
  664. }
  665. }
  666. bodyElements.add(i, newP);
  667. cursor.toCursor(newParaPos);
  668. cursor.toEndToken();
  669. return newP;
  670. }
  671. }
  672. return null;
  673. }
  674. @Override
  675. public XWPFTable insertNewTbl(XmlCursor cursor) {
  676. if (isCursorInBody(cursor)) {
  677. String uri = CTTbl.type.getName().getNamespaceURI();
  678. String localPart = "tbl";
  679. cursor.beginElement(localPart, uri);
  680. cursor.toParent();
  681. CTTbl t = (CTTbl) cursor.getObject();
  682. XWPFTable newT = new XWPFTable(t, this);
  683. XmlObject o = null;
  684. while (!(o instanceof CTTbl) && (cursor.toPrevSibling())) {
  685. o = cursor.getObject();
  686. }
  687. if (!(o instanceof CTTbl)) {
  688. tables.add(0, newT);
  689. } else {
  690. int pos = tables.indexOf(getTable((CTTbl) o)) + 1;
  691. tables.add(pos, newT);
  692. }
  693. int i = 0;
  694. try (XmlCursor tableCursor = t.newCursor()) {
  695. cursor.toCursor(tableCursor);
  696. while (cursor.toPrevSibling()) {
  697. o = cursor.getObject();
  698. if (o instanceof CTP || o instanceof CTTbl) {
  699. i++;
  700. }
  701. }
  702. bodyElements.add(i, newT);
  703. cursor.toCursor(tableCursor);
  704. cursor.toEndToken();
  705. return newT;
  706. }
  707. }
  708. return null;
  709. }
  710. /**
  711. * verifies that cursor is on the right position
  712. */
  713. private boolean isCursorInBody(XmlCursor cursor) {
  714. try (XmlCursor verify = cursor.newCursor()) {
  715. verify.toParent();
  716. return (verify.getObject() == this.ctDocument.getBody());
  717. }
  718. }
  719. private int getPosOfBodyElement(IBodyElement needle) {
  720. BodyElementType type = needle.getElementType();
  721. IBodyElement current;
  722. for (int i = 0; i < bodyElements.size(); i++) {
  723. current = bodyElements.get(i);
  724. if (current.getElementType() == type) {
  725. if (current.equals(needle)) {
  726. return i;
  727. }
  728. }
  729. }
  730. return -1;
  731. }
  732. /**
  733. * Get the position of the paragraph, within the list
  734. * of all the body elements.
  735. *
  736. * @param p The paragraph to find
  737. * @return The location, or -1 if the paragraph couldn't be found
  738. */
  739. public int getPosOfParagraph(XWPFParagraph p) {
  740. return getPosOfBodyElement(p);
  741. }
  742. /**
  743. * Get the position of the table, within the list of
  744. * all the body elements.
  745. *
  746. * @param t The table to find
  747. * @return The location, or -1 if the table couldn't be found
  748. */
  749. public int getPosOfTable(XWPFTable t) {
  750. return getPosOfBodyElement(t);
  751. }
  752. /**
  753. * commit and saves the document
  754. */
  755. @Override
  756. protected void commit() throws IOException {
  757. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  758. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTDocument1.type.getName().getNamespaceURI(), "document"));
  759. PackagePart part = getPackagePart();
  760. try (OutputStream out = part.getOutputStream()) {
  761. ctDocument.save(out, xmlOptions);
  762. }
  763. }
  764. /**
  765. * Gets the index of the relation we're trying to create
  766. */
  767. private int getRelationIndex(XWPFRelation relation) {
  768. int i = 1;
  769. for (RelationPart rp : getRelationParts()) {
  770. if (rp.getRelationship().getRelationshipType().equals(relation.getRelation())) {
  771. i++;
  772. }
  773. }
  774. return i;
  775. }
  776. /**
  777. * Appends a new paragraph to this document
  778. *
  779. * @return a new paragraph
  780. */
  781. public XWPFParagraph createParagraph() {
  782. XWPFParagraph p = new XWPFParagraph(ctDocument.getBody().addNewP(), this);
  783. bodyElements.add(p);
  784. paragraphs.add(p);
  785. return p;
  786. }
  787. /**
  788. * Creates an empty comments for the document if one does not already exist
  789. *
  790. * @return comments
  791. */
  792. public XWPFComments createComments() {
  793. if (comments == null) {
  794. CommentsDocument commentsDoc = CommentsDocument.Factory.newInstance();
  795. XWPFRelation relation = XWPFRelation.COMMENT;
  796. int i = getRelationIndex(relation);
  797. XWPFComments wrapper = (XWPFComments) createRelationship(relation, XWPFFactory.getInstance(), i);
  798. wrapper.setCtComments(commentsDoc.addNewComments());
  799. wrapper.setXWPFDocument(getXWPFDocument());
  800. comments = wrapper;
  801. }
  802. return comments;
  803. }
  804. /**
  805. * Creates an empty numbering if one does not already exist and sets the numbering member
  806. *
  807. * @return numbering
  808. */
  809. public XWPFNumbering createNumbering() {
  810. if (numbering == null) {
  811. NumberingDocument numberingDoc = NumberingDocument.Factory.newInstance();
  812. XWPFRelation relation = XWPFRelation.NUMBERING;
  813. int i = getRelationIndex(relation);
  814. XWPFNumbering wrapper = (XWPFNumbering) createRelationship(relation, XWPFFactory.getInstance(), i);
  815. wrapper.setNumbering(numberingDoc.addNewNumbering());
  816. numbering = wrapper;
  817. }
  818. return numbering;
  819. }
  820. /**
  821. * Creates an empty styles for the document if one does not already exist
  822. *
  823. * @return styles
  824. */
  825. public XWPFStyles createStyles() {
  826. if (styles == null) {
  827. StylesDocument stylesDoc = StylesDocument.Factory.newInstance();
  828. XWPFRelation relation = XWPFRelation.STYLES;
  829. int i = getRelationIndex(relation);
  830. XWPFStyles wrapper = (XWPFStyles) createRelationship(relation, XWPFFactory.getInstance(), i);
  831. wrapper.setStyles(stylesDoc.addNewStyles());
  832. styles = wrapper;
  833. }
  834. return styles;
  835. }
  836. /**
  837. * Creates an empty footnotes element for the document if one does not already exist
  838. *
  839. * @return footnotes
  840. */
  841. public XWPFFootnotes createFootnotes() {
  842. if (footnotes == null) {
  843. FootnotesDocument footnotesDoc = FootnotesDocument.Factory.newInstance();
  844. XWPFRelation relation = XWPFRelation.FOOTNOTE;
  845. int i = getRelationIndex(relation);
  846. XWPFFootnotes wrapper = (XWPFFootnotes) createRelationship(relation, XWPFFactory.getInstance(), i);
  847. wrapper.setFootnotes(footnotesDoc.addNewFootnotes());
  848. wrapper.setIdManager(this.footnoteIdManager);
  849. footnotes = wrapper;
  850. }
  851. return footnotes;
  852. }
  853. /**
  854. * Add a CTFtnEdn footnote to the document.
  855. *
  856. * @param note CTFtnEnd to be added.
  857. * @return New {@link XWPFFootnote}
  858. */
  859. @Internal
  860. public XWPFFootnote addFootnote(CTFtnEdn note) {
  861. return footnotes.addFootnote(note);
  862. }
  863. /**
  864. * Add a CTFtnEdn endnote to the document.
  865. *
  866. * @param note CTFtnEnd to be added.
  867. * @return New {@link XWPFEndnote}
  868. */
  869. @Internal
  870. public XWPFEndnote addEndnote(CTFtnEdn note) {
  871. XWPFEndnote endnote = new XWPFEndnote(this, note);
  872. endnotes.addEndnote(note);
  873. return endnote;
  874. }
  875. /**
  876. * remove a BodyElement from bodyElements array list
  877. *
  878. * @return true if removing was successfully, else return false
  879. */
  880. public boolean removeBodyElement(int pos) {
  881. if (pos >= 0 && pos < bodyElements.size()) {
  882. BodyElementType type = bodyElements.get(pos).getElementType();
  883. if (type == BodyElementType.TABLE) {
  884. int tablePos = getTablePos(pos);
  885. tables.remove(tablePos);
  886. ctDocument.getBody().removeTbl(tablePos);
  887. }
  888. if (type == BodyElementType.PARAGRAPH) {
  889. int paraPos = getParagraphPos(pos);
  890. paragraphs.remove(paraPos);
  891. ctDocument.getBody().removeP(paraPos);
  892. }
  893. bodyElements.remove(pos);
  894. return true;
  895. }
  896. return false;
  897. }
  898. /**
  899. * copies content of a paragraph to a existing paragraph in the list paragraphs at position pos
  900. */
  901. public void setParagraph(XWPFParagraph paragraph, int pos) {
  902. paragraphs.set(pos, paragraph);
  903. ctDocument.getBody().setPArray(pos, paragraph.getCTP());
  904. /* TODO update body element, update xwpf element, verify that
  905. * incoming paragraph belongs to this document or if not, XML was
  906. * copied properly (namespace-abbreviations, etc.)
  907. */
  908. }
  909. /**
  910. * @return the LastParagraph of the document
  911. */
  912. public XWPFParagraph getLastParagraph() {
  913. int lastPos = paragraphs.toArray().length - 1;
  914. return paragraphs.get(lastPos);
  915. }
  916. /**
  917. * Create an empty table with one row and one column as default.
  918. *
  919. * @return a new table
  920. */
  921. public XWPFTable createTable() {
  922. XWPFTable table = new XWPFTable(ctDocument.getBody().addNewTbl(), this);
  923. bodyElements.add(table);
  924. tables.add(table);
  925. return table;
  926. }
  927. /**
  928. * Create an empty table with a number of rows and cols specified
  929. */
  930. public XWPFTable createTable(int rows, int cols) {
  931. XWPFTable table = new XWPFTable(ctDocument.getBody().addNewTbl(), this, rows, cols);
  932. bodyElements.add(table);
  933. tables.add(table);
  934. return table;
  935. }
  936. /**
  937. *
  938. */
  939. public void createTOC() {
  940. CTSdtBlock block = this.getDocument().getBody().addNewSdt();
  941. TOC toc = new TOC(block);
  942. for (XWPFParagraph par : paragraphs) {
  943. String parStyle = par.getStyle();
  944. if (parStyle != null && parStyle.startsWith("Heading")) {
  945. try {
  946. int level = Integer.parseInt(parStyle.substring("Heading".length()));
  947. toc.addRow(level, par.getText(), 1, "112723803");
  948. } catch (NumberFormatException e) {
  949. LOG.atError().withThrowable(e).log("can't format number in TOC heading");
  950. }
  951. }
  952. }
  953. }
  954. /**
  955. * Replace content of table in array tables at position pos with a
  956. */
  957. public void setTable(int pos, XWPFTable table) {
  958. tables.set(pos, table);
  959. ctDocument.getBody().setTblArray(pos, table.getCTTbl());
  960. }
  961. /**
  962. * Verifies that the documentProtection tag in settings.xml file <br>
  963. * specifies that the protection is enforced (w:enforcement="1") <br>
  964. * <br>
  965. * sample snippet from settings.xml
  966. * <pre>
  967. * &lt;w:settings ... &gt;
  968. * &lt;w:documentProtection w:edit=&quot;readOnly&quot; w:enforcement=&quot;1&quot;/&gt;
  969. * </pre>
  970. *
  971. * @return true if documentProtection is enforced with option any
  972. */
  973. public boolean isEnforcedProtection() {
  974. return settings.isEnforcedWith();
  975. }
  976. /**
  977. * Verifies that the documentProtection tag in settings.xml file <br>
  978. * specifies that the protection is enforced (w:enforcement="1") <br>
  979. * and that the kind of protection is readOnly (w:edit="readOnly")<br>
  980. * <br>
  981. * sample snippet from settings.xml
  982. * <pre>
  983. * &lt;w:settings ... &gt;
  984. * &lt;w:documentProtection w:edit=&quot;readOnly&quot; w:enforcement=&quot;1&quot;/&gt;
  985. * </pre>
  986. *
  987. * @return true if documentProtection is enforced with option readOnly
  988. */
  989. public boolean isEnforcedReadonlyProtection() {
  990. return settings.isEnforcedWith(STDocProtect.READ_ONLY);
  991. }
  992. /**
  993. * Verifies that the documentProtection tag in settings.xml file <br>
  994. * specifies that the protection is enforced (w:enforcement="1") <br>
  995. * and that the kind of protection is forms (w:edit="forms")<br>
  996. * <br>
  997. * sample snippet from settings.xml
  998. * <pre>
  999. * &lt;w:settings ... &gt;
  1000. * &lt;w:documentProtection w:edit=&quot;forms&quot; w:enforcement=&quot;1&quot;/&gt;
  1001. * </pre>
  1002. *
  1003. * @return true if documentProtection is enforced with option forms
  1004. */
  1005. public boolean isEnforcedFillingFormsProtection() {
  1006. return settings.isEnforcedWith(STDocProtect.FORMS);
  1007. }
  1008. /**
  1009. * Verifies that the documentProtection tag in settings.xml file <br>
  1010. * specifies that the protection is enforced (w:enforcement="1") <br>
  1011. * and that the kind of protection is comments (w:edit="comments")<br>
  1012. * <br>
  1013. * sample snippet from settings.xml
  1014. * <pre>
  1015. * &lt;w:settings ... &gt;
  1016. * &lt;w:documentProtection w:edit=&quot;comments&quot; w:enforcement=&quot;1&quot;/&gt;
  1017. * </pre>
  1018. *
  1019. * @return true if documentProtection is enforced with option comments
  1020. */
  1021. public boolean isEnforcedCommentsProtection() {
  1022. return settings.isEnforcedWith(STDocProtect.COMMENTS);
  1023. }
  1024. /**
  1025. * Verifies that the documentProtection tag in settings.xml file <br>
  1026. * specifies that the protection is enforced (w:enforcement="1") <br>
  1027. * and that the kind of protection is trackedChanges (w:edit="trackedChanges")<br>
  1028. * <br>
  1029. * sample snippet from settings.xml
  1030. * <pre>
  1031. * &lt;w:settings ... &gt;
  1032. * &lt;w:documentProtection w:edit=&quot;trackedChanges&quot; w:enforcement=&quot;1&quot;/&gt;
  1033. * </pre>
  1034. *
  1035. * @return true if documentProtection is enforced with option trackedChanges
  1036. */
  1037. public boolean isEnforcedTrackedChangesProtection() {
  1038. return settings.isEnforcedWith(STDocProtect.TRACKED_CHANGES);
  1039. }
  1040. public boolean isEnforcedUpdateFields() {
  1041. return settings.isUpdateFields();
  1042. }
  1043. /**
  1044. * Enforces the readOnly protection.<br>
  1045. * In the documentProtection tag inside settings.xml file, <br>
  1046. * it sets the value of enforcement to "1" (w:enforcement="1") <br>
  1047. * and the value of edit to readOnly (w:edit="readOnly")<br>
  1048. * <br>
  1049. * sample snippet from settings.xml
  1050. * <pre>
  1051. * &lt;w:settings ... &gt;
  1052. * &lt;w:documentProtection w:edit=&quot;readOnly&quot; w:enforcement=&quot;1&quot;/&gt;
  1053. * </pre>
  1054. */
  1055. public void enforceReadonlyProtection() {
  1056. settings.setEnforcementEditValue(STDocProtect.READ_ONLY);
  1057. }
  1058. /**
  1059. * Enforces the readOnly protection with a password.<br>
  1060. * <br>
  1061. * sample snippet from settings.xml
  1062. * <pre>
  1063. * &lt;w:documentProtection w:edit=&quot;readOnly&quot; w:enforcement=&quot;1&quot;
  1064. * w:cryptProviderType=&quot;rsaAES&quot; w:cryptAlgorithmClass=&quot;hash&quot;
  1065. * w:cryptAlgorithmType=&quot;typeAny&quot; w:cryptAlgorithmSid=&quot;14&quot;
  1066. * w:cryptSpinCount=&quot;100000&quot; w:hash=&quot;...&quot; w:salt=&quot;....&quot;
  1067. * /&gt;
  1068. * </pre>
  1069. *
  1070. * @param password the plaintext password, if null no password will be applied
  1071. * @param hashAlgo the hash algorithm - only md2, m5, sha1, sha256, sha384 and sha512 are supported.
  1072. * if null, it will default default to sha1
  1073. */
  1074. public void enforceReadonlyProtection(String password, HashAlgorithm hashAlgo) {
  1075. settings.setEnforcementEditValue(STDocProtect.READ_ONLY, password, hashAlgo);
  1076. }
  1077. /**
  1078. * Enforce the Filling Forms protection.<br>
  1079. * In the documentProtection tag inside settings.xml file, <br>
  1080. * it sets the value of enforcement to "1" (w:enforcement="1") <br>
  1081. * and the value of edit to forms (w:edit="forms")<br>
  1082. * <br>
  1083. * sample snippet from settings.xml
  1084. * <pre>
  1085. * &lt;w:settings ... &gt;
  1086. * &lt;w:documentProtection w:edit=&quot;forms&quot; w:enforcement=&quot;1&quot;/&gt;
  1087. * </pre>
  1088. */
  1089. public void enforceFillingFormsProtection() {
  1090. settings.setEnforcementEditValue(STDocProtect.FORMS);
  1091. }
  1092. /**
  1093. * Enforce the Filling Forms protection.<br>
  1094. * <br>
  1095. * sample snippet from settings.xml
  1096. * <pre>
  1097. * &lt;w:documentProtection w:edit=&quot;forms&quot; w:enforcement=&quot;1&quot;
  1098. * w:cryptProviderType=&quot;rsaAES&quot; w:cryptAlgorithmClass=&quot;hash&quot;
  1099. * w:cryptAlgorithmType=&quot;typeAny&quot; w:cryptAlgorithmSid=&quot;14&quot;
  1100. * w:cryptSpinCount=&quot;100000&quot; w:hash=&quot;...&quot; w:salt=&quot;....&quot;
  1101. * /&gt;
  1102. * </pre>
  1103. *
  1104. * @param password the plaintext password, if null no password will be applied
  1105. * @param hashAlgo the hash algorithm - only md2, m5, sha1, sha256, sha384 and sha512 are supported.
  1106. * if null, it will default default to sha1
  1107. */
  1108. public void enforceFillingFormsProtection(String password, HashAlgorithm hashAlgo) {
  1109. settings.setEnforcementEditValue(STDocProtect.FORMS, password, hashAlgo);
  1110. }
  1111. /**
  1112. * Enforce the Comments protection.<br>
  1113. * In the documentProtection tag inside settings.xml file,<br>
  1114. * it sets the value of enforcement to "1" (w:enforcement="1") <br>
  1115. * and the value of edit to comments (w:edit="comments")<br>
  1116. * <br>
  1117. * sample snippet from settings.xml
  1118. * <pre>
  1119. * &lt;w:settings ... &gt;
  1120. * &lt;w:documentProtection w:edit=&quot;comments&quot; w:enforcement=&quot;1&quot;/&gt;
  1121. * </pre>
  1122. */
  1123. public void enforceCommentsProtection() {
  1124. settings.setEnforcementEditValue(STDocProtect.COMMENTS);
  1125. }
  1126. /**
  1127. * Enforce the Comments protection.<br>
  1128. * <br>
  1129. * sample snippet from settings.xml
  1130. * <pre>
  1131. * &lt;w:documentProtection w:edit=&quot;comments&quot; w:enforcement=&quot;1&quot;
  1132. * w:cryptProviderType=&quot;rsaAES&quot; w:cryptAlgorithmClass=&quot;hash&quot;
  1133. * w:cryptAlgorithmType=&quot;typeAny&quot; w:cryptAlgorithmSid=&quot;14&quot;
  1134. * w:cryptSpinCount=&quot;100000&quot; w:hash=&quot;...&quot; w:salt=&quot;....&quot;
  1135. * /&gt;
  1136. * </pre>
  1137. *
  1138. * @param password the plaintext password, if null no password will be applied
  1139. * @param hashAlgo the hash algorithm - only md2, m5, sha1, sha256, sha384 and sha512 are supported.
  1140. * if null, it will default default to sha1
  1141. */
  1142. public void enforceCommentsProtection(String password, HashAlgorithm hashAlgo) {
  1143. settings.setEnforcementEditValue(STDocProtect.COMMENTS, password, hashAlgo);
  1144. }
  1145. /**
  1146. * Enforce the Tracked Changes protection.<br>
  1147. * In the documentProtection tag inside settings.xml file, <br>
  1148. * it sets the value of enforcement to "1" (w:enforcement="1") <br>
  1149. * and the value of edit to trackedChanges (w:edit="trackedChanges")<br>
  1150. * <br>
  1151. * sample snippet from settings.xml
  1152. * <pre>
  1153. * &lt;w:settings ... &gt;
  1154. * &lt;w:documentProtection w:edit=&quot;trackedChanges&quot; w:enforcement=&quot;1&quot;/&gt;
  1155. * </pre>
  1156. */
  1157. public void enforceTrackedChangesProtection() {
  1158. settings.setEnforcementEditValue(STDocProtect.TRACKED_CHANGES);
  1159. }
  1160. /**
  1161. * Enforce the Tracked Changes protection.<br>
  1162. * <br>
  1163. * sample snippet from settings.xml
  1164. * <pre>
  1165. * &lt;w:documentProtection w:edit=&quot;trackedChanges&quot; w:enforcement=&quot;1&quot;
  1166. * w:cryptProviderType=&quot;rsaAES&quot; w:cryptAlgorithmClass=&quot;hash&quot;
  1167. * w:cryptAlgorithmType=&quot;typeAny&quot; w:cryptAlgorithmSid=&quot;14&quot;
  1168. * w:cryptSpinCount=&quot;100000&quot; w:hash=&quot;...&quot; w:salt=&quot;....&quot;
  1169. * /&gt;
  1170. * </pre>
  1171. *
  1172. * @param password the plaintext password, if null no password will be applied
  1173. * @param hashAlgo the hash algorithm - only md2, m5, sha1, sha256, sha384 and sha512 are supported.
  1174. * if null, it will default default to sha1
  1175. */
  1176. public void enforceTrackedChangesProtection(String password, HashAlgorithm hashAlgo) {
  1177. settings.setEnforcementEditValue(STDocProtect.TRACKED_CHANGES, password, hashAlgo);
  1178. }
  1179. /**
  1180. * Validates the existing password
  1181. *
  1182. * @return true, only if password was set and equals, false otherwise
  1183. */
  1184. public boolean validateProtectionPassword(String password) {
  1185. return settings.validateProtectionPassword(password);
  1186. }
  1187. /**
  1188. * Remove protection enforcement.<br>
  1189. * In the documentProtection tag inside settings.xml file <br>
  1190. * it sets the value of enforcement to "0" (w:enforcement="0") <br>
  1191. */
  1192. public void removeProtectionEnforcement() {
  1193. settings.removeEnforcement();
  1194. }
  1195. /**
  1196. * Enforces fields update on document open (in Word).
  1197. * In the settings.xml file <br>
  1198. * sets the updateSettings value to true (w:updateSettings w:val="true")
  1199. * <p>
  1200. * NOTICES:
  1201. * <ul>
  1202. * <li>Causing Word to ask on open: "This document contains fields that may refer to other files. Do you want to update the fields in this document?"
  1203. * (if "Update automatic links at open" is enabled)</li>
  1204. * <li>Flag is removed after saving with changes in Word </li>
  1205. * </ul>
  1206. */
  1207. public void enforceUpdateFields() {
  1208. settings.setUpdateFields();
  1209. }
  1210. /**
  1211. * Check if revision tracking is turned on.
  1212. *
  1213. * @return {@code true} if revision tracking is turned on
  1214. */
  1215. public boolean isTrackRevisions() {
  1216. return settings.isTrackRevisions();
  1217. }
  1218. /**
  1219. * Enable or disable revision tracking.
  1220. *
  1221. * @param enable {@code true} to turn on revision tracking, {@code false} to turn off revision tracking
  1222. */
  1223. public void setTrackRevisions(boolean enable) {
  1224. settings.setTrackRevisions(enable);
  1225. }
  1226. /**
  1227. * Returns the current zoom factor in percent values, i.e. 100 is normal zoom.
  1228. *
  1229. * @return A percent value denoting the current zoom setting of this document.
  1230. */
  1231. public long getZoomPercent() {
  1232. return settings.getZoomPercent();
  1233. }
  1234. /**
  1235. * Set the zoom setting as percent value, i.e. 100 is normal zoom.
  1236. *
  1237. * @param zoomPercent A percent value denoting the zoom setting for this document.
  1238. */
  1239. public void setZoomPercent(long zoomPercent) {
  1240. settings.setZoomPercent(zoomPercent);
  1241. }
  1242. /**
  1243. * Returns the even-and-odd-headings setting
  1244. *
  1245. * @return True or false indicating whether or not separate even and odd headings is turned on.
  1246. */
  1247. public boolean getEvenAndOddHeadings() {
  1248. return settings.getEvenAndOddHeadings();
  1249. }
  1250. /**
  1251. * Sets the even-and-odd-headings setting
  1252. * @param enable Set to true to turn on separate even and odd headings.
  1253. */
  1254. public void setEvenAndOddHeadings(boolean enable) {
  1255. settings.setEvenAndOddHeadings(enable);
  1256. }
  1257. /**
  1258. * Returns the mirror margins setting
  1259. *
  1260. * @return True or false indicating whether or not mirror margins is turned on.
  1261. */
  1262. public boolean getMirrorMargins() {
  1263. return settings.getMirrorMargins();
  1264. }
  1265. /**
  1266. * Sets the mirror margins setting
  1267. * @param enable Set to true to turn on mirror margins.
  1268. */
  1269. public void setMirrorMargins(boolean enable) {
  1270. settings.setMirrorMargins(enable);
  1271. }
  1272. /**
  1273. * inserts an existing XWPFTable to the arrays bodyElements and tables
  1274. */
  1275. @Override
  1276. public void insertTable(int pos, XWPFTable table) {
  1277. bodyElements.add(pos, table);
  1278. int i = 0;
  1279. for (CTTbl tbl : ctDocument.getBody().getTblArray()) {
  1280. if (tbl == table.getCTTbl()) {
  1281. break;
  1282. }
  1283. i++;
  1284. }
  1285. tables.add(i, table);
  1286. }
  1287. /**
  1288. * Returns all Pictures, which are referenced from the document itself.
  1289. *
  1290. * @return a {@link List} of {@link XWPFPictureData}. The returned {@link List} is unmodifiable. Use #a
  1291. */
  1292. public List<XWPFPictureData> getAllPictures() {
  1293. return Collections.unmodifiableList(pictures);
  1294. }
  1295. /**
  1296. * @return all Pictures in this package
  1297. */
  1298. public List<XWPFPictureData> getAllPackagePictures() {
  1299. List<XWPFPictureData> result = new ArrayList<>();
  1300. Collection<List<XWPFPictureData>> values = packagePictures.values();
  1301. for (List<XWPFPictureData> list : values) {
  1302. result.addAll(list);
  1303. }
  1304. return Collections.unmodifiableList(result);
  1305. }
  1306. /**
  1307. * @return document level settings
  1308. * @since POI 5.2.1
  1309. */
  1310. public XWPFSettings getSettings() {
  1311. return settings;
  1312. }
  1313. void registerPackagePictureData(XWPFPictureData picData) {
  1314. List<XWPFPictureData> list = packagePictures.computeIfAbsent(picData.getChecksum(), k -> new ArrayList<>(1));
  1315. if (!list.contains(picData)) {
  1316. list.add(picData);
  1317. }
  1318. }
  1319. XWPFPictureData findPackagePictureData(byte[] pictureData) {
  1320. long checksum = IOUtils.calculateChecksum(pictureData);
  1321. XWPFPictureData xwpfPicData = null;
  1322. /*
  1323. * Try to find PictureData with this checksum. Create new, if none
  1324. * exists.
  1325. */
  1326. List<XWPFPictureData> xwpfPicDataList = packagePictures.get(checksum);
  1327. if (xwpfPicDataList != null) {
  1328. Iterator<XWPFPictureData> iter = xwpfPicDataList.iterator();
  1329. while (iter.hasNext() && xwpfPicData == null) {
  1330. XWPFPictureData curElem = iter.next();
  1331. if (Arrays.equals(pictureData, curElem.getData())) {
  1332. xwpfPicData = curElem;
  1333. }
  1334. }
  1335. }
  1336. return xwpfPicData;
  1337. }
  1338. /**
  1339. * Adds a picture to the document.
  1340. *
  1341. * @param pictureData The picture data
  1342. * @param format the format of the picture, see constants in {@link Document}
  1343. * @return the index to this picture (0 based), the added picture can be
  1344. * obtained from {@link #getAllPictures()} .
  1345. * @throws InvalidFormatException if the format is not known
  1346. * @see #addPictureData(byte[], PictureType)
  1347. */
  1348. public String addPictureData(byte[] pictureData, int format) throws InvalidFormatException {
  1349. return addPictureData(pictureData, PictureType.findByOoxmlId(format));
  1350. }
  1351. /**
  1352. * Adds a picture to the document.
  1353. *
  1354. * @param pictureData The picture data
  1355. * @param pictureType the {@link PictureType}
  1356. * @return the index to this picture (0 based), the added picture can be
  1357. * obtained from {@link #getAllPictures()} .
  1358. * @throws InvalidFormatException if the format is not known
  1359. * @since POI 5.2.3
  1360. */
  1361. public String addPictureData(byte[] pictureData, PictureType pictureType) throws InvalidFormatException {
  1362. if (pictureType == null) {
  1363. throw new InvalidFormatException("pictureType is not supported");
  1364. }
  1365. XWPFPictureData xwpfPicData = findPackagePictureData(pictureData);
  1366. POIXMLRelation relDesc = XWPFPictureData.RELATIONS[pictureType.ooxmlId];
  1367. if (xwpfPicData == null) {
  1368. /* Part doesn't exist, create a new one */
  1369. int idx = getNextPicNameNumber(pictureType);
  1370. xwpfPicData = (XWPFPictureData) createRelationship(relDesc, XWPFFactory.getInstance(), idx);
  1371. /* write bytes to new part */
  1372. PackagePart picDataPart = xwpfPicData.getPackagePart();
  1373. try (OutputStream out = picDataPart.getOutputStream()) {
  1374. out.write(pictureData);
  1375. } catch (IOException e) {
  1376. throw new POIXMLException(e);
  1377. }
  1378. registerPackagePictureData(xwpfPicData);
  1379. pictures.add(xwpfPicData);
  1380. return getRelationId(xwpfPicData);
  1381. } else if (!getRelations().contains(xwpfPicData)) {
  1382. /*
  1383. * Part already existed, but was not related so far. Create
  1384. * relationship to the already existing part and update
  1385. * POIXMLDocumentPart data.
  1386. */
  1387. // TODO add support for TargetMode.EXTERNAL relations.
  1388. RelationPart rp = addRelation(null, XWPFRelation.IMAGES, xwpfPicData);
  1389. return rp.getRelationship().getId();
  1390. } else {
  1391. /* Part already existed, get relation id and return it */
  1392. return getRelationId(xwpfPicData);
  1393. }
  1394. }
  1395. /**
  1396. * Adds a picture to the document.
  1397. *
  1398. * @param is The picture data
  1399. * @param format the format of the picture, see constants in {@link Document}
  1400. * @return the index to this picture (0 based), the added picture can be
  1401. * obtained from {@link #getAllPictures()} .
  1402. * @throws InvalidFormatException if the format is not known
  1403. * @see #addPictureData(InputStream, PictureType)
  1404. */
  1405. public String addPictureData(InputStream is, int format) throws InvalidFormatException {
  1406. try {
  1407. byte[] data = IOUtils.toByteArrayWithMaxLength(is, XWPFPictureData.getMaxImageSize());
  1408. return addPictureData(data, format);
  1409. } catch (IOException e) {
  1410. throw new POIXMLException(e);
  1411. }
  1412. }
  1413. /**
  1414. * Adds a picture to the document.
  1415. *
  1416. * @param is The picture data
  1417. * @param pictureType the {@link PictureType}
  1418. * @return the index to this picture (0 based), the added picture can be
  1419. * obtained from {@link #getAllPictures()} .
  1420. * @throws InvalidFormatException if the pictureType is not known
  1421. * @since POI 5.2.3
  1422. */
  1423. public String addPictureData(InputStream is, PictureType pictureType) throws InvalidFormatException {
  1424. try {
  1425. byte[] data = IOUtils.toByteArrayWithMaxLength(is, XWPFPictureData.getMaxImageSize());
  1426. return addPictureData(data, pictureType);
  1427. } catch (IOException e) {
  1428. throw new POIXMLException(e);
  1429. }
  1430. }
  1431. /**
  1432. * get the next free ImageNumber
  1433. *
  1434. * @param format the format of the picture, see constants in {@link Document}
  1435. * @return the next free ImageNumber
  1436. * @throws InvalidFormatException If the format of the picture is not known.
  1437. * @see #getNextPicNameNumber(PictureType)
  1438. */
  1439. public int getNextPicNameNumber(int format) throws InvalidFormatException {
  1440. return getNextPicNameNumber(PictureType.findByOoxmlId(format));
  1441. }
  1442. /**
  1443. * get the next free ImageNumber
  1444. *
  1445. * @param pictureType the {@link PictureType}
  1446. * @return the next free ImageNumber
  1447. * @throws InvalidFormatException If the pictureType of the picture is not known.
  1448. * @since POI 5.2.3
  1449. */
  1450. public int getNextPicNameNumber(PictureType pictureType) throws InvalidFormatException {
  1451. if (pictureType == null) {
  1452. throw new InvalidFormatException("pictureType is not supported");
  1453. }
  1454. int img = getAllPackagePictures().size() + 1;
  1455. String proposal = XWPFPictureData.RELATIONS[pictureType.ooxmlId].getFileName(img);
  1456. PackagePartName createPartName = PackagingURIHelper.createPartName(proposal);
  1457. while (this.getPackage().getPart(createPartName) != null) {
  1458. img++;
  1459. proposal = XWPFPictureData.RELATIONS[pictureType.ooxmlId].getFileName(img);
  1460. createPartName = PackagingURIHelper.createPartName(proposal);
  1461. }
  1462. return img;
  1463. }
  1464. /**
  1465. * returns the PictureData by blipID
  1466. *
  1467. * @return XWPFPictureData of a specificID
  1468. */
  1469. public XWPFPictureData getPictureDataByID(String blipID) {
  1470. POIXMLDocumentPart relatedPart = getRelationById(blipID);
  1471. if (relatedPart instanceof XWPFPictureData) {
  1472. return (XWPFPictureData) relatedPart;
  1473. }
  1474. return null;
  1475. }
  1476. /**
  1477. * getNumbering
  1478. *
  1479. * @return numbering
  1480. */
  1481. public XWPFNumbering getNumbering() {
  1482. return numbering;
  1483. }
  1484. /**
  1485. * get Styles
  1486. *
  1487. * @return styles for this document
  1488. */
  1489. public XWPFStyles getStyles() {
  1490. return styles;
  1491. }
  1492. @Override
  1493. public XWPFParagraph getParagraph(CTP p) {
  1494. for (XWPFParagraph paragraph : paragraphs) {
  1495. if (paragraph.getCTP() == p) {
  1496. return paragraph;
  1497. }
  1498. }
  1499. return null;
  1500. }
  1501. /**
  1502. * get a table by its CTTbl-Object
  1503. *
  1504. * @return a table by its CTTbl-Object or null
  1505. */
  1506. @Override
  1507. public XWPFTable getTable(CTTbl ctTbl) {
  1508. for (int i = 0; i < tables.size(); i++) {
  1509. if (getTables().get(i).getCTTbl() == ctTbl) {
  1510. return getTables().get(i);
  1511. }
  1512. }
  1513. return null;
  1514. }
  1515. public Iterator<XWPFTable> getTablesIterator() {
  1516. return tables.iterator();
  1517. }
  1518. /**
  1519. * @since POI 5.2.0
  1520. */
  1521. public Spliterator<XWPFTable> getTablesSpliterator() {
  1522. return tables.spliterator();
  1523. }
  1524. public Iterator<XWPFParagraph> getParagraphsIterator() {
  1525. return paragraphs.iterator();
  1526. }
  1527. /**
  1528. * @since POI 5.2.0
  1529. */
  1530. public Spliterator<XWPFParagraph> getParagraphsSpliterator() {
  1531. return paragraphs.spliterator();
  1532. }
  1533. /**
  1534. * Returns the paragraph that of position pos
  1535. */
  1536. @Override
  1537. public XWPFParagraph getParagraphArray(int pos) {
  1538. if (pos >= 0 && pos < paragraphs.size()) {
  1539. return paragraphs.get(pos);
  1540. }
  1541. return null;
  1542. }
  1543. /**
  1544. * returns the Part, to which the body belongs, which you need for adding relationship to other parts
  1545. * Actually it is needed of the class XWPFTableCell. Because you have to know to which part the tableCell
  1546. * belongs.
  1547. */
  1548. @Override
  1549. public POIXMLDocumentPart getPart() {
  1550. return this;
  1551. }
  1552. /**
  1553. * get the PartType of the body, for example
  1554. * DOCUMENT, HEADER, FOOTER, FOOTNOTE,
  1555. */
  1556. @Override
  1557. public BodyType getPartType() {
  1558. return BodyType.DOCUMENT;
  1559. }
  1560. /**
  1561. * get the TableCell which belongs to the TableCell
  1562. */
  1563. @Override
  1564. public XWPFTableCell getTableCell(CTTc cell) {
  1565. XmlObject o;
  1566. CTRow row;
  1567. try (final XmlCursor cursor = cell.newCursor()) {
  1568. cursor.toParent();
  1569. o = cursor.getObject();
  1570. if (!(o instanceof CTRow)) {
  1571. return null;
  1572. }
  1573. row = (CTRow) o;
  1574. cursor.toParent();
  1575. o = cursor.getObject();
  1576. }
  1577. if (!(o instanceof CTTbl)) {
  1578. return null;
  1579. }
  1580. CTTbl tbl = (CTTbl) o;
  1581. XWPFTable table = getTable(tbl);
  1582. if (table == null) {
  1583. return null;
  1584. }
  1585. XWPFTableRow tableRow = table.getRow(row);
  1586. if (tableRow == null) {
  1587. return null;
  1588. }
  1589. return tableRow.getTableCell(cell);
  1590. }
  1591. @Override
  1592. public XWPFDocument getXWPFDocument() {
  1593. return this;
  1594. }
  1595. /**
  1596. * This method is used to create template for chart XML
  1597. * no need to read MS-Word file and modify charts
  1598. *
  1599. * @return This method return object of XWPFChart Object with default height and width
  1600. * @since POI 4.0.0
  1601. */
  1602. public XWPFChart createChart() throws InvalidFormatException, IOException {
  1603. return createChart(XDDFChart.DEFAULT_WIDTH, XDDFChart.DEFAULT_HEIGHT);
  1604. }
  1605. /**
  1606. * This method is used to create template for chart XML
  1607. * no need to read MS-Word file and modify charts
  1608. *
  1609. * @param width width of chart in document
  1610. * @param height height of chart in document
  1611. * @return This method return object of XWPFChart
  1612. * @since POI 4.0.0
  1613. */
  1614. public XWPFChart createChart(int width, int height) throws InvalidFormatException, IOException {
  1615. return createChart(createParagraph().createRun(), width, height);
  1616. }
  1617. /**
  1618. *
  1619. * @param run in which the chart will be attached.
  1620. * @param width in EMU.
  1621. * @param height in EMU.
  1622. * @return the new chart.
  1623. * @since POI 4.1.2
  1624. */
  1625. public XWPFChart createChart(XWPFRun run, int width, int height) throws InvalidFormatException, IOException {
  1626. //get chart number
  1627. int chartNumber = getNextPartNumber(XWPFRelation.CHART, charts.size() + 1);
  1628. //create relationship in document for new chart
  1629. RelationPart rp = createRelationship(
  1630. XWPFRelation.CHART, XWPFFactory.getInstance(), chartNumber, false);
  1631. // initialize xwpfchart object
  1632. XWPFChart xwpfChart = rp.getDocumentPart();
  1633. xwpfChart.setChartIndex(chartNumber);
  1634. xwpfChart.attach(rp.getRelationship().getId(), run);
  1635. xwpfChart.setChartBoundingBox(width, height);
  1636. //add chart object to chart list
  1637. charts.add(xwpfChart);
  1638. return xwpfChart;
  1639. }
  1640. /**
  1641. * Create a new footnote and add it to the document.
  1642. *
  1643. * @return New XWPFFootnote.
  1644. * @since 4.0.0
  1645. */
  1646. public XWPFFootnote createFootnote() {
  1647. XWPFFootnotes footnotes = this.createFootnotes();
  1648. return footnotes.createFootnote();
  1649. }
  1650. /**
  1651. * Remove the specified footnote if present.
  1652. *
  1653. * @param pos Array position of the footnote to be removed.
  1654. * @return True if the footnote was removed.
  1655. * @since 4.0.0
  1656. */
  1657. public boolean removeFootnote(int pos) {
  1658. if (null != footnotes) {
  1659. return footnotes.removeFootnote(pos);
  1660. } else {
  1661. return false;
  1662. }
  1663. }
  1664. /**
  1665. * Create a new end note and add it to the document.
  1666. *
  1667. * @return New {@link XWPFEndnote}.
  1668. * @since 4.0.0
  1669. */
  1670. public XWPFEndnote createEndnote() {
  1671. XWPFEndnotes endnotes = this.createEndnotes();
  1672. return endnotes.createEndnote();
  1673. }
  1674. public XWPFEndnotes createEndnotes() {
  1675. if (endnotes == null) {
  1676. EndnotesDocument endnotesDoc = EndnotesDocument.Factory.newInstance();
  1677. XWPFRelation relation = XWPFRelation.ENDNOTE;
  1678. int i = getRelationIndex(relation);
  1679. XWPFEndnotes wrapper = (XWPFEndnotes) createRelationship(relation, XWPFFactory.getInstance(), i);
  1680. wrapper.setEndnotes(endnotesDoc.addNewEndnotes());
  1681. wrapper.setIdManager(footnoteIdManager);
  1682. endnotes = wrapper;
  1683. }
  1684. return endnotes;
  1685. }
  1686. /**
  1687. * Gets the list of end notes for the document.
  1688. *
  1689. * @return List, possibly empty, of {@link XWPFEndnote}s.
  1690. */
  1691. public List<XWPFEndnote> getEndnotes() {
  1692. if (endnotes == null) {
  1693. return Collections.emptyList();
  1694. }
  1695. return endnotes.getEndnotesList();
  1696. }
  1697. /**
  1698. * Remove the specified end note if present.
  1699. *
  1700. * @param pos Array position of the end note to be removed.
  1701. * @return True if the end note was removed.
  1702. * @since 4.0.0
  1703. */
  1704. public boolean removeEndnote(int pos) {
  1705. if (null != endnotes) {
  1706. return endnotes.removeEndnote(pos);
  1707. } else {
  1708. return false;
  1709. }
  1710. }
  1711. }