java.lang.Objectcom.aspose.words.NodeChangingArgs
public class NodeChangingArgs
Example:
public void fontChangeViaCallback() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set up and pass the object which implements the handler methods
doc.setNodeChangingCallback(new HandleNodeChangingFontChanger());
// Insert sample HTML content
builder.insertHtml("<p>Hello World</p>");
doc.save(getArtifactsDir() + "Document.FontChangeViaCallback.docx");
}
public class HandleNodeChangingFontChanger implements INodeChangingCallback {
// Implement the NodeInserted handler to set default font settings for every Run node inserted into the Document
public void nodeInserted(final NodeChangingArgs args) {
// Change the font of inserted text contained in the Run nodes
if (args.getNode().getNodeType() == NodeType.RUN) {
Font font = ((Run) args.getNode()).getFont();
font.setSize(24);
font.setName("Arial");
}
}
public void nodeInserting(final NodeChangingArgs args) {
// Do Nothing
}
public void nodeRemoved(final NodeChangingArgs args) {
// Do Nothing
}
public void nodeRemoving(final NodeChangingArgs args) {
// Do Nothing
}
}
Property Getters/Setters Summary | ||
---|---|---|
int | getAction() | |
Gets a value indicating what type of node change event is occurring. The value of the property is NodeChangingAction integer constant. | ||
Node | getNewParent() | |
Gets the node's parent that will be set after the operation completes. | ||
Node | getNode() | |
Gets the |
||
Node | getOldParent() | |
Gets the node's parent before the operation began. |
Property Getters/Setters Detail |
---|
getAction | |
public int getAction() |
Example:
Shows how to use a NodeChangingCallback to monitor changes to the document tree as it is edited.public void nodeChangingCallback() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the NodeChangingCallback attribute to a custom printer doc.setNodeChangingCallback(new NodeChangingPrinter()); // All node additions and removals will be printed to the console as we edit the document builder.writeln("Hello world!"); builder.startTable(); builder.insertCell(); builder.write("Cell 1"); builder.insertCell(); builder.write("Cell 2"); builder.endTable(); builder.insertImage(getImageDir() + "Logo.jpg"); builder.getCurrentParagraph().getParentNode().removeAllChildren(); } /// <summary> /// Prints all inserted/removed nodes as well as their parent nodes. /// </summary> private static class NodeChangingPrinter implements INodeChangingCallback { public void nodeInserting(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT); Assert.assertEquals(args.getOldParent(), null); } public void nodeInserted(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT); Assert.assertNotNull(args.getNewParent()); System.out.println("Inserted node:"); System.out.println(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType())); if (!"".equals(args.getNode().getText().trim())) { System.out.println(MessageFormat.format("\tText:\t\"{0}\"", args.getNode().getText().trim())); } System.out.println(MessageFormat.format("\tHash:\t{0}", args.getNode().hashCode())); System.out.println(MessageFormat.format("\tParent:\t{0} ({1})", args.getNewParent().getNodeType(), args.getNewParent().hashCode())); } public void nodeRemoving(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE); } public void nodeRemoved(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE); Assert.assertNull(args.getNewParent()); System.out.println(MessageFormat.format("Removed node: {0} ({1})", args.getNode().getNodeType(), args.getNode().hashCode())); } }
getNewParent | |
public Node getNewParent() |
Example:
Shows how to use a NodeChangingCallback to monitor changes to the document tree as it is edited.public void nodeChangingCallback() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the NodeChangingCallback attribute to a custom printer doc.setNodeChangingCallback(new NodeChangingPrinter()); // All node additions and removals will be printed to the console as we edit the document builder.writeln("Hello world!"); builder.startTable(); builder.insertCell(); builder.write("Cell 1"); builder.insertCell(); builder.write("Cell 2"); builder.endTable(); builder.insertImage(getImageDir() + "Logo.jpg"); builder.getCurrentParagraph().getParentNode().removeAllChildren(); } /// <summary> /// Prints all inserted/removed nodes as well as their parent nodes. /// </summary> private static class NodeChangingPrinter implements INodeChangingCallback { public void nodeInserting(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT); Assert.assertEquals(args.getOldParent(), null); } public void nodeInserted(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT); Assert.assertNotNull(args.getNewParent()); System.out.println("Inserted node:"); System.out.println(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType())); if (!"".equals(args.getNode().getText().trim())) { System.out.println(MessageFormat.format("\tText:\t\"{0}\"", args.getNode().getText().trim())); } System.out.println(MessageFormat.format("\tHash:\t{0}", args.getNode().hashCode())); System.out.println(MessageFormat.format("\tParent:\t{0} ({1})", args.getNewParent().getNodeType(), args.getNewParent().hashCode())); } public void nodeRemoving(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE); } public void nodeRemoved(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE); Assert.assertNull(args.getNewParent()); System.out.println(MessageFormat.format("Removed node: {0} ({1})", args.getNode().getNodeType(), args.getNode().hashCode())); } }
getNode | |
public Node getNode() |
Example:
Shows how to implement custom logic over node insertion in the document by changing the font of inserted HTML content.public void fontChangeViaCallback() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set up and pass the object which implements the handler methods doc.setNodeChangingCallback(new HandleNodeChangingFontChanger()); // Insert sample HTML content builder.insertHtml("<p>Hello World</p>"); doc.save(getArtifactsDir() + "Document.FontChangeViaCallback.docx"); } public class HandleNodeChangingFontChanger implements INodeChangingCallback { // Implement the NodeInserted handler to set default font settings for every Run node inserted into the Document public void nodeInserted(final NodeChangingArgs args) { // Change the font of inserted text contained in the Run nodes if (args.getNode().getNodeType() == NodeType.RUN) { Font font = ((Run) args.getNode()).getFont(); font.setSize(24); font.setName("Arial"); } } public void nodeInserting(final NodeChangingArgs args) { // Do Nothing } public void nodeRemoved(final NodeChangingArgs args) { // Do Nothing } public void nodeRemoving(final NodeChangingArgs args) { // Do Nothing } }
getOldParent | |
public Node getOldParent() |
Example:
Shows how to use a NodeChangingCallback to monitor changes to the document tree as it is edited.public void nodeChangingCallback() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the NodeChangingCallback attribute to a custom printer doc.setNodeChangingCallback(new NodeChangingPrinter()); // All node additions and removals will be printed to the console as we edit the document builder.writeln("Hello world!"); builder.startTable(); builder.insertCell(); builder.write("Cell 1"); builder.insertCell(); builder.write("Cell 2"); builder.endTable(); builder.insertImage(getImageDir() + "Logo.jpg"); builder.getCurrentParagraph().getParentNode().removeAllChildren(); } /// <summary> /// Prints all inserted/removed nodes as well as their parent nodes. /// </summary> private static class NodeChangingPrinter implements INodeChangingCallback { public void nodeInserting(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT); Assert.assertEquals(args.getOldParent(), null); } public void nodeInserted(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT); Assert.assertNotNull(args.getNewParent()); System.out.println("Inserted node:"); System.out.println(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType())); if (!"".equals(args.getNode().getText().trim())) { System.out.println(MessageFormat.format("\tText:\t\"{0}\"", args.getNode().getText().trim())); } System.out.println(MessageFormat.format("\tHash:\t{0}", args.getNode().hashCode())); System.out.println(MessageFormat.format("\tParent:\t{0} ({1})", args.getNewParent().getNodeType(), args.getNewParent().hashCode())); } public void nodeRemoving(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE); } public void nodeRemoved(NodeChangingArgs args) { Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE); Assert.assertNull(args.getNewParent()); System.out.println(MessageFormat.format("Removed node: {0} ({1})", args.getNode().getNodeType(), args.getNode().hashCode())); } }