Set the Paper Size and Orientation in a Doc Using Google Apps Script

Google Apps Script: DocumentApp setPageWidth, setPageHeight

Sometimes you need to prepare a Google Doc’s paper size and orientation programmatically using Google Apps Script.

Unfortunately, you can’t just call for say, A4 in Landscape. Okay, not until now (see my code below).

Google Apps Script does provide a way to set the dimensions of your page  in the body class by using:

  • setPageWidth(pageWidth)
  • setPageHeight(pageHeight)

The page widths and heights are measured in PostScripts Points which is a bit of a pain too.

Here is an example of setting and A3 paper size in Landscape.

Ugh. What a chore. You need to find the dimensions of the paper in points.

Enter this little nifty function and your life will be so much easier:

You can see that the dictionary of paper sizes matches the options in File>Page Setup...>Paper Size

Setup Docs Google Apps Script

The function takes 3 parameters:

  1. The Document variable. This can be a newly created document, and active document or one you call by id.
  2. The Paper Size. Identified by the name of the paper type followed by an underscore and the size. E.g. legal_size.
  3. Portrait or Landscape. Here a boolean is used. If true, then Portrait will be used. If false, then the page will be set to Landscape.

The function call would look like this:

var letterLandscape = paper_size(document,"letter_size", false);

Create and Publish Google Workspace Add-ons with Apps Script Course 300px

 

If you would prefer to do your own thing or you will only need to make one page, layout in your code then it might be best to use the setPageHeight(points), setPageWidth(points), methods and use the reference table below:

Paper Size Argument Inches Millimeters Points
Letter letter_size 8.5″x11 216×279 612.283×790.866
Tabloid tabloid_size 11″x17″ 279×432 790.866×1224.57
Legal legal_size 8.5″x14″ 216×356 612.283×1009.13
Statement statement_size 5.5″x8.5″ 140×216 396.85×612.283
Executive executive_size 7.25″x10.5″ 184×267 521.575×756.85
Folio folio_size 8.5″x13″ 216×330 612.283×935.433
A3 a3_size 11.69″x16.54″ 297×420 841.89×1190.55
A4 a4_size 8.27″x11.69″ 210×297 595.276×841.89
A5 a5_size 5.83″x8.27 148×210 419.528×595.276
B4 b4_size 9.84″x13.9″ 250×353 708.661×1000.63
B5 b5_size 6.93″x9.84″ 176×250 498.898×708.661

Finally, if you are looking to convert your own special page setup, then Google can do that for you. A simple keyword search like “milometers to points” will bring up Google’s unit converter.

If you want to make conversions in the code then:

  • 1 mm = 2.834646 point
  • 1 Inch = 72 Points

Hope you find this useful.

Create and Publish a Google Workspace Add-on with Apps Script Course

Need help with Google Workspace development?

My team of experts can help you with all of your needs, from custom app development to integrations and security. We have a proven track record of success in helping businesses of all sizes get the most out of Google Workspace.

Schedule a free consultation today to discuss your needs and get started or learn more about our services here.


2 thoughts on “Set the Paper Size and Orientation in a Doc Using Google Apps Script”

  1. thank you!!! your solution is just perfect!!!

Leave a Reply