public class SpecialChar extends Inline
To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.
Remarks:
A Microsoft Word document can include a number of special characters that represent fields, form fields, shapes, OLE objects, footnotes etc. For the list of special characters see ControlChar.
SpecialChar is an inline-node and can only be a child of Paragraph.
SpecialChar char is used as a base class for more specific classes that represent special characters that Aspose.Words provides programmatic access for. The SpecialChar class is also used itself to represent special character for which Aspose.Words does not provide detailed programmatic access.
Examples:
Shows how to use a DocumentVisitor implementation to remove all hidden content from a document.
public void removeHiddenContentFromDocument() throws Exception {
Document doc = new Document(getMyDir() + "Hidden content.docx");
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// Below are three types of fields which can accept a document visitor,
// which will allow it to visit the accepting node, and then traverse its child nodes in a depth-first manner.
// 1 - Paragraph node:
Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// 2 - Table node:
Table table = doc.getFirstSection().getBody().getTables().get(0);
table.accept(hiddenContentRemover);
// 3 - Document node:
doc.accept(hiddenContentRemover);
doc.save(getArtifactsDir() + "Font.RemoveHiddenContentFromDocument.docx");
}
/// <summary>
/// Removes all visited nodes marked as "hidden content".
/// </summary>
public static class RemoveHiddenContentVisitor extends DocumentVisitor {
/// <summary>
/// Called when a FieldStart node is encountered in the document.
/// </summary>
public int visitFieldStart(FieldStart fieldStart) {
if (fieldStart.getFont().getHidden())
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldEnd node is encountered in the document.
/// </summary>
public int visitFieldEnd(FieldEnd fieldEnd) {
if (fieldEnd.getFont().getHidden())
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldSeparator node is encountered in the document.
/// </summary>
public int visitFieldSeparator(FieldSeparator fieldSeparator) {
if (fieldSeparator.getFont().getHidden())
fieldSeparator.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public int visitRun(Run run) {
if (run.getFont().getHidden())
run.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Paragraph node is encountered in the document.
/// </summary>
public int visitParagraphStart(Paragraph paragraph) {
if (paragraph.getParagraphBreakFont().getHidden())
paragraph.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FormField is encountered in the document.
/// </summary>
public int visitFormField(FormField formField) {
if (formField.getFont().getHidden())
formField.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a GroupShape is encountered in the document.
/// </summary>
public int visitGroupShapeStart(GroupShape groupShape) {
if (groupShape.getFont().getHidden())
groupShape.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Shape is encountered in the document.
/// </summary>
public int visitShapeStart(Shape shape) {
if (shape.getFont().getHidden())
shape.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Comment is encountered in the document.
/// </summary>
public int visitCommentStart(Comment comment) {
if (comment.getFont().getHidden())
comment.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Footnote is encountered in the document.
/// </summary>
public int visitFootnoteStart(Footnote footnote) {
if (footnote.getFont().getHidden())
footnote.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a SpecialCharacter is encountered in the document.
/// </summary>
public int visitSpecialChar(SpecialChar specialChar) {
if (specialChar.getFont().getHidden())
specialChar.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when visiting of a Table node is ended in the document.
/// </summary>
public int visitTableEnd(Table table) {
// The content inside table cells may have the hidden content flag, but the tables themselves cannot.
// If this table had nothing but hidden content, this visitor would have removed all of it,
// and there would be no child nodes left.
// Thus, we can also treat the table itself as hidden content and remove it.
// Tables which are empty but do not have hidden content will have cells with empty paragraphs inside,
// which this visitor will not remove.
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when visiting of a Cell node is ended in the document.
/// </summary>
public int visitCellEnd(Cell cell) {
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when visiting of a Row node is ended in the document.
/// </summary>
public int visitRowEnd(Row row) {
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
}
| Modifier and Type | Method and Description |
|---|---|
boolean |
accept(DocumentVisitor visitor)
Accepts a visitor.
|
int |
getNodeType()
Returns
NodeType.SPECIAL_CHAR. |
java.lang.String |
getText()
Gets the special character that this node represents.
|
clearRunAttrs, fetchInheritedRunAttr, getDirectRunAttr, getDirectRunAttr, getDocument_IInline, getFont, getParentParagraph_IInline, getParentParagraph, isDeleteRevision, isFormatRevision, isInsertRevision, isMoveFromRevision, isMoveToRevision, removeMoveRevisions, removeRunAttr, setRunAttrdeepClone, getAncestor, getAncestor, getCustomNodeId, getDocument, getNextSibling, getParentNode, getPreviousSibling, getRange, isComposite, memberwiseClone, nextPreOrder, nodeTypeToString, previousPreOrder, remove, setCustomNodeId, toString, toString, toString, visitorActionToBoolpublic int getNodeType()
NodeType.SPECIAL_CHAR.
Examples:
Shows how to traverse a composite node's tree of child nodes.
public void recurseChildren() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
// Any node that can contain child nodes, such as the document itself, is composite.
Assert.assertTrue(doc.isComposite());
// Invoke the recursive function that will go through and print all the child nodes of a composite node.
traverseAllNodes(doc, 0);
}
/// <summary>
/// Recursively traverses a node tree while printing the type of each node
/// with an indent depending on depth as well as the contents of all inline nodes.
/// </summary>
public void traverseAllNodes(CompositeNode parentNode, int depth) {
for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
System.out.println(MessageFormat.format("{0}{1}", String.format(" ", depth), Node.nodeTypeToString(childNode.getNodeType())));
// Recurse into the node if it is a composite node. Otherwise, print its contents if it is an inline node.
if (childNode.isComposite()) {
System.out.println();
traverseAllNodes((CompositeNode) childNode, depth + 1);
} else if (childNode instanceof Inline) {
System.out.println(MessageFormat.format(" - \"{0}\"", childNode.getText().trim()));
} else {
System.out.println();
}
}
}
getNodeType in class NodeNodeType.SPECIAL_CHAR. The returned value is one of NodeType constants.public boolean accept(DocumentVisitor visitor) throws java.lang.Exception
Remarks:
Calls DocumentVisitor.visitSpecialChar(com.aspose.words.SpecialChar).
For more info see the Visitor design pattern.
Examples:
Shows how to use a DocumentVisitor implementation to remove all hidden content from a document.
public void removeHiddenContentFromDocument() throws Exception {
Document doc = new Document(getMyDir() + "Hidden content.docx");
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// Below are three types of fields which can accept a document visitor,
// which will allow it to visit the accepting node, and then traverse its child nodes in a depth-first manner.
// 1 - Paragraph node:
Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// 2 - Table node:
Table table = doc.getFirstSection().getBody().getTables().get(0);
table.accept(hiddenContentRemover);
// 3 - Document node:
doc.accept(hiddenContentRemover);
doc.save(getArtifactsDir() + "Font.RemoveHiddenContentFromDocument.docx");
}
/// <summary>
/// Removes all visited nodes marked as "hidden content".
/// </summary>
public static class RemoveHiddenContentVisitor extends DocumentVisitor {
/// <summary>
/// Called when a FieldStart node is encountered in the document.
/// </summary>
public int visitFieldStart(FieldStart fieldStart) {
if (fieldStart.getFont().getHidden())
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldEnd node is encountered in the document.
/// </summary>
public int visitFieldEnd(FieldEnd fieldEnd) {
if (fieldEnd.getFont().getHidden())
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldSeparator node is encountered in the document.
/// </summary>
public int visitFieldSeparator(FieldSeparator fieldSeparator) {
if (fieldSeparator.getFont().getHidden())
fieldSeparator.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public int visitRun(Run run) {
if (run.getFont().getHidden())
run.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Paragraph node is encountered in the document.
/// </summary>
public int visitParagraphStart(Paragraph paragraph) {
if (paragraph.getParagraphBreakFont().getHidden())
paragraph.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FormField is encountered in the document.
/// </summary>
public int visitFormField(FormField formField) {
if (formField.getFont().getHidden())
formField.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a GroupShape is encountered in the document.
/// </summary>
public int visitGroupShapeStart(GroupShape groupShape) {
if (groupShape.getFont().getHidden())
groupShape.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Shape is encountered in the document.
/// </summary>
public int visitShapeStart(Shape shape) {
if (shape.getFont().getHidden())
shape.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Comment is encountered in the document.
/// </summary>
public int visitCommentStart(Comment comment) {
if (comment.getFont().getHidden())
comment.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Footnote is encountered in the document.
/// </summary>
public int visitFootnoteStart(Footnote footnote) {
if (footnote.getFont().getHidden())
footnote.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a SpecialCharacter is encountered in the document.
/// </summary>
public int visitSpecialChar(SpecialChar specialChar) {
if (specialChar.getFont().getHidden())
specialChar.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when visiting of a Table node is ended in the document.
/// </summary>
public int visitTableEnd(Table table) {
// The content inside table cells may have the hidden content flag, but the tables themselves cannot.
// If this table had nothing but hidden content, this visitor would have removed all of it,
// and there would be no child nodes left.
// Thus, we can also treat the table itself as hidden content and remove it.
// Tables which are empty but do not have hidden content will have cells with empty paragraphs inside,
// which this visitor will not remove.
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when visiting of a Cell node is ended in the document.
/// </summary>
public int visitCellEnd(Cell cell) {
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when visiting of a Row node is ended in the document.
/// </summary>
public int visitRowEnd(Row row) {
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
}
public java.lang.String getText()
Examples:
Shows how to use a DocumentVisitor implementation to remove all hidden content from a document.
public void removeHiddenContentFromDocument() throws Exception {
Document doc = new Document(getMyDir() + "Hidden content.docx");
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// Below are three types of fields which can accept a document visitor,
// which will allow it to visit the accepting node, and then traverse its child nodes in a depth-first manner.
// 1 - Paragraph node:
Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// 2 - Table node:
Table table = doc.getFirstSection().getBody().getTables().get(0);
table.accept(hiddenContentRemover);
// 3 - Document node:
doc.accept(hiddenContentRemover);
doc.save(getArtifactsDir() + "Font.RemoveHiddenContentFromDocument.docx");
}
/// <summary>
/// Removes all visited nodes marked as "hidden content".
/// </summary>
public static class RemoveHiddenContentVisitor extends DocumentVisitor {
/// <summary>
/// Called when a FieldStart node is encountered in the document.
/// </summary>
public int visitFieldStart(FieldStart fieldStart) {
if (fieldStart.getFont().getHidden())
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldEnd node is encountered in the document.
/// </summary>
public int visitFieldEnd(FieldEnd fieldEnd) {
if (fieldEnd.getFont().getHidden())
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldSeparator node is encountered in the document.
/// </summary>
public int visitFieldSeparator(FieldSeparator fieldSeparator) {
if (fieldSeparator.getFont().getHidden())
fieldSeparator.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public int visitRun(Run run) {
if (run.getFont().getHidden())
run.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Paragraph node is encountered in the document.
/// </summary>
public int visitParagraphStart(Paragraph paragraph) {
if (paragraph.getParagraphBreakFont().getHidden())
paragraph.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FormField is encountered in the document.
/// </summary>
public int visitFormField(FormField formField) {
if (formField.getFont().getHidden())
formField.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a GroupShape is encountered in the document.
/// </summary>
public int visitGroupShapeStart(GroupShape groupShape) {
if (groupShape.getFont().getHidden())
groupShape.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Shape is encountered in the document.
/// </summary>
public int visitShapeStart(Shape shape) {
if (shape.getFont().getHidden())
shape.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Comment is encountered in the document.
/// </summary>
public int visitCommentStart(Comment comment) {
if (comment.getFont().getHidden())
comment.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Footnote is encountered in the document.
/// </summary>
public int visitFootnoteStart(Footnote footnote) {
if (footnote.getFont().getHidden())
footnote.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a SpecialCharacter is encountered in the document.
/// </summary>
public int visitSpecialChar(SpecialChar specialChar) {
if (specialChar.getFont().getHidden())
specialChar.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when visiting of a Table node is ended in the document.
/// </summary>
public int visitTableEnd(Table table) {
// The content inside table cells may have the hidden content flag, but the tables themselves cannot.
// If this table had nothing but hidden content, this visitor would have removed all of it,
// and there would be no child nodes left.
// Thus, we can also treat the table itself as hidden content and remove it.
// Tables which are empty but do not have hidden content will have cells with empty paragraphs inside,
// which this visitor will not remove.
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when visiting of a Cell node is ended in the document.
/// </summary>
public int visitCellEnd(Cell cell) {
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when visiting of a Row node is ended in the document.
/// </summary>
public int visitRowEnd(Row row) {
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
}