Word.Page class
Represents a page in the document. Page objects manage the page layout and content.
- Extends
Remarks
Used by
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Reads the first page's paper dimensions in points and its 1-based index.
await Word.run(async (context) => {
const pages: Word.PageCollection = context.document.activeWindow.activePane.pages;
const firstPage: Word.Page = pages.getFirst();
firstPage.load("index,height,width");
await context.sync();
console.log("First page properties:", {
index: firstPage.index,
heightInPoints: firstPage.height,
widthInPoints: firstPage.width
});
});
Properties
| breaks | Gets a |
| context | The request context associated with the object. This connects the add-in's process to the Office host application's process. |
| height | Gets the height, in points, of the paper defined in the Page Setup dialog box. |
| index | Gets the index of the page. The page index is 1-based and independent of the user's custom page numbering. |
| width | Gets the width, in points, of the paper defined in the Page Setup dialog box. |
Methods
| get |
Gets the next page in the pane. Throws an |
| get |
Gets the next page. If this page is the last one, then this method will return an object with its |
| get |
Gets the whole page, or the starting or ending point of the page, as a range. |
| load(options) | Queues up a command to load the specified properties of the object. You must call |
| load(property |
Queues up a command to load the specified properties of the object. You must call |
| load(property |
Queues up a command to load the specified properties of the object. You must call |
| toJSON() | Overrides the JavaScript |
| track() | Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across |
| untrack() | Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call |
Property Details
breaks
Gets a BreakCollection object that represents the breaks on the page.
readonly breaks: Word.BreakCollection;
Property Value
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Gets the page number of every break in the active document pane.
await Word.run(async (context) => {
const pages: Word.PageCollection = context.document.activeWindow.activePane.pages;
pages.load("items");
await context.sync();
const breakCollections: Word.BreakCollection[] = [];
pages.items.forEach((page: Word.Page) => {
const breaks: Word.BreakCollection = page.breaks;
breaks.load("items/pageIndex");
breakCollections.push(breaks);
});
await context.sync();
const documentBreaks: Word.Break[] = breakCollections.flatMap((breaks) => breaks.items);
console.log(`Breaks found: ${documentBreaks.length}`);
documentBreaks.forEach((documentBreak: Word.Break) => {
console.log(`Break on page ${documentBreak.pageIndex}.`);
});
});
context
The request context associated with the object. This connects the add-in's process to the Office host application's process.
context: RequestContext;
Property Value
height
Gets the height, in points, of the paper defined in the Page Setup dialog box.
readonly height: number;
Property Value
number
Remarks
index
Gets the index of the page. The page index is 1-based and independent of the user's custom page numbering.
readonly index: number;
Property Value
number
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Reads the page indexes for the third paragraph.
await Word.run(async (context) => {
const paragraphs: Word.ParagraphCollection = context.document.body.paragraphs;
paragraphs.load("items");
await context.sync();
if (paragraphs.items.length < 3) {
console.warn("Run Set up to create at least three paragraphs.");
return;
}
const thirdParagraph: Word.Paragraph = paragraphs.items[2];
const rangeOfParagraph: Word.Range = thirdParagraph.getRange();
const pages: Word.PageCollection = rangeOfParagraph.pages;
pages.load("items/index");
await context.sync();
console.log(`Pages containing the third paragraph: ${pages.items.length}`);
pages.items.forEach((page: Word.Page) => {
console.log(`Page index: ${page.index}`);
});
});
width
Gets the width, in points, of the paper defined in the Page Setup dialog box.
readonly width: number;
Property Value
number
Remarks
Method Details
getNext()
Gets the next page in the pane. Throws an ItemNotFound error if this page is the last one.
getNext(): Word.Page;
Returns
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Reads the page following the first page when the document has multiple pages.
await Word.run(async (context) => {
const pages: Word.PageCollection = context.document.activeWindow.activePane.pages;
pages.load("items");
await context.sync();
if (pages.items.length < 2) {
console.warn("Run Set up to create multiple pages before getting the next page.");
return;
}
const firstPage: Word.Page = pages.getFirst();
const nextPage: Word.Page = firstPage.getNext();
nextPage.load("index");
const nextPageRange: Word.Range = nextPage.getRange();
nextPageRange.load("text");
await context.sync();
console.log(`Next page index: ${nextPage.index}`);
console.log(`Next page text: ${nextPageRange.text}`);
});
getNextOrNullObject()
Gets the next page. If this page is the last one, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.
getNextOrNullObject(): Word.Page;
Returns
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/iterate-pages.yaml
// Visits each page and stops safely when there is no next page.
await Word.run(async (context) => {
const pages: Word.PageCollection = context.document.activeWindow.activePane.pages;
let currentPage: Word.Page = pages.getFirstOrNullObject();
await context.sync();
while (!currentPage.isNullObject) {
currentPage.load("index");
const pageRange: Word.Range = currentPage.getRange();
pageRange.load("text");
await context.sync();
console.log(`Page ${currentPage.index} text: ${pageRange.text}`);
currentPage = currentPage.getNextOrNullObject();
await context.sync();
}
console.log("Reached the end of the pages in the active pane.");
});
getRange(rangeLocation)
Gets the whole page, or the starting or ending point of the page, as a range.
getRange(rangeLocation?: Word.RangeLocation.whole | Word.RangeLocation.start | Word.RangeLocation.end | "Whole" | "Start" | "End"): Word.Range;
Parameters
Optional. The range location must be whole, start, or end.
Returns
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Reads the index and text of each page intersecting the selection.
await Word.run(async (context) => {
const selectedRange: Word.Range = context.document.getSelection();
const pages: Word.PageCollection = selectedRange.pages;
pages.load("items/index");
await context.sync();
console.log(`Pages in the selection: ${pages.items.length}`);
const pageRanges: Word.Range[] = [];
pages.items.forEach((page: Word.Page) => {
const pageRange: Word.Range = page.getRange(Word.RangeLocation.whole);
pageRange.load("text");
pageRanges.push(pageRange);
});
await context.sync();
for (let i = 0; i < pages.items.length; i++) {
console.log(`Page ${pages.items[i].index} text: ${pageRanges[i].text}`);
}
});
load(options)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(options?: Word.Interfaces.PageLoadOptions): Word.Page;
Parameters
- options
- Word.Interfaces.PageLoadOptions
Provides options for which properties of the object to load.
Returns
load(propertyNames)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(propertyNames?: string | string[]): Word.Page;
Parameters
- propertyNames
-
string | string[]
A comma-delimited string or an array of strings that specify the properties to load.
Returns
load(propertyNamesAndPaths)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(propertyNamesAndPaths?: {
select?: string;
expand?: string;
}): Word.Page;
Parameters
- propertyNamesAndPaths
-
{ select?: string; expand?: string; }
propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.
Returns
toJSON()
Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that's passed to it.) Whereas the original Word.Page object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.PageData) that contains shallow copies of any loaded child properties from the original object.
toJSON(): Word.Interfaces.PageData;
Returns
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Copies the first page's loaded properties into a plain JavaScript object.
await Word.run(async (context) => {
const firstPage: Word.Page = context.document.activeWindow.activePane.pages.getFirst();
firstPage.load("index,height,width");
await context.sync();
const pageData: Word.Interfaces.PageData = firstPage.toJSON();
console.log("First page data:", pageData);
});
track()
Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created. If this object is part of a collection, you should also track the parent collection.
track(): Word.Page;
Returns
untrack()
Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call context.sync() before the memory release takes effect.
untrack(): Word.Page;