This topic describes how to export the document as a PDF or an image file, obtain the total number of pages, change the paging mode, show or hide the table of contents, and obtain the page width when you use a text document.
Export a document as a PDF or an image file
Export a document as a PDF or an image file and obtain the URL of the exported file.
Syntax
Expression.ActiveDocument.ExportAsFixedFormat({ OutputFileName, ExportFormat })Expression: the document type application object.
Parameters
Parameter
Data type
Required
Description
OutputFileName
String
No
The name of the exported file. This parameter is not supported.
ExportFormat
Enum
No
The type of the file to which the document is exported. Valid values of
Enum.WdExportFormat:17 or wdExportFormatPDF: exports the document as a PDF file. This is the default value.
18 or wdExportFormatXPS: exports the document as an XML Paper Specification (XPS) file. This value is not supported.
19 or wdExportFormatIMG: exports the document as an image file.
OpenAfterExport
Boolean
No
Specifies whether to automatically open the exported file. This parameter is not supported.
OptimizeFor
Enum
No
Specifies whether to optimize for screen or print. This parameter is not supported. Valid values of
Enum.WdExportOptimizeFor:0 or wdExportOptimizeForPrint: exports for print. The exported file has higher quality and a larger file size.
1 or wdExportOptimizeForOnScreen: exports for screen. The exported file has lower quality and a smaller file size.
Range
Enum
No
The range to be exported. Valid values of
Enum.WdExportRange:0 or wdExportAllDocument: exports the entire document. This is the default value.
1 or wdExportSelection: exports the currently selected content.
2 or wdExportCurrentPage: exports the current page.
3 or wdExportFromTo: exports the content of a range by using a start position and an end position.
From
Number
No
The starting page number. This parameter is required if Range is set to wdExportFromTo.
To
Number
No
The ending page number. This parameter is required if Range is set to wdExportFromTo.
Item
Enum
No
Specifies whether to export the document with markup. Valid values of
Enum.WdExportItem:0 or wdExportDocumentContent: exports the document without markup. This is the default value.
7 or wdExportDocumentWithMarkup: exports the document with markup.
IncludeDocProps
Boolean
No
Specifies whether to include document properties in the exported file.
KeepIRM
Boolean
No
Specifies whether to copy Information Rights Management (IRM) permissions to an XPS document if the source document has IRM protection. This parameter is not supported. Default value: true. Valid values:
true
false
CreateBookmarks
Enum
No
Specifies whether to export bookmarks and the type of bookmarks to export. This parameter is not supported. Valid values of
Enum.WdExportCreateBookmarks:0 or wdExportCreateHeadingBookmarks: creates a bookmark in the exported file for each Microsoft Office Word heading, which includes only headings within the main document and text boxes not within headers, footers, endnotes, footnotes, or comments.
1 or wdExportCreateNoBookmarks: does not create bookmarks in the exported document.
2 or wdExportCreateWordBookmarks: creates a bookmark in the exported document for each Word bookmark, which includes all bookmarks except those contained within headers and footers.
DocStructureTags
Boolean
No
Specifies whether to include extra data to help screen readers, such as information about the flow and logical organization of the content. Default value: true. Valid values:
true
false
BitmapMissingFonts
Boolean
No
Specifies whether to include a bitmap of the text. This parameter is not supported. Default value: true. Valid values:
true: includes a bitmap of the text. Font licenses do not permit a font to be embedded in the PDF file.
false: references the text font. The computer of the viewer substitutes an appropriate font if the specified one is not available.
UseISO19005_1
Boolean
No
Specifies whether to limit PDF usage to the PDF subset standardized as ISO 19005-1. This parameter is not supported. Default value: false. Valid values:
false: no.
true: yes. The resulting files are more reliably self-contained but may be larger or show more visual artifacts due to format restrictions. This parameter is not supported.
FixedFormatExtClassPtr
Enum
No
Specifies a pointer to an add-in that allows the calls to the alternative implementations of code. The alternative implementations of code interpret the EMF and EMF+ pages generated by the application and save the file as other formats, such as PDF and XPS. This parameter is not supported.
Dpi
Number
No
The DPI of the exported image. Default value: 92.
Combine2LongPic
Boolean
No
Specifies whether to export the document as a long image. Default value: false. Valid values:
false
true
ImgFormat
Enum
No
The format of the exported image. Valid values of
Enum.WdExportImgFormat:0 or wdExportFormatPNG: PNG. This is the default value.
1 or wdExportFormatJPG: JPG.
2 or wdExportFormatBMP: BMP.
3 or wdExportFormatTIF: TIF.
WaterMark
Boolean
No
Specifies whether the exported file has a watermark. Default value: false. Valid values:
false
true
Return value
Returns a
stringvalue that represents the URL of the exported file.Example
Export the document as a PDF file
async function example() { await instance.ready(); const app = instance.Application; // Export the document as a PDF file and obtain the URL of the exported file. const pdfUrl = await app.ActiveDocument.ExportAsFixedFormat(); // Export the document as a PDF file by default. console.log(pdfUrl); }Export the document as an image
async function example() { await instance.ready(); const app = instance.Application; // Export the document as an image and obtain the URL of the exported file. const imgUrl = await app.ActiveDocument.ExportAsFixedFormat({ ExportFormat: app.Enum.WdExportFormat.wdExportFormatIMG }); // Or specify 19. console.log(imgUrl); }
Obtain the total number of pages
Use the Range.Information method to obtain the information about pages. Currently, only the total number of pages can be obtained.
Because text documents use a liquid layout, the total number of pages of a text document can be obtained only when you browse to the bottom of the document.
Syntax
Expression.ActiveDocument.Range.Information(WdInformation)Expression: the document type application object.
Parameters
The following table describes the types of information about the selection or range that you can obtain by using
app.Enum.WdInformation.Parameter
Data type
Required
Description
WdInformation
Enum
Yes
Obtains the information about the selection or range. Valid values of
Enum.WdInformation:3 or wdActiveEndPageNumber: returns the number of the page that contains the active end of the specified selection or range, counting from the beginning of the document. Any manual adjustments to page numbering are ignored.
4 or wdNumberOfPagesInDocument: returns the number of pages in the document associated with the selection or range. This is the default value.
Return values
Parameter
Data type
Description
PagesCount
Number
The number of pages that finished typesetting.
End
Boolean
Indicates whether the typesetting ends.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the total number of pages. const totalPages = await app.ActiveDocument.Range.Information(app.Enum.WdInformation.wdNumberOfPagesInDocument); console.log(totalPages); }
Obtain the number of the current page
Obtain the number of the current page.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.Information(WdInformation)Expression: the document type application object.
Parameters
Parameter
Data type
Required
Description
WdInformation
Enum
Yes
Obtains the information about the selection or range. Valid values of
Enum.WdInformation:3 or WdInformation.wdActiveEndPageNumber: returns the number of the page that contains the active end of the specified selection or range, counting from the beginning of the document. Any manual adjustments to page numbering are ignored.
4 or WdInformation.wdNumberOfPagesInDocument: returns the number of pages in the document associated with the selection or range. This is the default value.
Return value
Returns a number that represents the number of the current page.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the number of the current page. const currentPage = await app.ActiveDocument.ActiveWindow.Selection.Information(app.Enum.WdInformation.wdActiveEndPageNumber); console.log(currentPage ); }
Change the paging mode
Change the paging mode to the paged mode or the pageless mode.
Syntax
Expression.ActiveDocument.SwitchTypoMode(Boolean)Expression: the document type application object.
Specify a value of the
Booleantype.trueindicates that the pageless mode is used.falseindicates that the paged mode is used.Return value
A value of the
Booleantype is returned.trueindicates that the paging mode is changed successfully.falseindicates that the paging mode fails to be changed.Example
async function example() { await instance.ready(); const app = instance.Application; // Change to the paged mode. await app.ActiveDocument.SwitchTypoMode(false); }
Obtain the total number of pages in the paged mode
Obtain the total number of pages in the paged mode.
Syntax
Expression.ActiveDocument.ActiveWindow.ActivePane.Pages.CountExpression: the document type application object.
Return value
Returns a
numberthat represents the total number of pages in the paged mode.Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the total number of pages in the paged mode. const pages = await app.ActiveDocument.ActiveWindow.ActivePane.Pages.Count; console.log(pages); }
Show or hide the file name column
The file name column can be displayed at the beginning of the content in the pageless mode. The file name column is not applicable to the paged mode. Use the SwitchFileName method to show or hide the file name column.
Syntax
Expression.ActiveDocument.SwitchFileName(Boolean)Expression: the document type application object.
Specify a value of the
Booleantype.trueindicates that the file name column is displayed.falseindicates that the file name column is hidden.Example
async function example() { await instance.ready(); const app = instance.Application; // Change to the pageless mode. await app.ActiveDocument.SwitchTypoMode(true); // Hide the file name column. await app.ActiveDocument.SwitchFileName(false); }
Show or hide the table of contents
Show or hide the table of contents.
Syntax
Expression.ActiveDocument.ActiveWindow.DocumentMap = BooleanExpression: the document type application object.
Specify a value of the
Booleantype.trueindicates that the table of contents is displayed.falseindicates that the table of contents is hidden.Example
async function example() { await instance.ready(); const app = instance.Application; // Show the table of contents. app.ActiveDocument.ActiveWindow.DocumentMap = true; }
Listen to the clipboard events
Obtain the clipboard callback events.
Syntax
Expression.Sub.ClipboardCopy = FunctionExpression: the document type application object.
Return values
Parameter
Data type
Description
copyId
String
The ID of the copy operation.
isRestoreFocus
Boolean
Indicates whether the focus is restored.
text
String
The copied content.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the clipboard callback events. app.Sub.ClipboardCopy = async (e) => { await console.log(e); } }
Go to a specified position
Use the GoTo() method to go to a specified position. For example, go to a specified page or a specified bookmark.
Because text documents use a liquid layout, it takes a long time to go to a specified position in a large document. In this case, we recommend that you add a loading transition effect.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.GoTo(What, Which, Count, Name)Expression: the document type application object.
Parameters
Parameter
Data type
Required
Description
What
Number
No
The type of the item prior to which the selection or content is moved. Valid values of
WdGoToItem:-1 or WdGoToItem.wdGoToBookmark: a bookmark.
1 or WdGoToItem.wdGoToPage: a page.
Which
Number
No
The item prior to which the selection or content is moved. If you set
WdGoToDirectionto 1 or WdGoToDirection.wdGoToAbsolute, absolute positioning is used.Count
Number
No
The number of items in the document. The default value is 1. Only positive integers are valid.
Name
String
No
If What is set to wdGoToBookmark, specify a name.
Return value
Returns the page that you go to.
Example
Go to a specified page
// Go to the specified page. async function example() { await instance.ready(); const app = instance.Application; const page = await app.ActiveDocument.ActiveWindow.Selection.GoTo(instance.Enum.WdGoToItem.wdGoToPage, instance.Enum.WdGoToDirection.wdGoToAbsolute, 3); }Go to a specified bookmark
// Obtain all bookmarks. async function example() { await instance.ready(); const app = instance.Application; // Add a bookmark. await app.ActiveDocument.ActiveWindow.Bookmarks.Add({ Name: 'WebOffice', Range: { Start: 1, End: 10, }, }); // Go to the specified bookmark. await app.ActiveDocument.ActiveWindow.Selection.GoTo( instance.Enum.WdGoToItem.wdGoToBookmark, // Type: Bookmark. instance.Enum.WdGoToDirection.wdGoToAbsolute, // The position. undefined, // The number. 'WebOffice', // The bookmark name. ); // Or await instance.Application.ActiveDocument.ActiveWindow.Selection.GoTo({ What: Enum.WdGoToItem.wdGoToBookmark, Which: Enum.WdGoToDirection.wdGoToAbsolute, Name: 'WebOffice', // The bookmark name. }); }
Save a document
Save changes to a document.
JS-SDK V1.1.9 and later support this feature.
Syntax
Expression.ActiveDocument.Save()Expression: the document type application object.
Return values
Parameter
Data type
Description
result
String
The save status.
size
Number
The size of the document. Unit: bytes.
version
Number
The version.
Description of the save status
Save status
Description
ok
The version is saved. You can view the saved version in historical versions of the document.
nochange
The document is not updated. You do not need to save the version.
SavedEmptyFile
The document is empty and cannot be saved. This status is returned when the document is empty after the kernel is saved.
SpaceFull
The storage space is used up.
QueneFull
Frequent saving operations are not allowed when the document is being saved. This status is returned if the saving operation queue is full on the server.
fail
Failed to save the document.
Example
async function example() { await instance.ready(); const app = instance.Application; // Save changes to the document. const save = await app.ActiveDocument.Save(); console.log(save); }
Page properties
Obtain the page property objects
Syntax
Expression.ActiveDocument.Sections.Item(Index).PageSetupExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the page property objects. const pageSetup = await app.ActiveDocument.Sections.Item(1).PageSetup; }
Obtain the page width
Syntax
Expression.ActiveDocument.Sections.Item(Index).PageSetup.PageWidthExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the page property objects. const pageSetup = await app.ActiveDocument.Sections.Item(1).PageSetup; // Obtain the page width. const pageWidth = await pageSetup.PageWidth; console.log(pageWidth); }
Typeset to a specified page
Because text documents use a liquid layout, you can use the TypesetToPage() method to specify the number of pages to typeset and load when the document is opened. Other pages are loaded when you scroll the document.
Syntax
Expression.ActiveDocument.Sections.Item(Index).PageSetup.TypesetToPage(PageNum)Expression: the document type application object.
Parameters
Parameter
Data type
Required
Description
PageNum
Number
Yes
The number of pages to load when the document is opened.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the page property objects. const pageSetup = await app.ActiveDocument.Sections.Item(1).PageSetup; // Typeset to the specified page. await pageSetup.TypesetToPage(2); }
Obtain the page height
Syntax
Expression.ActiveDocument.Sections.Item(Index).PageSetup.PageHeightExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the page property objects. const PageSetup = await app.ActiveDocument.Sections.Item(1).PageSetup; // Obtain the page height. const PageHeight = await PageSetup.PageHeight; console.log(PageHeight); }
Obtain the distance between the top edge of the page and the top boundary of the body text
Syntax
Expression.ActiveDocument.Sections.Item(Index).PageSetup.TopMarginExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the page property objects. const PageSetup = await app.ActiveDocument.Sections.Item(1).PageSetup; // Obtain the distance between the top edge of the page and the top boundary of the body text. const TopMargin = await PageSetup.TopMargin; console.log(TopMargin); }
Obtain the distance between the bottom edge of the page and the bottom boundary of the body text
Syntax
Expression.ActiveDocument.Sections.Item(Index).PageSetup.BottomMarginExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the page property objects. const PageSetup = await app.ActiveDocument.Sections.Item(1).PageSetup; // Obtain the distance between the bottom edge of the page and the bottom boundary of the body text. const BottomMargin = await PageSetup.BottomMargin; console.log(BottomMargin); }
Obtain the distance between the left edge of the page and the left boundary of the body text
Syntax
Expression.ActiveDocument.Sections.Item(Index).PageSetup.LeftMarginExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the page property objects. const PageSetup = await app.ActiveDocument.Sections.Item(1).PageSetup; // Obtain the distance between the left edge of the page and the left boundary of the body text. const LeftMargin = await PageSetup.LeftMargin; console.log(LeftMargin); }
Obtain the distance between the right edge of the page and the right boundary of the body text
Syntax
Expression.ActiveDocument.Sections.Item(Index).PageSetup.RightMarginExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the page property objects. const PageSetup = await app.ActiveDocument.Sections.Item(1).PageSetup; // Obtain the distance between the right edge of the page and the right boundary of the body text. const RightMargin = await PageSetup.RightMargin; console.log(RightMargin); }