Node.js and .NET Version Differences
Aspose.Words for Node.js via .NET is a native Node.js module based on Aspose.Words for .NET with Node-API, that is why the two products have almost the same set of features. Nevertheless, there are some nuances of work and differences in features and API, which are described on this page.
Feature Differences
Aspose.Words for Node.js has some differences as compared to its equivalent .NET version of the API. This section contains information about all such functionality that is not available in the current release. The missing features will be added in the future releases.
- Implementation of interfaces is not supported yet, that is why it is not possible to use callbacks such as IWarningCallback, IReplacingCallback, IFieldUpdatingCallback, IFieldMergingCallback etc.
- It does not support Printing features.
- DocumentVisitor implementation is currently not possible from the Node.js code.
- Reporting features are very .NET specific and aren’t not supported in Node.js.
- MailMerge functionality is not implemented.
- Low Code API is not implemented yet.
- Reading of PDF documents is not implemented.
- All .NET-specific APIs like OleDB and ADO are not supported.
- The first release only supports Microsoft Windows┬о x64 platform and Node.js 14.17.0 or higher.
Casting Aspose.Words Objects in Node.js
Though type casting is not natural for Node.js developers some tasks cannot be accomplished without casting documents nodes or fields to concrete type. Aspose.Words for Node.js via .NET provides special methods that allow casting objects where this is necessary.
Casting Nodes
Base class for all document nodes in Aspose.Words DOM is Node class. For example getChild method returns and instance of Node class, but if you need to modify the returned node, in most cases you should to cast it to concrete type. The following code demonstrates how to change font color of the first Run in the document:
let doc = new aw.Document("Document.docx");
// Get the first Run node and cast it to Run object.
let run = doc.getChild(aw.NodeType.Run, 0, true).asRun();
// Make changes to the run
run.font.color = "red";
// Save the result
doc.save("WorkingWithNode.change_run_color.docx");
Casting might be also required when clone method is used:
let doc = new aw.Document("Document.docx");
let clone = doc.clone().asDocument();
clone.save("CloneAndCombineDocuments.cloning_document.docx");
As you might noticed where in C# code you would use (Paragraph)node
for casting, in Node.js you have to use node.asParagraph()
method. In the Node.js version of Aspose.Words Node class introduces the following link of asXxx
methods:
- asDocument()
- asSection()
- asBody()
- asHeaderFooter()
- asTable()
- asRow()
- asCell()
- asParagraph()
- asBookmarkStart()
- asBookmarkEnd()
- asEditableRangeStart()
- asEditableRangeEnd()
- asGroupShape()
- asShape()
- asComment()
- asFootnote()
- asRun()
- asFieldStart()
- asFieldSeparator()
- asFieldEnd()
- asFormField()
- asSpecialChar()
- asSmartTag()
- asStructuredDocumentTag()
- asStructuredDocumentTagRangeStart()
- asStructuredDocumentTagRangeEnd()
- asGlossaryDocument()
- asBuildingBlock()
- asCommentRangeStart()
- asCommentRangeEnd()
- asOfficeMath()
- asSubDocument()
- asCompositeNode()
The asXxx
method raise a RuntimeError
with the message like the following if the node cannot be cased to the specified type:
RuntimeError: Unable to cast object of type ‘Aspose.Words.XXX’ to type ‘Aspose.Words.Drawing.YYY’.
Casting Fields
The same situation applied to fields. The following code example demonstrates how to replace hyper link links:
let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);
builder.write("This document was written by ");
let fieldAuthor = builder.insertField(aw.Fields.FieldType.FieldAuthor, true).asFieldAuthor();
fieldAuthor.authorName = "John Doe";
As you might noticed Field object also provides set of asXxx
methods, which are listed below:
- asFieldUnknown()
- asFieldMergeBarcode()
- asFieldDisplayBarcode()
- asFieldPrint()
- asFieldPrivate()
- asFieldAdvance()
- asFieldFormCheckBox()
- asFieldFormDropDown()
- asFieldIndex()
- asFieldRd()
- asFieldTa()
- asFieldToa()
- asFieldAsk()
- asFieldAutoText()
- asFieldAutoTextList()
- asFieldBibliography()
- asFieldCitation()
- asFieldDde()
- asFieldDdeAuto()
- asFieldFillIn()
- asFieldGlossary()
- asFieldImport()
- asFieldInclude()
- asFieldShape()
- asFieldDatabase()
- asFieldSkipIf()
- asFieldListNum()
- asFieldRevNum()
- asFieldSection()
- asFieldSectionPages()
- asFieldData()
- asFieldEmbed()
- asFieldOcx()
- asFieldAutoNum()
- asFieldAutoNumLgl()
- asFieldAutoNumOut()
- asFieldAddIn()
- asFieldBarcode()
- asFieldBidiOutline()
- asFieldEq()
- asFieldFootnoteRef()
- asFieldInfo()
- asFieldUserAddress()
- asFieldUserInitials()
- asFieldUserName()
- asFieldIncludePicture()
- asFieldPage()
- asFieldCreateDate()
- asFieldEditTime()
- asFieldPrintDate()
- asFieldSaveDate()
- asFieldGoToButton()
- asFieldAuthor()
- asFieldComments()
- asFieldFileName()
- asFieldFileSize()
- asFieldKeywords()
- asFieldLastSavedBy()
- asFieldNumChars()
- asFieldNumPages()
- asFieldNumWords()
- asFieldSubject()
- asFieldTemplate()
- asFieldTitle()
- asFieldFormula()
- asFieldSymbol()
- asFieldQuote()
- asFieldSet()
- asFieldAddressBlock()
- asFieldCompare()
- asFieldDate()
- asFieldDocProperty()
- asFieldDocVariable()
- asFieldGreetingLine()
- asFieldHyperlink()
- asFieldIf()
- asFieldIncludeText()
- asFieldLink()
- asFieldMacroButton()
- asFieldMergeField()
- asFieldMergeRec()
- asFieldMergeSeq()
- asFieldNext()
- asFieldNextIf()
- asFieldNoteRef()
- asFieldPageRef()
- asFieldRef()
- asFieldSeq()
- asFieldStyleRef()
- asFieldTc()
- asFieldTime()
- asFieldToc()
- asFieldXe()
- asFieldFormText()
Casting Styles
Casting is also required to work with table styles:
let doc = aw.Document();
let builder = aw.DocumentBuilder(doc);
let table = builder.startTable();
builder.insertCell();
builder.write("Name");
builder.insertCell();
builder.write("Value");
builder.endRow();
builder.insertCell();
builder.insertCell();
builder.endTable();
// Add a table style and modify it's properties.
let tableStyle = doc.styles.add(aw.StyleType.Table, "MyTableStyle1").asTableStyle();
tableStyle.borders.lineStyle = aw.LineStyle.Double;
tableStyle.borders.lineWidth = 1;
tableStyle.leftPadding = 18;
tableStyle.rightPadding = 18;
tableStyle.topPadding = 12;
tableStyle.bottomPadding = 12;
table.style = tableStyle;
doc.save("WorkingWithTableStylesAndFormatting.create_table_style.docx");
Casting Ole Controls
The following code example demonstrates how to read ActiveX control properties:
let doc = new aw.Document("ActiveX controls.docx");
let shape = doc.getShape(0, true);
let oleControl = shape.oleFormat.oleControl;
expect(oleControl.name).toEqual("CheckBox1");
if (oleControl.isForms2OleControl) {
console.log(oleControl);
let checkBox = oleControl.asForms2OleControl();
console.log(checkBox.caption);
console.log(checkBox.groupName);
}
Casting Font Sources
FontSourceBase class provides set of asXxx
methods, which are listed below:
- asFileFontSource()
- asFolderFontSource()
- asMemoryFontSource()
- asStreamFontSource()
- asSystemFontSource()
Accessing Indexer Properties
Aspose.Words for Node.js does not support .NET indexers. To fill this gap the following classes have an additional at() and setAt() methods.
C# code:
var doc = new Document("Bookmarks.docx");
var bookmarks = doc.Range.Bookmarks;
// Bookmarks can be accessed in the bookmark collection by index or name, and their names can be updated.
bookmarks[0].Name = "New name";
bookmarks["MyBookmark_2"].Text = $"Updated text contents of {bookmarks[1].Name}";
Node.js code:
let doc = new aw.Document("Bookmarks.docx");
let bookmarks = doc.range.bookmarks;
// Bookmarks can be accessed in the bookmark collection by index or name, and their names can be updated.
bookmarks.at(0).name = "New name";
bookmarks.at("MyBookmark_2").text = `Updated text contents of ${bookmarks.at(1).name}`;
C# code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
ToaCategories toaCategories = new ToaCategories();
toaCategories[1] = "My Category 1";
doc.FieldOptions.ToaCategories = toaCategories;
Node.js code:
let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);
let toaCategories = new aw.Fields.ToaCategories();
toaCategories.setAt(1, "My Category 1");
doc.fieldOptions.toaCategories = toaCategories;
API Members Naming
To be closer to Node.js world, API members of Aspose.Words for Node.js via .NET uses camelCase for identifier names (variables and functions).