Hello Team,
I am using Aspose.Words for Node.js and I need complete documentation + practical examples related to the following areas:
1. Word Range Operations
I am looking for full details and examples for:
Accessing Range
range.text
range.replace()
range.delete()
- Getting range of a node/paragraph/section
Formatting Through Range
- Applying font formatting to a range
- Applying paragraph formatting
- Updating styles through a range
- Clearing/removing formatting inside a range
Range-Based Manipulation
- Extracting partial content using ranges
- Inserting text before/after a given range
- Copying/moving ranges between documents
- Ranges inside tables, shapes, headers, footers
If available, please share all capabilities or complete API documentation for the Range class.
@AbhayKumar007
Aspose.Words for Node.js – Range class
The complete API reference for the Range class is published in the official Aspose.Words for Node.js documentation:
Below is a compact “cook‑book” that covers the most frequently used members you asked about. The snippets assume you have already created a Document object (doc) and loaded a DOCX file.
1. Accessing a Range
| Operation |
Code (Node.js) |
Remarks |
| Get the whole document text |
let txt = doc.getRange().getText(); |
getRange() returns the root Range. |
| Replace text |
doc.getRange().replace("old", "new", { isMatchCase: false, isWholeWord: false }); |
Options are optional; they map to the .NET overloads. |
| Delete a range |
doc.getRange().delete(); |
Removes the entire document content. |
| Range of a specific node (paragraph, section, etc.) |
js\nlet para = doc.getSections().get(0).getBody().getParagraphs().get(2);\nlet paraRange = para.getRange();\n |
getRange() is available on any Node. |
| Range of a section |
js\nlet sec = doc.getSections().get(1);\nlet secRange = sec.getRange();\n |
|
2. Formatting through a Range
| Goal |
Code |
Explanation |
| Apply font formatting |
js\nlet rng = doc.getRange();\nlet font = rng.getFont();\nfont.setName("Arial");\nfont.setSize(12);\nfont.setBold(true);\n |
getFont() works on a Range that contains text. |
| Apply paragraph formatting |
js\nlet paraRng = doc.getSections().get(0).getBody().getParagraphs().get(0).getRange();\nlet paraFmt = paraRng.getParagraphFormat();\nparaFmt.setAlignment(asposewordscloud.ParagraphAlignment.CENTER);\nparaFmt.setSpaceAfter(12);\n |
|
| Update a style |
js\nlet style = doc.getStyles().getByName("Heading 1");\nstyle.getFont().setColor(asposewordscloud.Color.fromArgb(255,0,0));\nstyle.getParagraphFormat().setSpacingAfter(6);\n |
Changing the style automatically affects all ranges that use it. |
| Clear formatting |
js\ndoc.getRange().clearFormatting();\n |
Removes character and paragraph formatting, but keeps the text. |
3. Range‑based manipulation
| Operation |
Code |
Notes |
| Extract a part of the document |
js\nlet startNode = doc.getSections().get(0).getBody().getParagraphs().get(2);\nlet endNode = doc.getSections().get(0).getBody().getParagraphs().get(5);\nlet subRange = doc.getRange().getDocument().getRange(startNode, endNode);\nlet extracted = subRange.getText();\n |
getRange(startNode, endNode) creates a new Range bounded by the two nodes. |
| Insert text before a range |
js\nlet target = doc.getSections().get(0).getBody().getParagraphs().get(3).getRange();\ntarget.insertBefore("Inserted before ");\n |
|
| Insert text after a range |
js\ntarget.insertAfter(" inserted after.");\n |
|
| Copy a range to another document |
js\nlet srcRange = doc.getRange();\nlet dstDoc = new asposewordscloud.Document();\nlet dstRange = dstDoc.getRange();\nsrcRange.copy(dstRange);\n |
copy clones the content (including formatting). |
| Move a range to another document |
js\nsrcRange.move(dstRange);\n |
After move, the source range becomes empty. |
| Range inside a table cell |
js\nlet cell = doc.getSections().get(0).getBody().getTables().get(0).getRows().get(1).getCells().get(0);\nlet cellRange = cell.getRange();\ncellRange.replace("old", "new");\n |
|
| Range inside a shape |
js\nlet shape = doc.getSections().get(0).getBody().getShapes().get(0);\nlet shapeRange = shape.getTextFrame().getTextRange();\nshapeRange.setText("New shape text");\n |
|
| Range inside header/footer |
js\nlet header = doc.getSections().get(0).getHeadersFooters().getByHeaderFooterType(asposewordscloud.HeaderFooterType.HEADER_PRIMARY);\nlet hdrRange = header.getRange();\nhdrRange.replace("Company", "Acme Corp");\n |
|
4. Quick checklist for “all capabilities”
| Category |
Member (Node.js) |
Typical use |
| Text access |
getText(), setText() |
Read/write raw text. |
| Search/replace |
replace(old, new, options) |
Regex‑enabled replace. |
| Deletion |
delete() |
Remove the whole range. |
| Formatting |
getFont(), getParagraphFormat(), clearFormatting() |
Font/paragraph styling. |
| Style handling |
getDocument().getStyles(), style.apply() |
Work with named styles. |
| Node navigation |
getStartNode(), getEndNode(), getDocument() |
Determine boundaries. |
| Copy/Move |
copy(targetRange), move(targetRange) |
Transfer content between docs. |
| Insertion |
insertBefore(text), insertAfter(text), insertDocument(doc) |
Add new content. |
| Cloning |
clone() |
Duplicate a range object. |
| Saving |
getDocument().save(pathOrStream, saveOptions) |
Persist changes. |
Tip: Most Range members are also exposed on Node objects (Paragraph, Section, Cell, Shape, HeaderFooter, etc.) via the getRange() method, so you can work at any granularity without first converting to a Document.
What to do next
- Confirm your SDK version – the API surface is stable from v22.9 onward, but a few method signatures (e.g.,
replace) were refined in v23.x.
- If you need a specific scenario (e.g., moving a range that contains a table across documents while preserving layout), share a minimal reproducible code snippet and the exact version you are using; we can then point to the precise overload.
Feel free to post the snippet here, and we’ll fine‑tune the example for your case.
Which product do you use? Aspose.Words or Aspose Words Cloud?
Okay, then I move all your topics to appropriate forum.