`
53873039oycg
  • 浏览: 822898 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

[简单]poi删除word 2007书签

    博客分类:
  • poi
 
阅读更多

      见代码:

     

//删除所有的书签
	public void removeAllDocBookMark(XWPFDocument xdoc) {
		List<XWPFParagraph> paraList = xdoc.getParagraphs();
		removeParaListBookMark(paraList);
		List<XWPFTable> tableList = xdoc.getTables();
		if (tableList != null) {
			for (XWPFTable table : tableList) {
				List<XWPFTableRow> rowList = table.getRows();
				if (rowList != null) {
					for (XWPFTableRow row : rowList) {
						List<XWPFTableCell> cellList = row.getTableCells();
						if (cellList != null) {
							for (XWPFTableCell cell : cellList) {
								removeParaListBookMark(cell.getParagraphs());
							}
						}
					}
				}
			}
		}
	}

	public void removeParaListBookMark(List<XWPFParagraph> paraList) {
		if (paraList != null) {
			for (XWPFParagraph para : paraList) {
				removeParagraphBookMark(para);
			}
		}
	}

	////删除段落内所有的书签 去掉w:bookmarkStart/w:bookmarkEnd
	public void removeParagraphBookMark(XWPFParagraph para) {
		List<CTBookmark> bookmarkStartList = para.getCTP()
				.getBookmarkStartList();
		if (bookmarkStartList == null) {
			return;
		}
		for (int i = bookmarkStartList.size() - 1; i >= 0; i--) {
			Node bookStartNode = bookmarkStartList.get(i).getDomNode();
			printNodeAllAttributeValue(bookStartNode);
			bookStartNode.getParentNode().removeChild(bookStartNode);
		}
		List<CTMarkupRange> bookmarkEndList = para.getCTP()
				.getBookmarkEndList();
		if (bookmarkEndList == null) {
			return;
		}
		for (int i = bookmarkEndList.size() - 1; i >= 0; i--) {
			Node bookEndNode = bookmarkEndList.get(i).getDomNode();
			printNodeAllAttributeValue(bookEndNode);
			bookEndNode.getParentNode().removeChild(bookEndNode);
		}

	}

	public void printNodeAllAttributeValue(Node node) {
		NamedNodeMap nodeAttr = node.getAttributes();
		if (nodeAttr != null) {
			int numAttrs = nodeAttr.getLength();
			for (int i = 0; i < numAttrs; i++) {
				Attr attr = (Attr) nodeAttr.item(i);
				String attrName = attr.getNodeName();
				String attrValue = attr.getNodeValue();
				System.out.println(node.getNodeName() + " 属性="
						+ attrName + " 值= " + attrValue);
			}
		}
	}

    全文完。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics