java.lang.Object
com.aspose.words.DocumentVisitor
public abstract class DocumentVisitor
- extends java.lang.Object
Base class for custom document visitors.
With DocumentVisitor you can define and execute custom operations
that require enumeration over the document tree.
For example, Aspose.Words uses DocumentVisitor internally for saving Document
in various formats and for other operations like finding fields or bookmarks over
a fragment of a document.
To use DocumentVisitor:
- Create a class derived from DocumentVisitor.
- Override and provide implementations for some or all of the VisitXXX methods
to perform some custom operations.
- Call Node.accept(com.aspose.words.DocumentVisitor) on the Node that
you want to start the enumeration from.
DocumentVisitor provides default implementations for all of the VisitXXX methods
to make it easier to create new document visitors as only the methods required for the particular
visitor need to be overridden. It is not necessary to override all of the visitor methods.
For more information see the Visitor design pattern.
Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.
public void toText() throws Exception
{
// Open the document we want to convert.
Document doc = new Document(getMyDir() + "Visitor.ToText.doc");
// Create an object that inherits from the DocumentVisitor class.
MyDocToTxtWriter myConverter = new MyDocToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
System.out.println(myConverter.getText());
}
/**
* Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
*/
public class MyDocToTxtWriter extends DocumentVisitor
{
public MyDocToTxtWriter() throws Exception
{
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/**
* Gets the plain text of the document that was accumulated by the visitor.
*/
public String getText() throws Exception
{
return mBuilder.toString();
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
appendText(run.getText());
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows
// after a field start character. We want to skip field codes and output field
// result only, therefore we use a flag to suspend the output while inside a field code.
//
// Note this is a very simplistic implementation and will not work very well
// if you have nested fields in a document.
mIsSkipText = true;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldSeparator node is encountered in the document.
*/
public int visitFieldSeparator(FieldSeparator fieldSeparator) throws Exception
{
// Once reached a field separator node, we enable the output because we are
// now entering the field result nodes.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
// Make sure we enable the output when reached a field end because some fields
// do not have field separator and do not have field result.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Paragraph node is ended in the document.
*/
public int visitParagraphEnd(Paragraph paragraph) throws Exception
{
// When outputting to plain text we output Cr+Lf characters.
appendText(ControlChar.CR_LF);
return VisitorAction.CONTINUE;
}
public int visitBodyStart(Body body) throws Exception
{
// We can detect beginning and end of all composite nodes such as Section, Body,
// Table, Paragraph etc and provide custom handling for them.
mBuilder.append("*** Body Started ***\r\n");
return VisitorAction.CONTINUE;
}
public int visitBodyEnd(Body body) throws Exception
{
mBuilder.append("*** Body Ended ***\r\n");
return VisitorAction.CONTINUE;
}
/**
* Called when a HeaderFooter node is encountered in the document.
*/
public int visitHeaderFooterStart(HeaderFooter headerFooter) throws Exception
{
// Returning this value from a visitor method causes visiting of this
// node to stop and move on to visiting the next sibling node.
// The net effect in this example is that the text of headers and footers
// is not included in the resulting output.
return VisitorAction.SKIP_THIS_NODE;
}
/**
* Adds text to the current output. Honours the enabled/disabled output flag.
*/
private void appendText(String text) throws Exception
{
if (!mIsSkipText)
mBuilder.append(text);
}
private final StringBuilder mBuilder;
private boolean mIsSkipText;
}
visitAbsolutePositionTab | |
public int visitAbsolutePositionTab(AbsolutePositionTab tab)
throws java.lang.Exception |
-
Called when a AbsolutePositionTab node is encountered in the document.
- Parameters:
tab
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitBodyEnd | |
public int visitBodyEnd(Body body)
throws java.lang.Exception |
-
Called when enumeration of the main text story in a section has ended.
- Parameters:
body
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.
public void toText() throws Exception
{
// Open the document we want to convert.
Document doc = new Document(getMyDir() + "Visitor.ToText.doc");
// Create an object that inherits from the DocumentVisitor class.
MyDocToTxtWriter myConverter = new MyDocToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
System.out.println(myConverter.getText());
}
/**
* Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
*/
public class MyDocToTxtWriter extends DocumentVisitor
{
public MyDocToTxtWriter() throws Exception
{
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/**
* Gets the plain text of the document that was accumulated by the visitor.
*/
public String getText() throws Exception
{
return mBuilder.toString();
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
appendText(run.getText());
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows
// after a field start character. We want to skip field codes and output field
// result only, therefore we use a flag to suspend the output while inside a field code.
//
// Note this is a very simplistic implementation and will not work very well
// if you have nested fields in a document.
mIsSkipText = true;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldSeparator node is encountered in the document.
*/
public int visitFieldSeparator(FieldSeparator fieldSeparator) throws Exception
{
// Once reached a field separator node, we enable the output because we are
// now entering the field result nodes.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
// Make sure we enable the output when reached a field end because some fields
// do not have field separator and do not have field result.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Paragraph node is ended in the document.
*/
public int visitParagraphEnd(Paragraph paragraph) throws Exception
{
// When outputting to plain text we output Cr+Lf characters.
appendText(ControlChar.CR_LF);
return VisitorAction.CONTINUE;
}
public int visitBodyStart(Body body) throws Exception
{
// We can detect beginning and end of all composite nodes such as Section, Body,
// Table, Paragraph etc and provide custom handling for them.
mBuilder.append("*** Body Started ***\r\n");
return VisitorAction.CONTINUE;
}
public int visitBodyEnd(Body body) throws Exception
{
mBuilder.append("*** Body Ended ***\r\n");
return VisitorAction.CONTINUE;
}
/**
* Called when a HeaderFooter node is encountered in the document.
*/
public int visitHeaderFooterStart(HeaderFooter headerFooter) throws Exception
{
// Returning this value from a visitor method causes visiting of this
// node to stop and move on to visiting the next sibling node.
// The net effect in this example is that the text of headers and footers
// is not included in the resulting output.
return VisitorAction.SKIP_THIS_NODE;
}
/**
* Adds text to the current output. Honours the enabled/disabled output flag.
*/
private void appendText(String text) throws Exception
{
if (!mIsSkipText)
mBuilder.append(text);
}
private final StringBuilder mBuilder;
private boolean mIsSkipText;
}
visitBodyStart | |
public int visitBodyStart(Body body)
throws java.lang.Exception |
-
Called when enumeration of the main text story in a section has started.
- Parameters:
body
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.
public void toText() throws Exception
{
// Open the document we want to convert.
Document doc = new Document(getMyDir() + "Visitor.ToText.doc");
// Create an object that inherits from the DocumentVisitor class.
MyDocToTxtWriter myConverter = new MyDocToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
System.out.println(myConverter.getText());
}
/**
* Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
*/
public class MyDocToTxtWriter extends DocumentVisitor
{
public MyDocToTxtWriter() throws Exception
{
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/**
* Gets the plain text of the document that was accumulated by the visitor.
*/
public String getText() throws Exception
{
return mBuilder.toString();
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
appendText(run.getText());
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows
// after a field start character. We want to skip field codes and output field
// result only, therefore we use a flag to suspend the output while inside a field code.
//
// Note this is a very simplistic implementation and will not work very well
// if you have nested fields in a document.
mIsSkipText = true;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldSeparator node is encountered in the document.
*/
public int visitFieldSeparator(FieldSeparator fieldSeparator) throws Exception
{
// Once reached a field separator node, we enable the output because we are
// now entering the field result nodes.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
// Make sure we enable the output when reached a field end because some fields
// do not have field separator and do not have field result.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Paragraph node is ended in the document.
*/
public int visitParagraphEnd(Paragraph paragraph) throws Exception
{
// When outputting to plain text we output Cr+Lf characters.
appendText(ControlChar.CR_LF);
return VisitorAction.CONTINUE;
}
public int visitBodyStart(Body body) throws Exception
{
// We can detect beginning and end of all composite nodes such as Section, Body,
// Table, Paragraph etc and provide custom handling for them.
mBuilder.append("*** Body Started ***\r\n");
return VisitorAction.CONTINUE;
}
public int visitBodyEnd(Body body) throws Exception
{
mBuilder.append("*** Body Ended ***\r\n");
return VisitorAction.CONTINUE;
}
/**
* Called when a HeaderFooter node is encountered in the document.
*/
public int visitHeaderFooterStart(HeaderFooter headerFooter) throws Exception
{
// Returning this value from a visitor method causes visiting of this
// node to stop and move on to visiting the next sibling node.
// The net effect in this example is that the text of headers and footers
// is not included in the resulting output.
return VisitorAction.SKIP_THIS_NODE;
}
/**
* Adds text to the current output. Honours the enabled/disabled output flag.
*/
private void appendText(String text) throws Exception
{
if (!mIsSkipText)
mBuilder.append(text);
}
private final StringBuilder mBuilder;
private boolean mIsSkipText;
}
visitBookmarkEnd | |
public int visitBookmarkEnd(BookmarkEnd bookmarkEnd)
throws java.lang.Exception |
-
Called when an end of a bookmark is encountered in the document.
- Parameters:
bookmarkEnd
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitBookmarkStart | |
public int visitBookmarkStart(BookmarkStart bookmarkStart)
throws java.lang.Exception |
-
Called when a start of a bookmark is encountered in the document.
- Parameters:
bookmarkStart
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitBuildingBlockEnd | |
public int visitBuildingBlockEnd(BuildingBlock block)
throws java.lang.Exception |
-
Called when enumeration of a building block has ended.
Note: A building block node and its children are not visited when you execute a
Visitor over a Document. If you want to execute a Visitor over a
building block, you need to execute the visitor over GlossaryDocument or
call BuildingBlock.accept(com.aspose.words.DocumentVisitor).
- Parameters:
block
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitBuildingBlockStart | |
public int visitBuildingBlockStart(BuildingBlock block)
throws java.lang.Exception |
-
Called when enumeration of a building block has started.
Note: A building block node and its children are not visited when you execute a
Visitor over a Document. If you want to execute a Visitor over a
building block, you need to execute the visitor over GlossaryDocument or
call BuildingBlock.accept(com.aspose.words.DocumentVisitor).
- Parameters:
block
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitCellEnd | |
public int visitCellEnd(Cell cell)
throws java.lang.Exception |
-
Called when enumeration of a table cell has ended.
- Parameters:
cell
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Implements the Visitor Pattern to remove all content formatted as hidden from the document.
public void removeHiddenContentFromDocument() throws Exception
{
// Open the document we want to remove hidden content from.
Document doc = new Document(getMyDir() + "Font.Hidden.doc");
// Create an object that inherits from the DocumentVisitor class.
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
// We can run it over the entire the document like so:
doc.accept(hiddenContentRemover);
// Or we can run it on only a specific node.
Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// Or over a different type of node like below.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.accept(hiddenContentRemover);
doc.save(getMyDir() + "Font.Hidden Out.doc");
}
/**
* This class when executed will remove all hidden content from the Document. Implemented as a Visitor.
*/
private class RemoveHiddenContentVisitor extends DocumentVisitor
{
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// If this node is hidden, then remove it.
if (isHidden(fieldStart))
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
if (isHidden(fieldEnd))
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
if (isHidden(run))
run.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Paragraph node is encountered in the document.
*/
public int visitParagraphStart(Paragraph paragraph) throws Exception
{
if (isHidden(paragraph))
paragraph.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FormField is encountered in the document.
*/
public int visitFormField(FormField field) throws Exception
{
if (isHidden(field))
field.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a GroupShape is encountered in the document.
*/
public int visitGroupShapeStart(GroupShape groupShape) throws Exception
{
if (isHidden(groupShape))
groupShape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Shape is encountered in the document.
*/
public int visitShapeStart(Shape shape) throws Exception
{
if (isHidden(shape))
shape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Comment is encountered in the document.
*/
public int visitCommentStart(Comment comment) throws Exception
{
if (isHidden(comment))
comment.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Footnote is encountered in the document.
*/
public int visitFootnoteStart(Footnote footnote) throws Exception
{
if (isHidden(footnote))
footnote.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Table node is ended in the document.
*/
public int visitTableEnd(Table table) throws Exception
{
// At the moment there is no way to tell if a particular Table/Row/Cell is hidden.
// Instead, if the content of a table is hidden, then all inline child nodes of the table should be
// hidden and thus removed by previous visits as well. This will result in the container being empty
// so if this is the case we know to remove the table node.
//
// Note that a table which is not hidden but simply has no content will not be affected by this algorthim,
// as technically they are not completely empty (for example a properly formed Cell will have at least
// an empty paragraph in it)
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Cell node is ended in the document.
*/
public int visitCellEnd(Cell cell) throws Exception
{
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Row node is ended in the document.
*/
public int visitRowEnd(Row row) throws Exception
{
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a SpecialCharacter is encountered in the document.
*/
public int visitSpecialChar(SpecialChar character) throws Exception
{
if (isHidden(character))
character.remove();
return VisitorAction.CONTINUE;
}
/**
* Returns true if the node passed is set as hidden, returns false if it is visible.
*/
private boolean isHidden(Node node) throws Exception
{
if (node instanceof Inline)
{
// If the node is Inline then cast it to retrieve the Font property which contains the hidden property
Inline currentNode = (Inline)node;
return currentNode.getFont().getHidden();
}
else if (node.getNodeType() == NodeType.PARAGRAPH)
{
// If the node is a paragraph cast it to retrieve the ParagraphBreakFont which contains the hidden property
Paragraph para = (Paragraph)node;
return para.getParagraphBreakFont().getHidden();
}
else if (node instanceof ShapeBase)
{
// Node is a shape or groupshape.
ShapeBase shape = (ShapeBase)node;
return shape.getFont().getHidden();
}
else if (node instanceof InlineStory)
{
// Node is a comment or footnote.
InlineStory inlineStory = (InlineStory)node;
return inlineStory.getFont().getHidden();
}
// A node that is passed to this method which does not contain a hidden property will end up here.
// By default nodes are not hidden so return false.
return false;
}
}
visitCellStart | |
public int visitCellStart(Cell cell)
throws java.lang.Exception |
-
Called when enumeration of a table cell has started.
- Parameters:
cell
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitCommentEnd | |
public int visitCommentEnd(Comment comment)
throws java.lang.Exception |
-
Called when enumeration of a comment text has ended.
- Parameters:
comment
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitCommentRangeEnd | |
public int visitCommentRangeEnd(CommentRangeEnd commentRangeEnd)
throws java.lang.Exception |
-
Called when the end of a commented range of text is encountered.
- Parameters:
commentRangeEnd
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitCommentRangeStart | |
public int visitCommentRangeStart(CommentRangeStart commentRangeStart)
throws java.lang.Exception |
-
Called when the start of a commented range of text is encountered.
- Parameters:
commentRangeStart
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitCommentStart | |
public int visitCommentStart(Comment comment)
throws java.lang.Exception |
-
Called when enumeration of a comment text has started.
- Parameters:
comment
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Implements the Visitor Pattern to remove all content formatted as hidden from the document.
public void removeHiddenContentFromDocument() throws Exception
{
// Open the document we want to remove hidden content from.
Document doc = new Document(getMyDir() + "Font.Hidden.doc");
// Create an object that inherits from the DocumentVisitor class.
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
// We can run it over the entire the document like so:
doc.accept(hiddenContentRemover);
// Or we can run it on only a specific node.
Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// Or over a different type of node like below.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.accept(hiddenContentRemover);
doc.save(getMyDir() + "Font.Hidden Out.doc");
}
/**
* This class when executed will remove all hidden content from the Document. Implemented as a Visitor.
*/
private class RemoveHiddenContentVisitor extends DocumentVisitor
{
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// If this node is hidden, then remove it.
if (isHidden(fieldStart))
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
if (isHidden(fieldEnd))
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
if (isHidden(run))
run.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Paragraph node is encountered in the document.
*/
public int visitParagraphStart(Paragraph paragraph) throws Exception
{
if (isHidden(paragraph))
paragraph.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FormField is encountered in the document.
*/
public int visitFormField(FormField field) throws Exception
{
if (isHidden(field))
field.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a GroupShape is encountered in the document.
*/
public int visitGroupShapeStart(GroupShape groupShape) throws Exception
{
if (isHidden(groupShape))
groupShape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Shape is encountered in the document.
*/
public int visitShapeStart(Shape shape) throws Exception
{
if (isHidden(shape))
shape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Comment is encountered in the document.
*/
public int visitCommentStart(Comment comment) throws Exception
{
if (isHidden(comment))
comment.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Footnote is encountered in the document.
*/
public int visitFootnoteStart(Footnote footnote) throws Exception
{
if (isHidden(footnote))
footnote.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Table node is ended in the document.
*/
public int visitTableEnd(Table table) throws Exception
{
// At the moment there is no way to tell if a particular Table/Row/Cell is hidden.
// Instead, if the content of a table is hidden, then all inline child nodes of the table should be
// hidden and thus removed by previous visits as well. This will result in the container being empty
// so if this is the case we know to remove the table node.
//
// Note that a table which is not hidden but simply has no content will not be affected by this algorthim,
// as technically they are not completely empty (for example a properly formed Cell will have at least
// an empty paragraph in it)
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Cell node is ended in the document.
*/
public int visitCellEnd(Cell cell) throws Exception
{
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Row node is ended in the document.
*/
public int visitRowEnd(Row row) throws Exception
{
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a SpecialCharacter is encountered in the document.
*/
public int visitSpecialChar(SpecialChar character) throws Exception
{
if (isHidden(character))
character.remove();
return VisitorAction.CONTINUE;
}
/**
* Returns true if the node passed is set as hidden, returns false if it is visible.
*/
private boolean isHidden(Node node) throws Exception
{
if (node instanceof Inline)
{
// If the node is Inline then cast it to retrieve the Font property which contains the hidden property
Inline currentNode = (Inline)node;
return currentNode.getFont().getHidden();
}
else if (node.getNodeType() == NodeType.PARAGRAPH)
{
// If the node is a paragraph cast it to retrieve the ParagraphBreakFont which contains the hidden property
Paragraph para = (Paragraph)node;
return para.getParagraphBreakFont().getHidden();
}
else if (node instanceof ShapeBase)
{
// Node is a shape or groupshape.
ShapeBase shape = (ShapeBase)node;
return shape.getFont().getHidden();
}
else if (node instanceof InlineStory)
{
// Node is a comment or footnote.
InlineStory inlineStory = (InlineStory)node;
return inlineStory.getFont().getHidden();
}
// A node that is passed to this method which does not contain a hidden property will end up here.
// By default nodes are not hidden so return false.
return false;
}
}
visitDocumentEnd | |
public int visitDocumentEnd(Document doc)
throws java.lang.Exception |
-
Called when enumeration of the document has finished.
- Parameters:
doc
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitDocumentStart | |
public int visitDocumentStart(Document doc)
throws java.lang.Exception |
-
Called when enumeration of the document has started.
- Parameters:
doc
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitEditableRangeEnd | |
public int visitEditableRangeEnd(EditableRangeEnd editableRangeEnd)
throws java.lang.Exception |
-
Called when an end of an editable range is encountered in the document.
- Parameters:
editableRangeEnd
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitEditableRangeStart | |
public int visitEditableRangeStart(EditableRangeStart editableRangeStart)
throws java.lang.Exception |
-
Called when a start of an editable range is encountered in the document.
- Parameters:
editableRangeStart
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitFieldEnd | |
public int visitFieldEnd(FieldEnd fieldEnd)
throws java.lang.Exception |
-
Called when a field ends in the document.
For more info see visitFieldStart(com.aspose.words.FieldStart)
- Parameters:
fieldEnd
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.
public void toText() throws Exception
{
// Open the document we want to convert.
Document doc = new Document(getMyDir() + "Visitor.ToText.doc");
// Create an object that inherits from the DocumentVisitor class.
MyDocToTxtWriter myConverter = new MyDocToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
System.out.println(myConverter.getText());
}
/**
* Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
*/
public class MyDocToTxtWriter extends DocumentVisitor
{
public MyDocToTxtWriter() throws Exception
{
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/**
* Gets the plain text of the document that was accumulated by the visitor.
*/
public String getText() throws Exception
{
return mBuilder.toString();
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
appendText(run.getText());
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows
// after a field start character. We want to skip field codes and output field
// result only, therefore we use a flag to suspend the output while inside a field code.
//
// Note this is a very simplistic implementation and will not work very well
// if you have nested fields in a document.
mIsSkipText = true;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldSeparator node is encountered in the document.
*/
public int visitFieldSeparator(FieldSeparator fieldSeparator) throws Exception
{
// Once reached a field separator node, we enable the output because we are
// now entering the field result nodes.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
// Make sure we enable the output when reached a field end because some fields
// do not have field separator and do not have field result.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Paragraph node is ended in the document.
*/
public int visitParagraphEnd(Paragraph paragraph) throws Exception
{
// When outputting to plain text we output Cr+Lf characters.
appendText(ControlChar.CR_LF);
return VisitorAction.CONTINUE;
}
public int visitBodyStart(Body body) throws Exception
{
// We can detect beginning and end of all composite nodes such as Section, Body,
// Table, Paragraph etc and provide custom handling for them.
mBuilder.append("*** Body Started ***\r\n");
return VisitorAction.CONTINUE;
}
public int visitBodyEnd(Body body) throws Exception
{
mBuilder.append("*** Body Ended ***\r\n");
return VisitorAction.CONTINUE;
}
/**
* Called when a HeaderFooter node is encountered in the document.
*/
public int visitHeaderFooterStart(HeaderFooter headerFooter) throws Exception
{
// Returning this value from a visitor method causes visiting of this
// node to stop and move on to visiting the next sibling node.
// The net effect in this example is that the text of headers and footers
// is not included in the resulting output.
return VisitorAction.SKIP_THIS_NODE;
}
/**
* Adds text to the current output. Honours the enabled/disabled output flag.
*/
private void appendText(String text) throws Exception
{
if (!mIsSkipText)
mBuilder.append(text);
}
private final StringBuilder mBuilder;
private boolean mIsSkipText;
}
visitFieldSeparator | |
public int visitFieldSeparator(FieldSeparator fieldSeparator)
throws java.lang.Exception |
-
Called when a field separator is encountered in the document.
The field separator separates field code from field value in the document. Note that some
fields have only field code and do not have field separator and field value.
For more info see visitFieldStart(com.aspose.words.FieldStart)
- Parameters:
fieldSeparator
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.
public void toText() throws Exception
{
// Open the document we want to convert.
Document doc = new Document(getMyDir() + "Visitor.ToText.doc");
// Create an object that inherits from the DocumentVisitor class.
MyDocToTxtWriter myConverter = new MyDocToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
System.out.println(myConverter.getText());
}
/**
* Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
*/
public class MyDocToTxtWriter extends DocumentVisitor
{
public MyDocToTxtWriter() throws Exception
{
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/**
* Gets the plain text of the document that was accumulated by the visitor.
*/
public String getText() throws Exception
{
return mBuilder.toString();
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
appendText(run.getText());
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows
// after a field start character. We want to skip field codes and output field
// result only, therefore we use a flag to suspend the output while inside a field code.
//
// Note this is a very simplistic implementation and will not work very well
// if you have nested fields in a document.
mIsSkipText = true;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldSeparator node is encountered in the document.
*/
public int visitFieldSeparator(FieldSeparator fieldSeparator) throws Exception
{
// Once reached a field separator node, we enable the output because we are
// now entering the field result nodes.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
// Make sure we enable the output when reached a field end because some fields
// do not have field separator and do not have field result.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Paragraph node is ended in the document.
*/
public int visitParagraphEnd(Paragraph paragraph) throws Exception
{
// When outputting to plain text we output Cr+Lf characters.
appendText(ControlChar.CR_LF);
return VisitorAction.CONTINUE;
}
public int visitBodyStart(Body body) throws Exception
{
// We can detect beginning and end of all composite nodes such as Section, Body,
// Table, Paragraph etc and provide custom handling for them.
mBuilder.append("*** Body Started ***\r\n");
return VisitorAction.CONTINUE;
}
public int visitBodyEnd(Body body) throws Exception
{
mBuilder.append("*** Body Ended ***\r\n");
return VisitorAction.CONTINUE;
}
/**
* Called when a HeaderFooter node is encountered in the document.
*/
public int visitHeaderFooterStart(HeaderFooter headerFooter) throws Exception
{
// Returning this value from a visitor method causes visiting of this
// node to stop and move on to visiting the next sibling node.
// The net effect in this example is that the text of headers and footers
// is not included in the resulting output.
return VisitorAction.SKIP_THIS_NODE;
}
/**
* Adds text to the current output. Honours the enabled/disabled output flag.
*/
private void appendText(String text) throws Exception
{
if (!mIsSkipText)
mBuilder.append(text);
}
private final StringBuilder mBuilder;
private boolean mIsSkipText;
}
visitFieldStart | |
public int visitFieldStart(FieldStart fieldStart)
throws java.lang.Exception |
-
Called when a field starts in the document.
A field in a Word Word document consists of a field code and field value.
For example, a field that displays a page number can be represented as follows:
[FieldStart]PAGE[FieldSeparator]98[FieldEnd]
The field separator separates field code from field value in the document. Note that some
fields have only field code and do not have field separator and field value.
Fields can be nested.
- Parameters:
fieldStart
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.
public void toText() throws Exception
{
// Open the document we want to convert.
Document doc = new Document(getMyDir() + "Visitor.ToText.doc");
// Create an object that inherits from the DocumentVisitor class.
MyDocToTxtWriter myConverter = new MyDocToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
System.out.println(myConverter.getText());
}
/**
* Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
*/
public class MyDocToTxtWriter extends DocumentVisitor
{
public MyDocToTxtWriter() throws Exception
{
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/**
* Gets the plain text of the document that was accumulated by the visitor.
*/
public String getText() throws Exception
{
return mBuilder.toString();
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
appendText(run.getText());
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows
// after a field start character. We want to skip field codes and output field
// result only, therefore we use a flag to suspend the output while inside a field code.
//
// Note this is a very simplistic implementation and will not work very well
// if you have nested fields in a document.
mIsSkipText = true;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldSeparator node is encountered in the document.
*/
public int visitFieldSeparator(FieldSeparator fieldSeparator) throws Exception
{
// Once reached a field separator node, we enable the output because we are
// now entering the field result nodes.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
// Make sure we enable the output when reached a field end because some fields
// do not have field separator and do not have field result.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Paragraph node is ended in the document.
*/
public int visitParagraphEnd(Paragraph paragraph) throws Exception
{
// When outputting to plain text we output Cr+Lf characters.
appendText(ControlChar.CR_LF);
return VisitorAction.CONTINUE;
}
public int visitBodyStart(Body body) throws Exception
{
// We can detect beginning and end of all composite nodes such as Section, Body,
// Table, Paragraph etc and provide custom handling for them.
mBuilder.append("*** Body Started ***\r\n");
return VisitorAction.CONTINUE;
}
public int visitBodyEnd(Body body) throws Exception
{
mBuilder.append("*** Body Ended ***\r\n");
return VisitorAction.CONTINUE;
}
/**
* Called when a HeaderFooter node is encountered in the document.
*/
public int visitHeaderFooterStart(HeaderFooter headerFooter) throws Exception
{
// Returning this value from a visitor method causes visiting of this
// node to stop and move on to visiting the next sibling node.
// The net effect in this example is that the text of headers and footers
// is not included in the resulting output.
return VisitorAction.SKIP_THIS_NODE;
}
/**
* Adds text to the current output. Honours the enabled/disabled output flag.
*/
private void appendText(String text) throws Exception
{
if (!mIsSkipText)
mBuilder.append(text);
}
private final StringBuilder mBuilder;
private boolean mIsSkipText;
}
visitFootnoteEnd | |
public int visitFootnoteEnd(Footnote footnote)
throws java.lang.Exception |
-
Called when enumeration of a footnote or endnote text has ended.
- Parameters:
footnote
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitFootnoteStart | |
public int visitFootnoteStart(Footnote footnote)
throws java.lang.Exception |
-
Called when enumeration of a footnote or endnote text has started.
- Parameters:
footnote
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Implements the Visitor Pattern to remove all content formatted as hidden from the document.
public void removeHiddenContentFromDocument() throws Exception
{
// Open the document we want to remove hidden content from.
Document doc = new Document(getMyDir() + "Font.Hidden.doc");
// Create an object that inherits from the DocumentVisitor class.
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
// We can run it over the entire the document like so:
doc.accept(hiddenContentRemover);
// Or we can run it on only a specific node.
Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// Or over a different type of node like below.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.accept(hiddenContentRemover);
doc.save(getMyDir() + "Font.Hidden Out.doc");
}
/**
* This class when executed will remove all hidden content from the Document. Implemented as a Visitor.
*/
private class RemoveHiddenContentVisitor extends DocumentVisitor
{
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// If this node is hidden, then remove it.
if (isHidden(fieldStart))
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
if (isHidden(fieldEnd))
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
if (isHidden(run))
run.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Paragraph node is encountered in the document.
*/
public int visitParagraphStart(Paragraph paragraph) throws Exception
{
if (isHidden(paragraph))
paragraph.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FormField is encountered in the document.
*/
public int visitFormField(FormField field) throws Exception
{
if (isHidden(field))
field.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a GroupShape is encountered in the document.
*/
public int visitGroupShapeStart(GroupShape groupShape) throws Exception
{
if (isHidden(groupShape))
groupShape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Shape is encountered in the document.
*/
public int visitShapeStart(Shape shape) throws Exception
{
if (isHidden(shape))
shape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Comment is encountered in the document.
*/
public int visitCommentStart(Comment comment) throws Exception
{
if (isHidden(comment))
comment.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Footnote is encountered in the document.
*/
public int visitFootnoteStart(Footnote footnote) throws Exception
{
if (isHidden(footnote))
footnote.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Table node is ended in the document.
*/
public int visitTableEnd(Table table) throws Exception
{
// At the moment there is no way to tell if a particular Table/Row/Cell is hidden.
// Instead, if the content of a table is hidden, then all inline child nodes of the table should be
// hidden and thus removed by previous visits as well. This will result in the container being empty
// so if this is the case we know to remove the table node.
//
// Note that a table which is not hidden but simply has no content will not be affected by this algorthim,
// as technically they are not completely empty (for example a properly formed Cell will have at least
// an empty paragraph in it)
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Cell node is ended in the document.
*/
public int visitCellEnd(Cell cell) throws Exception
{
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Row node is ended in the document.
*/
public int visitRowEnd(Row row) throws Exception
{
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a SpecialCharacter is encountered in the document.
*/
public int visitSpecialChar(SpecialChar character) throws Exception
{
if (isHidden(character))
character.remove();
return VisitorAction.CONTINUE;
}
/**
* Returns true if the node passed is set as hidden, returns false if it is visible.
*/
private boolean isHidden(Node node) throws Exception
{
if (node instanceof Inline)
{
// If the node is Inline then cast it to retrieve the Font property which contains the hidden property
Inline currentNode = (Inline)node;
return currentNode.getFont().getHidden();
}
else if (node.getNodeType() == NodeType.PARAGRAPH)
{
// If the node is a paragraph cast it to retrieve the ParagraphBreakFont which contains the hidden property
Paragraph para = (Paragraph)node;
return para.getParagraphBreakFont().getHidden();
}
else if (node instanceof ShapeBase)
{
// Node is a shape or groupshape.
ShapeBase shape = (ShapeBase)node;
return shape.getFont().getHidden();
}
else if (node instanceof InlineStory)
{
// Node is a comment or footnote.
InlineStory inlineStory = (InlineStory)node;
return inlineStory.getFont().getHidden();
}
// A node that is passed to this method which does not contain a hidden property will end up here.
// By default nodes are not hidden so return false.
return false;
}
}
visitFormField | |
public int visitFormField(FormField formField)
throws java.lang.Exception |
-
Called when a form field is encountered in the document.
- Parameters:
formField
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Implements the Visitor Pattern to remove all content formatted as hidden from the document.
public void removeHiddenContentFromDocument() throws Exception
{
// Open the document we want to remove hidden content from.
Document doc = new Document(getMyDir() + "Font.Hidden.doc");
// Create an object that inherits from the DocumentVisitor class.
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
// We can run it over the entire the document like so:
doc.accept(hiddenContentRemover);
// Or we can run it on only a specific node.
Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// Or over a different type of node like below.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.accept(hiddenContentRemover);
doc.save(getMyDir() + "Font.Hidden Out.doc");
}
/**
* This class when executed will remove all hidden content from the Document. Implemented as a Visitor.
*/
private class RemoveHiddenContentVisitor extends DocumentVisitor
{
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// If this node is hidden, then remove it.
if (isHidden(fieldStart))
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
if (isHidden(fieldEnd))
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
if (isHidden(run))
run.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Paragraph node is encountered in the document.
*/
public int visitParagraphStart(Paragraph paragraph) throws Exception
{
if (isHidden(paragraph))
paragraph.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FormField is encountered in the document.
*/
public int visitFormField(FormField field) throws Exception
{
if (isHidden(field))
field.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a GroupShape is encountered in the document.
*/
public int visitGroupShapeStart(GroupShape groupShape) throws Exception
{
if (isHidden(groupShape))
groupShape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Shape is encountered in the document.
*/
public int visitShapeStart(Shape shape) throws Exception
{
if (isHidden(shape))
shape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Comment is encountered in the document.
*/
public int visitCommentStart(Comment comment) throws Exception
{
if (isHidden(comment))
comment.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Footnote is encountered in the document.
*/
public int visitFootnoteStart(Footnote footnote) throws Exception
{
if (isHidden(footnote))
footnote.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Table node is ended in the document.
*/
public int visitTableEnd(Table table) throws Exception
{
// At the moment there is no way to tell if a particular Table/Row/Cell is hidden.
// Instead, if the content of a table is hidden, then all inline child nodes of the table should be
// hidden and thus removed by previous visits as well. This will result in the container being empty
// so if this is the case we know to remove the table node.
//
// Note that a table which is not hidden but simply has no content will not be affected by this algorthim,
// as technically they are not completely empty (for example a properly formed Cell will have at least
// an empty paragraph in it)
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Cell node is ended in the document.
*/
public int visitCellEnd(Cell cell) throws Exception
{
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Row node is ended in the document.
*/
public int visitRowEnd(Row row) throws Exception
{
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a SpecialCharacter is encountered in the document.
*/
public int visitSpecialChar(SpecialChar character) throws Exception
{
if (isHidden(character))
character.remove();
return VisitorAction.CONTINUE;
}
/**
* Returns true if the node passed is set as hidden, returns false if it is visible.
*/
private boolean isHidden(Node node) throws Exception
{
if (node instanceof Inline)
{
// If the node is Inline then cast it to retrieve the Font property which contains the hidden property
Inline currentNode = (Inline)node;
return currentNode.getFont().getHidden();
}
else if (node.getNodeType() == NodeType.PARAGRAPH)
{
// If the node is a paragraph cast it to retrieve the ParagraphBreakFont which contains the hidden property
Paragraph para = (Paragraph)node;
return para.getParagraphBreakFont().getHidden();
}
else if (node instanceof ShapeBase)
{
// Node is a shape or groupshape.
ShapeBase shape = (ShapeBase)node;
return shape.getFont().getHidden();
}
else if (node instanceof InlineStory)
{
// Node is a comment or footnote.
InlineStory inlineStory = (InlineStory)node;
return inlineStory.getFont().getHidden();
}
// A node that is passed to this method which does not contain a hidden property will end up here.
// By default nodes are not hidden so return false.
return false;
}
}
visitGlossaryDocumentEnd | |
public int visitGlossaryDocumentEnd(GlossaryDocument glossary)
throws java.lang.Exception |
-
Called when enumeration of a glossary document has ended.
Note: A glossary document node and its children are not visited when you execute a
Visitor over a Document. If you want to execute a Visitor over a
glossary document, you need to call GlossaryDocument.accept(com.aspose.words.DocumentVisitor).
- Parameters:
glossary
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitGlossaryDocumentStart | |
public int visitGlossaryDocumentStart(GlossaryDocument glossary)
throws java.lang.Exception |
-
Called when enumeration of a glossary document has started.
Note: A glossary document node and its children are not visited when you execute a
Visitor over a Document. If you want to execute a Visitor over a
glossary document, you need to call GlossaryDocument.accept(com.aspose.words.DocumentVisitor).
- Parameters:
glossary
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitGroupShapeEnd | |
public int visitGroupShapeEnd(GroupShape groupShape)
throws java.lang.Exception |
-
Called when enumeration of a group shape has ended.
- Parameters:
groupShape
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitGroupShapeStart | |
public int visitGroupShapeStart(GroupShape groupShape)
throws java.lang.Exception |
-
Called when enumeration of a group shape has started.
- Parameters:
groupShape
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Implements the Visitor Pattern to remove all content formatted as hidden from the document.
public void removeHiddenContentFromDocument() throws Exception
{
// Open the document we want to remove hidden content from.
Document doc = new Document(getMyDir() + "Font.Hidden.doc");
// Create an object that inherits from the DocumentVisitor class.
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
// We can run it over the entire the document like so:
doc.accept(hiddenContentRemover);
// Or we can run it on only a specific node.
Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// Or over a different type of node like below.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.accept(hiddenContentRemover);
doc.save(getMyDir() + "Font.Hidden Out.doc");
}
/**
* This class when executed will remove all hidden content from the Document. Implemented as a Visitor.
*/
private class RemoveHiddenContentVisitor extends DocumentVisitor
{
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// If this node is hidden, then remove it.
if (isHidden(fieldStart))
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
if (isHidden(fieldEnd))
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
if (isHidden(run))
run.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Paragraph node is encountered in the document.
*/
public int visitParagraphStart(Paragraph paragraph) throws Exception
{
if (isHidden(paragraph))
paragraph.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FormField is encountered in the document.
*/
public int visitFormField(FormField field) throws Exception
{
if (isHidden(field))
field.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a GroupShape is encountered in the document.
*/
public int visitGroupShapeStart(GroupShape groupShape) throws Exception
{
if (isHidden(groupShape))
groupShape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Shape is encountered in the document.
*/
public int visitShapeStart(Shape shape) throws Exception
{
if (isHidden(shape))
shape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Comment is encountered in the document.
*/
public int visitCommentStart(Comment comment) throws Exception
{
if (isHidden(comment))
comment.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Footnote is encountered in the document.
*/
public int visitFootnoteStart(Footnote footnote) throws Exception
{
if (isHidden(footnote))
footnote.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Table node is ended in the document.
*/
public int visitTableEnd(Table table) throws Exception
{
// At the moment there is no way to tell if a particular Table/Row/Cell is hidden.
// Instead, if the content of a table is hidden, then all inline child nodes of the table should be
// hidden and thus removed by previous visits as well. This will result in the container being empty
// so if this is the case we know to remove the table node.
//
// Note that a table which is not hidden but simply has no content will not be affected by this algorthim,
// as technically they are not completely empty (for example a properly formed Cell will have at least
// an empty paragraph in it)
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Cell node is ended in the document.
*/
public int visitCellEnd(Cell cell) throws Exception
{
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Row node is ended in the document.
*/
public int visitRowEnd(Row row) throws Exception
{
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a SpecialCharacter is encountered in the document.
*/
public int visitSpecialChar(SpecialChar character) throws Exception
{
if (isHidden(character))
character.remove();
return VisitorAction.CONTINUE;
}
/**
* Returns true if the node passed is set as hidden, returns false if it is visible.
*/
private boolean isHidden(Node node) throws Exception
{
if (node instanceof Inline)
{
// If the node is Inline then cast it to retrieve the Font property which contains the hidden property
Inline currentNode = (Inline)node;
return currentNode.getFont().getHidden();
}
else if (node.getNodeType() == NodeType.PARAGRAPH)
{
// If the node is a paragraph cast it to retrieve the ParagraphBreakFont which contains the hidden property
Paragraph para = (Paragraph)node;
return para.getParagraphBreakFont().getHidden();
}
else if (node instanceof ShapeBase)
{
// Node is a shape or groupshape.
ShapeBase shape = (ShapeBase)node;
return shape.getFont().getHidden();
}
else if (node instanceof InlineStory)
{
// Node is a comment or footnote.
InlineStory inlineStory = (InlineStory)node;
return inlineStory.getFont().getHidden();
}
// A node that is passed to this method which does not contain a hidden property will end up here.
// By default nodes are not hidden so return false.
return false;
}
}
visitHeaderFooterEnd | |
public int visitHeaderFooterEnd(HeaderFooter headerFooter)
throws java.lang.Exception |
-
Called when enumeration of a header or footer in a section has ended.
- Parameters:
headerFooter
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitHeaderFooterStart | |
public int visitHeaderFooterStart(HeaderFooter headerFooter)
throws java.lang.Exception |
-
Called when enumeration of a header or footer in a section has started.
- Parameters:
headerFooter
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.
public void toText() throws Exception
{
// Open the document we want to convert.
Document doc = new Document(getMyDir() + "Visitor.ToText.doc");
// Create an object that inherits from the DocumentVisitor class.
MyDocToTxtWriter myConverter = new MyDocToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
System.out.println(myConverter.getText());
}
/**
* Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
*/
public class MyDocToTxtWriter extends DocumentVisitor
{
public MyDocToTxtWriter() throws Exception
{
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/**
* Gets the plain text of the document that was accumulated by the visitor.
*/
public String getText() throws Exception
{
return mBuilder.toString();
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
appendText(run.getText());
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows
// after a field start character. We want to skip field codes and output field
// result only, therefore we use a flag to suspend the output while inside a field code.
//
// Note this is a very simplistic implementation and will not work very well
// if you have nested fields in a document.
mIsSkipText = true;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldSeparator node is encountered in the document.
*/
public int visitFieldSeparator(FieldSeparator fieldSeparator) throws Exception
{
// Once reached a field separator node, we enable the output because we are
// now entering the field result nodes.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
// Make sure we enable the output when reached a field end because some fields
// do not have field separator and do not have field result.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Paragraph node is ended in the document.
*/
public int visitParagraphEnd(Paragraph paragraph) throws Exception
{
// When outputting to plain text we output Cr+Lf characters.
appendText(ControlChar.CR_LF);
return VisitorAction.CONTINUE;
}
public int visitBodyStart(Body body) throws Exception
{
// We can detect beginning and end of all composite nodes such as Section, Body,
// Table, Paragraph etc and provide custom handling for them.
mBuilder.append("*** Body Started ***\r\n");
return VisitorAction.CONTINUE;
}
public int visitBodyEnd(Body body) throws Exception
{
mBuilder.append("*** Body Ended ***\r\n");
return VisitorAction.CONTINUE;
}
/**
* Called when a HeaderFooter node is encountered in the document.
*/
public int visitHeaderFooterStart(HeaderFooter headerFooter) throws Exception
{
// Returning this value from a visitor method causes visiting of this
// node to stop and move on to visiting the next sibling node.
// The net effect in this example is that the text of headers and footers
// is not included in the resulting output.
return VisitorAction.SKIP_THIS_NODE;
}
/**
* Adds text to the current output. Honours the enabled/disabled output flag.
*/
private void appendText(String text) throws Exception
{
if (!mIsSkipText)
mBuilder.append(text);
}
private final StringBuilder mBuilder;
private boolean mIsSkipText;
}
visitOfficeMathEnd | |
public int visitOfficeMathEnd(OfficeMath officeMath)
throws java.lang.Exception |
-
Called when enumeration of a Office Math object has ended.
- Parameters:
officeMath
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitOfficeMathStart | |
public int visitOfficeMathStart(OfficeMath officeMath)
throws java.lang.Exception |
-
Called when enumeration of a Office Math object has started.
- Parameters:
officeMath
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitParagraphEnd | |
public int visitParagraphEnd(Paragraph paragraph)
throws java.lang.Exception |
-
Called when enumeration of a paragraph has ended.
- Parameters:
paragraph
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.
public void toText() throws Exception
{
// Open the document we want to convert.
Document doc = new Document(getMyDir() + "Visitor.ToText.doc");
// Create an object that inherits from the DocumentVisitor class.
MyDocToTxtWriter myConverter = new MyDocToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
System.out.println(myConverter.getText());
}
/**
* Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
*/
public class MyDocToTxtWriter extends DocumentVisitor
{
public MyDocToTxtWriter() throws Exception
{
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/**
* Gets the plain text of the document that was accumulated by the visitor.
*/
public String getText() throws Exception
{
return mBuilder.toString();
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
appendText(run.getText());
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows
// after a field start character. We want to skip field codes and output field
// result only, therefore we use a flag to suspend the output while inside a field code.
//
// Note this is a very simplistic implementation and will not work very well
// if you have nested fields in a document.
mIsSkipText = true;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldSeparator node is encountered in the document.
*/
public int visitFieldSeparator(FieldSeparator fieldSeparator) throws Exception
{
// Once reached a field separator node, we enable the output because we are
// now entering the field result nodes.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
// Make sure we enable the output when reached a field end because some fields
// do not have field separator and do not have field result.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Paragraph node is ended in the document.
*/
public int visitParagraphEnd(Paragraph paragraph) throws Exception
{
// When outputting to plain text we output Cr+Lf characters.
appendText(ControlChar.CR_LF);
return VisitorAction.CONTINUE;
}
public int visitBodyStart(Body body) throws Exception
{
// We can detect beginning and end of all composite nodes such as Section, Body,
// Table, Paragraph etc and provide custom handling for them.
mBuilder.append("*** Body Started ***\r\n");
return VisitorAction.CONTINUE;
}
public int visitBodyEnd(Body body) throws Exception
{
mBuilder.append("*** Body Ended ***\r\n");
return VisitorAction.CONTINUE;
}
/**
* Called when a HeaderFooter node is encountered in the document.
*/
public int visitHeaderFooterStart(HeaderFooter headerFooter) throws Exception
{
// Returning this value from a visitor method causes visiting of this
// node to stop and move on to visiting the next sibling node.
// The net effect in this example is that the text of headers and footers
// is not included in the resulting output.
return VisitorAction.SKIP_THIS_NODE;
}
/**
* Adds text to the current output. Honours the enabled/disabled output flag.
*/
private void appendText(String text) throws Exception
{
if (!mIsSkipText)
mBuilder.append(text);
}
private final StringBuilder mBuilder;
private boolean mIsSkipText;
}
visitParagraphStart | |
public int visitParagraphStart(Paragraph paragraph)
throws java.lang.Exception |
-
Called when enumeration of a paragraph has started.
- Parameters:
paragraph
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Implements the Visitor Pattern to remove all content formatted as hidden from the document.
public void removeHiddenContentFromDocument() throws Exception
{
// Open the document we want to remove hidden content from.
Document doc = new Document(getMyDir() + "Font.Hidden.doc");
// Create an object that inherits from the DocumentVisitor class.
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
// We can run it over the entire the document like so:
doc.accept(hiddenContentRemover);
// Or we can run it on only a specific node.
Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// Or over a different type of node like below.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.accept(hiddenContentRemover);
doc.save(getMyDir() + "Font.Hidden Out.doc");
}
/**
* This class when executed will remove all hidden content from the Document. Implemented as a Visitor.
*/
private class RemoveHiddenContentVisitor extends DocumentVisitor
{
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// If this node is hidden, then remove it.
if (isHidden(fieldStart))
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
if (isHidden(fieldEnd))
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
if (isHidden(run))
run.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Paragraph node is encountered in the document.
*/
public int visitParagraphStart(Paragraph paragraph) throws Exception
{
if (isHidden(paragraph))
paragraph.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FormField is encountered in the document.
*/
public int visitFormField(FormField field) throws Exception
{
if (isHidden(field))
field.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a GroupShape is encountered in the document.
*/
public int visitGroupShapeStart(GroupShape groupShape) throws Exception
{
if (isHidden(groupShape))
groupShape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Shape is encountered in the document.
*/
public int visitShapeStart(Shape shape) throws Exception
{
if (isHidden(shape))
shape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Comment is encountered in the document.
*/
public int visitCommentStart(Comment comment) throws Exception
{
if (isHidden(comment))
comment.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Footnote is encountered in the document.
*/
public int visitFootnoteStart(Footnote footnote) throws Exception
{
if (isHidden(footnote))
footnote.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Table node is ended in the document.
*/
public int visitTableEnd(Table table) throws Exception
{
// At the moment there is no way to tell if a particular Table/Row/Cell is hidden.
// Instead, if the content of a table is hidden, then all inline child nodes of the table should be
// hidden and thus removed by previous visits as well. This will result in the container being empty
// so if this is the case we know to remove the table node.
//
// Note that a table which is not hidden but simply has no content will not be affected by this algorthim,
// as technically they are not completely empty (for example a properly formed Cell will have at least
// an empty paragraph in it)
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Cell node is ended in the document.
*/
public int visitCellEnd(Cell cell) throws Exception
{
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Row node is ended in the document.
*/
public int visitRowEnd(Row row) throws Exception
{
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a SpecialCharacter is encountered in the document.
*/
public int visitSpecialChar(SpecialChar character) throws Exception
{
if (isHidden(character))
character.remove();
return VisitorAction.CONTINUE;
}
/**
* Returns true if the node passed is set as hidden, returns false if it is visible.
*/
private boolean isHidden(Node node) throws Exception
{
if (node instanceof Inline)
{
// If the node is Inline then cast it to retrieve the Font property which contains the hidden property
Inline currentNode = (Inline)node;
return currentNode.getFont().getHidden();
}
else if (node.getNodeType() == NodeType.PARAGRAPH)
{
// If the node is a paragraph cast it to retrieve the ParagraphBreakFont which contains the hidden property
Paragraph para = (Paragraph)node;
return para.getParagraphBreakFont().getHidden();
}
else if (node instanceof ShapeBase)
{
// Node is a shape or groupshape.
ShapeBase shape = (ShapeBase)node;
return shape.getFont().getHidden();
}
else if (node instanceof InlineStory)
{
// Node is a comment or footnote.
InlineStory inlineStory = (InlineStory)node;
return inlineStory.getFont().getHidden();
}
// A node that is passed to this method which does not contain a hidden property will end up here.
// By default nodes are not hidden so return false.
return false;
}
}
visitRowEnd | |
public int visitRowEnd(Row row)
throws java.lang.Exception |
-
Called when enumeration of a table row has ended.
- Parameters:
row
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Implements the Visitor Pattern to remove all content formatted as hidden from the document.
public void removeHiddenContentFromDocument() throws Exception
{
// Open the document we want to remove hidden content from.
Document doc = new Document(getMyDir() + "Font.Hidden.doc");
// Create an object that inherits from the DocumentVisitor class.
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
// We can run it over the entire the document like so:
doc.accept(hiddenContentRemover);
// Or we can run it on only a specific node.
Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// Or over a different type of node like below.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.accept(hiddenContentRemover);
doc.save(getMyDir() + "Font.Hidden Out.doc");
}
/**
* This class when executed will remove all hidden content from the Document. Implemented as a Visitor.
*/
private class RemoveHiddenContentVisitor extends DocumentVisitor
{
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// If this node is hidden, then remove it.
if (isHidden(fieldStart))
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
if (isHidden(fieldEnd))
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
if (isHidden(run))
run.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Paragraph node is encountered in the document.
*/
public int visitParagraphStart(Paragraph paragraph) throws Exception
{
if (isHidden(paragraph))
paragraph.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FormField is encountered in the document.
*/
public int visitFormField(FormField field) throws Exception
{
if (isHidden(field))
field.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a GroupShape is encountered in the document.
*/
public int visitGroupShapeStart(GroupShape groupShape) throws Exception
{
if (isHidden(groupShape))
groupShape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Shape is encountered in the document.
*/
public int visitShapeStart(Shape shape) throws Exception
{
if (isHidden(shape))
shape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Comment is encountered in the document.
*/
public int visitCommentStart(Comment comment) throws Exception
{
if (isHidden(comment))
comment.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Footnote is encountered in the document.
*/
public int visitFootnoteStart(Footnote footnote) throws Exception
{
if (isHidden(footnote))
footnote.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Table node is ended in the document.
*/
public int visitTableEnd(Table table) throws Exception
{
// At the moment there is no way to tell if a particular Table/Row/Cell is hidden.
// Instead, if the content of a table is hidden, then all inline child nodes of the table should be
// hidden and thus removed by previous visits as well. This will result in the container being empty
// so if this is the case we know to remove the table node.
//
// Note that a table which is not hidden but simply has no content will not be affected by this algorthim,
// as technically they are not completely empty (for example a properly formed Cell will have at least
// an empty paragraph in it)
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Cell node is ended in the document.
*/
public int visitCellEnd(Cell cell) throws Exception
{
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Row node is ended in the document.
*/
public int visitRowEnd(Row row) throws Exception
{
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a SpecialCharacter is encountered in the document.
*/
public int visitSpecialChar(SpecialChar character) throws Exception
{
if (isHidden(character))
character.remove();
return VisitorAction.CONTINUE;
}
/**
* Returns true if the node passed is set as hidden, returns false if it is visible.
*/
private boolean isHidden(Node node) throws Exception
{
if (node instanceof Inline)
{
// If the node is Inline then cast it to retrieve the Font property which contains the hidden property
Inline currentNode = (Inline)node;
return currentNode.getFont().getHidden();
}
else if (node.getNodeType() == NodeType.PARAGRAPH)
{
// If the node is a paragraph cast it to retrieve the ParagraphBreakFont which contains the hidden property
Paragraph para = (Paragraph)node;
return para.getParagraphBreakFont().getHidden();
}
else if (node instanceof ShapeBase)
{
// Node is a shape or groupshape.
ShapeBase shape = (ShapeBase)node;
return shape.getFont().getHidden();
}
else if (node instanceof InlineStory)
{
// Node is a comment or footnote.
InlineStory inlineStory = (InlineStory)node;
return inlineStory.getFont().getHidden();
}
// A node that is passed to this method which does not contain a hidden property will end up here.
// By default nodes are not hidden so return false.
return false;
}
}
visitRowStart | |
public int visitRowStart(Row row)
throws java.lang.Exception |
-
Called when enumeration of a table row has started.
- Parameters:
row
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitRun | |
public int visitRun(Run run)
throws java.lang.Exception |
-
Called when a run of text in the is encountered.
- Parameters:
run
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.
public void toText() throws Exception
{
// Open the document we want to convert.
Document doc = new Document(getMyDir() + "Visitor.ToText.doc");
// Create an object that inherits from the DocumentVisitor class.
MyDocToTxtWriter myConverter = new MyDocToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
System.out.println(myConverter.getText());
}
/**
* Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
*/
public class MyDocToTxtWriter extends DocumentVisitor
{
public MyDocToTxtWriter() throws Exception
{
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/**
* Gets the plain text of the document that was accumulated by the visitor.
*/
public String getText() throws Exception
{
return mBuilder.toString();
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
appendText(run.getText());
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows
// after a field start character. We want to skip field codes and output field
// result only, therefore we use a flag to suspend the output while inside a field code.
//
// Note this is a very simplistic implementation and will not work very well
// if you have nested fields in a document.
mIsSkipText = true;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldSeparator node is encountered in the document.
*/
public int visitFieldSeparator(FieldSeparator fieldSeparator) throws Exception
{
// Once reached a field separator node, we enable the output because we are
// now entering the field result nodes.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
// Make sure we enable the output when reached a field end because some fields
// do not have field separator and do not have field result.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Paragraph node is ended in the document.
*/
public int visitParagraphEnd(Paragraph paragraph) throws Exception
{
// When outputting to plain text we output Cr+Lf characters.
appendText(ControlChar.CR_LF);
return VisitorAction.CONTINUE;
}
public int visitBodyStart(Body body) throws Exception
{
// We can detect beginning and end of all composite nodes such as Section, Body,
// Table, Paragraph etc and provide custom handling for them.
mBuilder.append("*** Body Started ***\r\n");
return VisitorAction.CONTINUE;
}
public int visitBodyEnd(Body body) throws Exception
{
mBuilder.append("*** Body Ended ***\r\n");
return VisitorAction.CONTINUE;
}
/**
* Called when a HeaderFooter node is encountered in the document.
*/
public int visitHeaderFooterStart(HeaderFooter headerFooter) throws Exception
{
// Returning this value from a visitor method causes visiting of this
// node to stop and move on to visiting the next sibling node.
// The net effect in this example is that the text of headers and footers
// is not included in the resulting output.
return VisitorAction.SKIP_THIS_NODE;
}
/**
* Adds text to the current output. Honours the enabled/disabled output flag.
*/
private void appendText(String text) throws Exception
{
if (!mIsSkipText)
mBuilder.append(text);
}
private final StringBuilder mBuilder;
private boolean mIsSkipText;
}
visitSectionEnd | |
public int visitSectionEnd(Section section)
throws java.lang.Exception |
-
Called when enumeration of a section has ended.
- Parameters:
section
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitSectionStart | |
public int visitSectionStart(Section section)
throws java.lang.Exception |
-
Called when enumeration of a section has started.
- Parameters:
section
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitShapeEnd | |
public int visitShapeEnd(Shape shape)
throws java.lang.Exception |
-
Called when enumeration of a shape has ended.
- Parameters:
shape
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitShapeStart | |
public int visitShapeStart(Shape shape)
throws java.lang.Exception |
-
Called when enumeration of a shape has started.
- Parameters:
shape
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Implements the Visitor Pattern to remove all content formatted as hidden from the document.
public void removeHiddenContentFromDocument() throws Exception
{
// Open the document we want to remove hidden content from.
Document doc = new Document(getMyDir() + "Font.Hidden.doc");
// Create an object that inherits from the DocumentVisitor class.
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
// We can run it over the entire the document like so:
doc.accept(hiddenContentRemover);
// Or we can run it on only a specific node.
Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// Or over a different type of node like below.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.accept(hiddenContentRemover);
doc.save(getMyDir() + "Font.Hidden Out.doc");
}
/**
* This class when executed will remove all hidden content from the Document. Implemented as a Visitor.
*/
private class RemoveHiddenContentVisitor extends DocumentVisitor
{
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// If this node is hidden, then remove it.
if (isHidden(fieldStart))
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
if (isHidden(fieldEnd))
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
if (isHidden(run))
run.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Paragraph node is encountered in the document.
*/
public int visitParagraphStart(Paragraph paragraph) throws Exception
{
if (isHidden(paragraph))
paragraph.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FormField is encountered in the document.
*/
public int visitFormField(FormField field) throws Exception
{
if (isHidden(field))
field.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a GroupShape is encountered in the document.
*/
public int visitGroupShapeStart(GroupShape groupShape) throws Exception
{
if (isHidden(groupShape))
groupShape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Shape is encountered in the document.
*/
public int visitShapeStart(Shape shape) throws Exception
{
if (isHidden(shape))
shape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Comment is encountered in the document.
*/
public int visitCommentStart(Comment comment) throws Exception
{
if (isHidden(comment))
comment.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Footnote is encountered in the document.
*/
public int visitFootnoteStart(Footnote footnote) throws Exception
{
if (isHidden(footnote))
footnote.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Table node is ended in the document.
*/
public int visitTableEnd(Table table) throws Exception
{
// At the moment there is no way to tell if a particular Table/Row/Cell is hidden.
// Instead, if the content of a table is hidden, then all inline child nodes of the table should be
// hidden and thus removed by previous visits as well. This will result in the container being empty
// so if this is the case we know to remove the table node.
//
// Note that a table which is not hidden but simply has no content will not be affected by this algorthim,
// as technically they are not completely empty (for example a properly formed Cell will have at least
// an empty paragraph in it)
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Cell node is ended in the document.
*/
public int visitCellEnd(Cell cell) throws Exception
{
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Row node is ended in the document.
*/
public int visitRowEnd(Row row) throws Exception
{
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a SpecialCharacter is encountered in the document.
*/
public int visitSpecialChar(SpecialChar character) throws Exception
{
if (isHidden(character))
character.remove();
return VisitorAction.CONTINUE;
}
/**
* Returns true if the node passed is set as hidden, returns false if it is visible.
*/
private boolean isHidden(Node node) throws Exception
{
if (node instanceof Inline)
{
// If the node is Inline then cast it to retrieve the Font property which contains the hidden property
Inline currentNode = (Inline)node;
return currentNode.getFont().getHidden();
}
else if (node.getNodeType() == NodeType.PARAGRAPH)
{
// If the node is a paragraph cast it to retrieve the ParagraphBreakFont which contains the hidden property
Paragraph para = (Paragraph)node;
return para.getParagraphBreakFont().getHidden();
}
else if (node instanceof ShapeBase)
{
// Node is a shape or groupshape.
ShapeBase shape = (ShapeBase)node;
return shape.getFont().getHidden();
}
else if (node instanceof InlineStory)
{
// Node is a comment or footnote.
InlineStory inlineStory = (InlineStory)node;
return inlineStory.getFont().getHidden();
}
// A node that is passed to this method which does not contain a hidden property will end up here.
// By default nodes are not hidden so return false.
return false;
}
}
visitSmartTagEnd | |
public int visitSmartTagEnd(SmartTag smartTag)
throws java.lang.Exception |
-
Called when enumeration of a smart tag has ended.
- Parameters:
smartTag
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitSmartTagStart | |
public int visitSmartTagStart(SmartTag smartTag)
throws java.lang.Exception |
-
Called when enumeration of a smart tag has started.
- Parameters:
smartTag
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitSpecialChar | |
public int visitSpecialChar(SpecialChar specialChar)
throws java.lang.Exception |
-
Called when a SpecialChar node is encountered in the document.
- Parameters:
specialChar
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitStructuredDocumentTagEnd | |
public int visitStructuredDocumentTagEnd(StructuredDocumentTag sdt)
throws java.lang.Exception |
-
Called when enumeration of a structured document tag has ended.
- Parameters:
sdt
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitStructuredDocumentTagStart | |
public int visitStructuredDocumentTagStart(StructuredDocumentTag sdt)
throws java.lang.Exception |
-
Called when enumeration of a structured document tag has started.
- Parameters:
sdt
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitSubDocument | |
public int visitSubDocument(SubDocument subDocument)
throws java.lang.Exception |
-
Called when a subDocument is encountered.
- Parameters:
subDocument
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitTableEnd | |
public int visitTableEnd(Table table)
throws java.lang.Exception |
-
Called when enumeration of a table has ended.
- Parameters:
table
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
Implements the Visitor Pattern to remove all content formatted as hidden from the document.
public void removeHiddenContentFromDocument() throws Exception
{
// Open the document we want to remove hidden content from.
Document doc = new Document(getMyDir() + "Font.Hidden.doc");
// Create an object that inherits from the DocumentVisitor class.
RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
// We can run it over the entire the document like so:
doc.accept(hiddenContentRemover);
// Or we can run it on only a specific node.
Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
para.accept(hiddenContentRemover);
// Or over a different type of node like below.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.accept(hiddenContentRemover);
doc.save(getMyDir() + "Font.Hidden Out.doc");
}
/**
* This class when executed will remove all hidden content from the Document. Implemented as a Visitor.
*/
private class RemoveHiddenContentVisitor extends DocumentVisitor
{
/**
* Called when a FieldStart node is encountered in the document.
*/
public int visitFieldStart(FieldStart fieldStart) throws Exception
{
// If this node is hidden, then remove it.
if (isHidden(fieldStart))
fieldStart.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FieldEnd node is encountered in the document.
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
if (isHidden(fieldEnd))
fieldEnd.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Run node is encountered in the document.
*/
public int visitRun(Run run) throws Exception
{
if (isHidden(run))
run.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Paragraph node is encountered in the document.
*/
public int visitParagraphStart(Paragraph paragraph) throws Exception
{
if (isHidden(paragraph))
paragraph.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a FormField is encountered in the document.
*/
public int visitFormField(FormField field) throws Exception
{
if (isHidden(field))
field.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a GroupShape is encountered in the document.
*/
public int visitGroupShapeStart(GroupShape groupShape) throws Exception
{
if (isHidden(groupShape))
groupShape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Shape is encountered in the document.
*/
public int visitShapeStart(Shape shape) throws Exception
{
if (isHidden(shape))
shape.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Comment is encountered in the document.
*/
public int visitCommentStart(Comment comment) throws Exception
{
if (isHidden(comment))
comment.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a Footnote is encountered in the document.
*/
public int visitFootnoteStart(Footnote footnote) throws Exception
{
if (isHidden(footnote))
footnote.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Table node is ended in the document.
*/
public int visitTableEnd(Table table) throws Exception
{
// At the moment there is no way to tell if a particular Table/Row/Cell is hidden.
// Instead, if the content of a table is hidden, then all inline child nodes of the table should be
// hidden and thus removed by previous visits as well. This will result in the container being empty
// so if this is the case we know to remove the table node.
//
// Note that a table which is not hidden but simply has no content will not be affected by this algorthim,
// as technically they are not completely empty (for example a properly formed Cell will have at least
// an empty paragraph in it)
if (!table.hasChildNodes())
table.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Cell node is ended in the document.
*/
public int visitCellEnd(Cell cell) throws Exception
{
if (!cell.hasChildNodes() && cell.getParentNode() != null)
cell.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when visiting of a Row node is ended in the document.
*/
public int visitRowEnd(Row row) throws Exception
{
if (!row.hasChildNodes() && row.getParentNode() != null)
row.remove();
return VisitorAction.CONTINUE;
}
/**
* Called when a SpecialCharacter is encountered in the document.
*/
public int visitSpecialChar(SpecialChar character) throws Exception
{
if (isHidden(character))
character.remove();
return VisitorAction.CONTINUE;
}
/**
* Returns true if the node passed is set as hidden, returns false if it is visible.
*/
private boolean isHidden(Node node) throws Exception
{
if (node instanceof Inline)
{
// If the node is Inline then cast it to retrieve the Font property which contains the hidden property
Inline currentNode = (Inline)node;
return currentNode.getFont().getHidden();
}
else if (node.getNodeType() == NodeType.PARAGRAPH)
{
// If the node is a paragraph cast it to retrieve the ParagraphBreakFont which contains the hidden property
Paragraph para = (Paragraph)node;
return para.getParagraphBreakFont().getHidden();
}
else if (node instanceof ShapeBase)
{
// Node is a shape or groupshape.
ShapeBase shape = (ShapeBase)node;
return shape.getFont().getHidden();
}
else if (node instanceof InlineStory)
{
// Node is a comment or footnote.
InlineStory inlineStory = (InlineStory)node;
return inlineStory.getFont().getHidden();
}
// A node that is passed to this method which does not contain a hidden property will end up here.
// By default nodes are not hidden so return false.
return false;
}
}
visitTableStart | |
public int visitTableStart(Table table)
throws java.lang.Exception |
-
Called when enumeration of a table has started.
- Parameters:
table
- The object that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
Example:
This class provides a static method convert fields of a particular type to static text.
private static class FieldsHelper extends DocumentVisitor
{
/**
* Converts any fields of the specified type found in the descendants of the node into static text.
*
* @param compositeNode The node in which all descendants of the specified FieldType will be converted to static text.
* @param targetFieldType The FieldType of the field to convert to static text.
*/
public static void convertFieldsToStaticText(CompositeNode compositeNode, int targetFieldType) throws Exception
{
FieldsHelper helper = new FieldsHelper(targetFieldType);
compositeNode.accept(helper);
}
private FieldsHelper(int targetFieldType)
{
mTargetFieldType = targetFieldType;
}
public int visitFieldStart(FieldStart fieldStart)
{
// We must keep track of the starts and ends of fields incase of any nested fields.
if (fieldStart.getFieldType() == mTargetFieldType)
{
mFieldDepth++;
fieldStart.remove();
}
else
{
// This removes the field start if it's inside a field that is being converted.
CheckDepthAndRemoveNode(fieldStart);
}
return VisitorAction.CONTINUE;
}
public int visitFieldSeparator(FieldSeparator fieldSeparator)
{
// When visiting a field separator we should decrease the depth level.
if (fieldSeparator.getFieldType() == mTargetFieldType)
{
mFieldDepth--;
fieldSeparator.remove();
}
else
{
// This removes the field separator if it's inside a field that is being converted.
CheckDepthAndRemoveNode(fieldSeparator);
}
return VisitorAction.CONTINUE;
}
public int visitFieldEnd(FieldEnd fieldEnd)
{
if (fieldEnd.getFieldType() == mTargetFieldType)
fieldEnd.remove();
else
CheckDepthAndRemoveNode(fieldEnd); // This removes the field end if it's inside a field that is being converted.
return VisitorAction.CONTINUE;
}
public int visitRun(Run run)
{
// Remove the run if it is between the FieldStart and FieldSeparator of the field being converted.
CheckDepthAndRemoveNode(run);
return VisitorAction.CONTINUE;
}
public int visitParagraphEnd(Paragraph paragraph)
{
if (mFieldDepth > 0)
{
// The field code that is being converted continues onto another paragraph. We
// need to copy the remaining content from this paragraph onto the next paragraph.
Node nextParagraph = paragraph.getNextSibling();
// Skip ahead to the next available paragraph.
while (nextParagraph != null && nextParagraph.getNodeType() != NodeType.PARAGRAPH)
nextParagraph = nextParagraph.getNextSibling();
// Copy all of the nodes over. Keep a list of these nodes so we know not to remove them.
while (paragraph.hasChildNodes())
{
mNodesToSkip.add(paragraph.getLastChild());
((Paragraph)nextParagraph).prependChild(paragraph.getLastChild());
}
paragraph.remove();
}
return VisitorAction.CONTINUE;
}
public int visitTableStart(Table table)
{
CheckDepthAndRemoveNode(table);
return VisitorAction.CONTINUE;
}
/**
* Checks whether the node is inside a field or should be skipped and then removes it if necessary.
*/
private void CheckDepthAndRemoveNode(Node node)
{
if (mFieldDepth > 0 && !mNodesToSkip.contains(node))
node.remove();
}
private int mFieldDepth = 0;
private ArrayList mNodesToSkip = new ArrayList();
private int mTargetFieldType;
}
See Also:
Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
Aspose.Words Support Forum - our preferred method of support.