/** * add a style to the document * * @param style * @throws IOException */ public void addStyle(XWPFStyle style) { listStyle.add(style); ctStyles.addNewStyle(); int pos = ctStyles.sizeOfStyleArray() - 1; ctStyles.setStyleArray(pos, style.getCTStyle()); }
/** * Get the style with the specified name, if any. * * @param styleName The name of the style to get, e.g., "Heading 1" * @return {@link XWPFStyle} with the specified name, or null if not found. */ public XWPFStyle getStyleWithName(String styleName) { XWPFStyle style = null; for (XWPFStyle cand : listStyle) { if (styleName.equals(cand.getName())) { style = cand; break; } } return style; } }
/** * checks whether style with styleID exist * * @param styleID styleID of the Style in the style-Document * @return true if style exist, false if style not exist */ public boolean styleExist(String styleID) { for (XWPFStyle style : listStyle) { if (style.getStyleId().equals(styleID)) return true; } return false; }
/** * get the styles which are related to parameter style * * @param style * @return all Styles of the parameterList */ private List<XWPFStyle> getUsedStyleList(XWPFStyle style, List<XWPFStyle> usedStyleList) { String basisStyleID = style.getBasisStyleID(); XWPFStyle basisStyle = getStyle(basisStyleID); if ((basisStyle != null) && (!usedStyleList.contains(basisStyle))) { usedStyleList.add(basisStyle); getUsedStyleList(basisStyle, usedStyleList); } String linkStyleID = style.getLinkStyleID(); XWPFStyle linkStyle = getStyle(linkStyleID); if ((linkStyle != null) && (!usedStyleList.contains(linkStyle))) { usedStyleList.add(linkStyle); getUsedStyleList(linkStyle, usedStyleList); } String nextStyleID = style.getNextStyleID(); XWPFStyle nextStyle = getStyle(nextStyleID); if ((nextStyle != null) && (!usedStyleList.contains(nextStyle))) { usedStyleList.add(linkStyle); getUsedStyleList(linkStyle, usedStyleList); } return usedStyleList; }
@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) { LOG.error("merge style error", e); } return styleIdsMap; }
System.out.println("============================================================="); XWPFStyle xStyle = xdoc.getStyles().getStyle(styleID); if (xStyle.getType() == STStyleType.CHARACTER) { System.out.println(xStyle.getCTStyle());
color.setVal(hexToBytes(hexColor)); rpr.setColor(color); style.getCTStyle().setRPr(rpr); style.setType(STStyleType.PARAGRAPH); styles.addStyle(style);
/** * get the style with the same name * if this style is not existing, return null */ public XWPFStyle getStyleWithSameName(XWPFStyle style) { for (XWPFStyle ownStyle : listStyle) { if (ownStyle.hasSameName(style)) { return ownStyle; } } return null; }
/** * Sets the ctStyles * * @param styles */ public void setStyles(CTStyles styles) { ctStyles = styles; // Build up all the style objects for (CTStyle style : ctStyles.getStyleArray()) { listStyle.add(new XWPFStyle(style, this)); } if (ctStyles.isSetDocDefaults()) { CTDocDefaults docDefaults = ctStyles.getDocDefaults(); if (docDefaults.isSetRPrDefault() && docDefaults.getRPrDefault().isSetRPr()) { defaultRunStyle = new XWPFDefaultRunStyle( docDefaults.getRPrDefault().getRPr()); } if (docDefaults.isSetPPrDefault() && docDefaults.getPPrDefault().isSetPPr()) { defaultParaStyle = new XWPFDefaultParagraphStyle( docDefaults.getPPrDefault().getPPr()); } } }
/** * get the styles which are related to parameter style * @param style * @return all Styles of the parameterList */ private List<XWPFStyle> getUsedStyleList(XWPFStyle style, List<XWPFStyle> usedStyleList){ String basisStyleID = style.getBasisStyleID(); XWPFStyle basisStyle = getStyle(basisStyleID); if((basisStyle!=null)&&(!usedStyleList.contains(basisStyle))){ usedStyleList.add(basisStyle); getUsedStyleList(basisStyle, usedStyleList); } String linkStyleID = style.getLinkStyleID(); XWPFStyle linkStyle = getStyle(linkStyleID); if((linkStyle!=null)&&(!usedStyleList.contains(linkStyle))){ usedStyleList.add(linkStyle); getUsedStyleList(linkStyle, usedStyleList); } String nextStyleID = style.getNextStyleID(); XWPFStyle nextStyle = getStyle(nextStyleID); if((nextStyle!=null)&&(!usedStyleList.contains(nextStyle))){ usedStyleList.add(linkStyle); getUsedStyleList(linkStyle, usedStyleList); } return usedStyleList; }
/** * get the style with the same name * if this style is not existing, return null */ public XWPFStyle getStyleWithSameName(XWPFStyle style) { for (XWPFStyle ownStyle : listStyle) { if (ownStyle.hasSameName(style)) { return ownStyle; } } return null; }
/** * Read document */ @Override protected void onDocumentRead() throws IOException{ StylesDocument stylesDoc; try { InputStream is = getPackagePart().getInputStream(); stylesDoc = StylesDocument.Factory.parse(is); ctStyles = stylesDoc.getStyles(); latentStyles = new XWPFLatentStyles(ctStyles.getLatentStyles(), this); } catch (XmlException e) { throw new POIXMLException("Unable to read styles", e); } // Build up all the style objects for(CTStyle style : ctStyles.getStyleList()) { listStyle.add(new XWPFStyle(style, this)); } }
/** * compares the names of the Styles * * @param compStyle */ public boolean hasSameName(XWPFStyle compStyle) { CTStyle ctCompStyle = compStyle.getCTStyle(); String name = ctCompStyle.getName().getVal(); return name.equals(ctStyle.getName().getVal()); }
/** * get the styles which are related to parameter style * * @param style * @return all Styles of the parameterList */ private List<XWPFStyle> getUsedStyleList(XWPFStyle style, List<XWPFStyle> usedStyleList) { String basisStyleID = style.getBasisStyleID(); XWPFStyle basisStyle = getStyle(basisStyleID); if ((basisStyle != null) && (!usedStyleList.contains(basisStyle))) { usedStyleList.add(basisStyle); getUsedStyleList(basisStyle, usedStyleList); } String linkStyleID = style.getLinkStyleID(); XWPFStyle linkStyle = getStyle(linkStyleID); if ((linkStyle != null) && (!usedStyleList.contains(linkStyle))) { usedStyleList.add(linkStyle); getUsedStyleList(linkStyle, usedStyleList); } String nextStyleID = style.getNextStyleID(); XWPFStyle nextStyle = getStyle(nextStyleID); if ((nextStyle != null) && (!usedStyleList.contains(nextStyle))) { usedStyleList.add(linkStyle); getUsedStyleList(linkStyle, usedStyleList); } return usedStyleList; }
/** * Get the style with the specified name, if any. * * @param styleName The name of the style to get, e.g., "Heading 1" * @return {@link XWPFStyle} with the specified name, or null if not found. */ public XWPFStyle getStyleWithName(String styleName) { XWPFStyle style = null; for (XWPFStyle cand : listStyle) { if (styleName.equals(cand.getName())) { style = cand; break; } } return style; } }
/** * Get style by a styleID * * @param styleID styleID of the searched style * @return style */ public XWPFStyle getStyle(String styleID) { for (XWPFStyle style : listStyle) { try { if (style.getStyleId().equals(styleID)) return style; } catch (NullPointerException e) { // Ignore NPE } } return null; }
/** * get the style with the same name * if this style is not existing, return null */ public XWPFStyle getStyleWithSameName(XWPFStyle style){ for (XWPFStyle ownStyle : listStyle) { if(ownStyle.hasSameName(style)){ return ownStyle; } } return null; } }//end class
/** * Sets the ctStyles * * @param styles */ public void setStyles(CTStyles styles) { ctStyles = styles; // Build up all the style objects for (CTStyle style : ctStyles.getStyleArray()) { listStyle.add(new XWPFStyle(style, this)); } if (ctStyles.isSetDocDefaults()) { CTDocDefaults docDefaults = ctStyles.getDocDefaults(); if (docDefaults.isSetRPrDefault() && docDefaults.getRPrDefault().isSetRPr()) { defaultRunStyle = new XWPFDefaultRunStyle( docDefaults.getRPrDefault().getRPr()); } if (docDefaults.isSetPPrDefault() && docDefaults.getPPrDefault().isSetPPr()) { defaultParaStyle = new XWPFDefaultParagraphStyle( docDefaults.getPPrDefault().getPPr()); } } }
public static CTPPr getPPr( XWPFStyle style ) { if ( style == null ) { return null; } CTStyle ctStyle = style.getCTStyle(); if ( ctStyle == null ) { return null; } return ctStyle.getPPr(); }