Remove Content
Node.Remove() deletes the element a node wraps from the document it came from.
Combined with the finders it gives you a one-pass filter: walk the document, test
each element, delete the ones you do not want, save.
There are four ways to take something out, and which you use depends on what you are holding when you decide:
| Call | Removes | Use when |
|---|---|---|
node.Remove() | the paragraph or table behind a node | you found the element with a node finder |
doc.RemoveParagraph(p) | one paragraph, searching the body, headers and footers | you already have the Paragraph |
doc.RemoveTable(t) | one table, including tables nested in cells | you already have the Table |
p.RemoveRun(r) | one run inside a paragraph | you are editing text within a paragraph |
Removing every table
nodes := doc.Nodes()
for _, node := range nodes.X() {
switch node.X().(type) {
case *document.Table:
node.Remove()
}
}
if err := doc.SaveToFile("output.docx"); err != nil {
panic(err)
}nodes.X() returns the slice built when doc.Nodes() was called, not a live view
of the document, so deleting during the loop is safe and no index bookkeeping is
needed. Change the case to *document.Paragraph to strip paragraphs instead; the
example ships that variant commented out.
This loop only inspects the top level of the document. That is enough for tables: a table nested inside another table’s cell goes away when its container does. On the example’s sample document, three top-level table nodes account for all four tables it contains, and the paragraph count drops from 43 to 6 because the cell paragraphs leave with their tables.
Limitations
Remove() handles paragraphs and tables. Anything else, including the run nodes
you get from FindNodeByCondition with wholeElements set to true and the
section-properties node at the end of doc.Nodes(), writes a message to the
debug log and returns. No error, no panic, no change to the document. To delete a
run, use Paragraph.RemoveRun instead.
Node.Clear() sounds like it should empty an element, and does not. It sets the
node’s own reference to nil and leaves the document untouched, so a paragraph
you called Clear() on is still in the saved file with its text intact. Nodes are
usually held by value out of a []Node, which makes the call affect only a copy
anyway. Use Remove() to delete and ReplaceText to blank text out.
Removal reaches the document body. Headers and footers are not part of
doc.Nodes(), though doc.RemoveParagraph does search them if you pass it a
paragraph you obtained another way.
Deleting every paragraph from a document leaves a file Word will open but that has no body content, which is not the same as a blank document. Add a paragraph back if the result is meant to be edited.
Run the example
The example opens sample.docx, removes its tables and writes output.docx
beside it.
git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/document/node-remove
go run main.goIf this is your first time using UniOffice, follow the getting started guide to create an API key and set up your development environment.