Timothy Law
Timothy Law

Reputation: 324

Detecting and setting paper size in Word JS API or OOXML

Now that I know how to resize a floating image in Word-addins + OOXML, is there a way that OOXML or Office-JS can resize the image according to various paper size (letter, legal, A4, etc.)? Or must the paper size be calculated and entered manually into XML (i.e. ...cx="7772400" cy="10058400")

Wonder if there's something like getPaperSize() / getPaperWidth / getPaperHeight methods or properties?

Upvotes: 1

Views: 776

Answers (2)

Cindy Meister
Cindy Meister

Reputation: 25673

With the exception of Drawing Objects there is no provision in Word to link a graphic object to the relative size of a page. So it will need to be calculated.

The page size can be ascertained from the document's Word Open XML. The information is in the <w:pgSz> attributes w:w (width) and w:h (height). The measurements are in twentieths of a point.

The relevant segment of Word XML: the XML definition of the last section of the document, just before the </w:body> element, is below. Note that a document can have varying page sizes for each section. This can be seen if an envelope is attached to a document, for example. Or if landscape and portrait is mixed, then the width and height measurements will reverse (although the actual page size is the same). If that could happen, then it would be necessary to pick up all section breaks. These would be attached to a w:p tag.

<w:sectPr w:rsidR="00000000" w:rsidRPr="00154F24">
  <w:pgSz w:w="11906" w:h="16838"/>
  <w:pgMar w:top="1417" w:right="1417" w:bottom="1134" w:left="1417" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="708"/>
  <w:docGrid w:linePitch="360"/>
</w:sectPr></w:body></w:document>

Upvotes: 2

Rick Kirkham
Rick Kirkham

Reputation: 9769

Currently, there's no API in Office.js that returns paper dimensions. But it's a good idea. Please suggest it at Office Developer Suggestion Box.

Upvotes: 1

Related Questions