org.apache.poi.xwpf.usermodel.XWPFStyles Java Examples
The following examples show how to use
org.apache.poi.xwpf.usermodel.XWPFStyles.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TocTest.java From poi-tl with Apache License 2.0 | 7 votes |
public static void main(String[] args) throws FileNotFoundException, IOException { XWPFDocument doc = new XWPFDocument(new FileInputStream("toc.docx")); XWPFStyles styles = doc.getStyles(); System.out.println(styles); XWPFParagraph p = doc.createParagraph(); XWPFRun run = p.createRun(); run.setText("Heading 1"); p.getCTP().addNewPPr(); p.getCTP().getPPr().addNewPStyle(); p.setStyle(HEADING1); XWPFParagraph tocPara = doc.createParagraph(); CTP ctP = tocPara.getCTP(); CTSimpleField toc = ctP.addNewFldSimple(); toc.setInstr("TOC \\h"); toc.setDirty(STOnOff.TRUE); System.out.println(doc.isEnforcedUpdateFields()); // doc.enforceUpdateFields(); // doc.createTOC(); doc.write(new FileOutputStream("CreateWordBookmark.docx")); doc.close(); }
Example #2
Source File: NiceXWPFDocument.java From poi-tl with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") private Map<String, String> mergeStyles(NiceXWPFDocument docMerge) { Map<String, String> styleIdsMap = new HashMap<String, String>(); XWPFStyles styles = this.getStyles(); if (null == styles) styles = createStyles(); XWPFStyles stylesMerge = docMerge.getStyles(); if (null == stylesMerge) return styleIdsMap; try { Field listStyleField = XWPFStyles.class.getDeclaredField("listStyle"); listStyleField.setAccessible(true); List<XWPFStyle> lists = (List<XWPFStyle>) listStyleField.get(stylesMerge); for (XWPFStyle xwpfStyle : lists) { if (styles.styleExist(xwpfStyle.getStyleId())) { String id = xwpfStyle.getStyleId(); xwpfStyle.setStyleId(UUID.randomUUID().toString()); styleIdsMap.put(id, xwpfStyle.getStyleId()); } styles.addStyle(xwpfStyle); } } catch (Exception e) { // throw exception? logger.error("merge style error", e); } return styleIdsMap; }