public class FieldNumWords extends Field
To learn more, visit the Working with Fields documentation article.
Remarks:
Retrieves the number of words in the current document, as recorded in the Words property of the built-in document properties.
Examples:
Shows how to use NUMCHARS, NUMWORDS, NUMPAGES and PAGE fields to track the size of our documents.
Document doc = new Document(getMyDir() + "Paragraphs.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// Below are three types of fields that we can use to track the size of our documents.
// 1 - Track the character count with a NUMCHARS field:
FieldNumChars fieldNumChars = (FieldNumChars) builder.insertField(FieldType.FIELD_NUM_CHARS, true);
builder.writeln(" characters");
// 2 - Track the word count with a NUMWORDS field:
FieldNumWords fieldNumWords = (FieldNumWords) builder.insertField(FieldType.FIELD_NUM_WORDS, true);
builder.writeln(" words");
// 3 - Use both PAGE and NUMPAGES fields to display what page the field is on,
// and the total number of pages in the document:
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.write("Page ");
FieldPage fieldPage = (FieldPage) builder.insertField(FieldType.FIELD_PAGE, true);
builder.write(" of ");
FieldNumPages fieldNumPages = (FieldNumPages) builder.insertField(FieldType.FIELD_NUM_PAGES, true);
Assert.assertEquals(fieldNumChars.getFieldCode(), " NUMCHARS ");
Assert.assertEquals(fieldNumWords.getFieldCode(), " NUMWORDS ");
Assert.assertEquals(fieldNumPages.getFieldCode(), " NUMPAGES ");
Assert.assertEquals(fieldPage.getFieldCode(), " PAGE ");
// These fields will not maintain accurate values in real time
// while we edit the document programmatically using Aspose.Words, or in Microsoft Word.
// We need to update them every we need to see an up-to-date value.
doc.updateFields();
doc.save(getArtifactsDir() + "Field.NUMCHARS.NUMWORDS.NUMPAGES.PAGE.docx");
getDisplayResult, getEnd, getFieldCode, getFieldCode, getFormat, getLocaleId, getResult, getSeparator, getStart, getType, isDirty, isDirty, isLocked, isLocked, needStoreOldResultNodes, remove, setLocaleId, setResult, unlink, update, update