Search code examples
office-js

Is it possible to get text with line breaks via Word api?


It seems that context.document.body.text (or paragraph.text or whatever.text) always has text value without line(newline) break. The only possible way to get text with newline characters is to parse ooxml or html. Ooxml parsing is no go because for office online ooxml is computed on server side and often takes up to 3 seconds to complete. Html retrieving is slow also and takes up to 500 ms event for one page files. It weird that you can insert text that contains newline characters (/n) but cannot retrieve this text back.

So the question is : Is it possible to get text with line breaks via Word api?


Solution

  • A new line character is included. If you look carefully the text retrieved, a '\r' is returned within the text, that's a new line or paragraph marker.

    The line break is also included (line breaks are inserted if users do Shift+Enter).

    This is an Sample literal string returned after getting the context.document.body.text property. That document has many paragraphs, I bolded te \r added:

    "Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document.\rTo make your document look professionally produced, Word provides header, footer, cover page, and text box designs that complement each other. For example, you can add a matching cover page, header, and sidebar. Click Insert and then choose the elements you want from the different galleries.\rThemes and styles also help keep your document coordinated. When you click Design and choose a new Theme, the pictures, charts, and SmartArt graphics change to match your new theme. When you apply styles, your headings change to match the new theme.\rSave time in Word with new buttons that show up where you need them. To change the way a picture fits in your document, click it and a button for layout options appears next to it. When you work on a table, click where you want to add a row or a column, and then click the plus sign.\rReading is easier, too, in the new Reading view. You can collapse parts of the document and focus on the text you want. If you need to stop reading before you reach the end, Word remembers where you left off - even on another device.\r"