Text

更新时间:
复制 MD 格式

This topic describes how to perform operations on text documents, such as exporting them to PDF, retrieving page numbers, and jumping to specific pages.

Important

This document is no longer maintained. Use the new version of Intelligent Media Management.

Export to PDF

 /*
  * Currently, only the RangeType and FrameSlides parameters are supported.
  * @param: { Range?: WdExportRange, From?: number, To?: number, Item?: WdExportItem, IncludeDocProps?: bool }
  * WdExportRange: {
  *      wdExportAllDocument: 0,
  *      wdExportCurrentPage: 2,
  *      wdExportFromTo: 3,
  *      wdExportSelection: 1,
  *  },
  * WdExportItem: {
  *      wdExportDocumentContent: 0,
  *      wdExportDocumentWithMarkup: 7
  *  }
  */
  await demo.WordApplication().ActiveDocument.ExportAsFixedFormat()

Get page numbers and jump to pages

Define the variables used in the following examples.

const app = demo.WordApplication()
const {Enum} = app
  • Retrieve the total number of pages

    Note

    Because text documents use a stream-based layout, the total page count is available only after the entire document is loaded.

     /*
      * @param: WdInformation: {
      *      wdNumberOfPagesInDocument: 4
      *  }
      * @return: {PagesCount: number, End: boolean}
      */
      let totalPages = await app.ActiveDocument.Range.Information(Enum.WdInformation.wdNumberOfPagesInDocument)
      if (totalPages.End) {
        console.log("Loading complete! Total pages:", totalPages.PagesCount)
      }
  • Retrieve the current page number

     /*
      * @param: WdInformation: {
      *      wdActiveEndPageNumber: 3
      *  }
      * @return: number
      */
      let currentPage = await app.ActiveDocument.Selection.Information(Enum.WdInformation.wdActiveEndPageNumber)
  • Jump to a specific page

    Note

    Because text documents use a stream-based layout, jumping to a page in a large document may take a long time. We recommend that you add a loading transition effect.

     /*
      * @param: { What?: WdGoToItem, Which?: WdGoToDirection.wdGoToAbsolute, Count?: number, Name?: string}
      * WdGoToItem: {
      *      wdGoToPage: 1,
      *  }
      *  WdGoToDirection: {
      *      wdGoToAbsolute: 1
      *  }
      */
      await app.ActiveDocument.Selection.GoTo(Enum.WdGoToItem.wdGoToPage, Enum.WdGoToDirection.wdGoToAbsolute, 10)
      // Or, use the following code.
      await app.ActiveDocument.Selection.GoTo({
        What: Enum.WdGoToItem.wdGoToPage,
        Which: Enum.WdGoToDirection.wdGoToAbsolute,
        Count: 10
      })
  • Current page change event

      function eventHandle() {
        // do something
      }
      // Listen for the current page change event.
      app.Sub.CurrentPageChange = eventHandle
      // Destroy the event listener.
      app.Sub.CurrentPageChange = null