1

On my R markdown code that outputs a word file and uses a template, I'm trying to change the page orientation of some of the pages. The word template that I'm using contains custom styles and headers and footnotes.

I was able to find this OpenXML code that changes the orientation of the page midway:

<w:p>
  <w:pPr>
    <w:sectPr>
      <w:pPr>
        <w:sectPr>
          <w:pgSz w:orient="landscape" w:w="15840" w:h="12240"/>
        </w:sectPr>
      </w:pPr>
    </w:sectPr>
  </w:pPr>
</w:p>

I was able to change the orientation of my desired page. But the problem is the headers and footnotes on my word template was removed. Is there a way to change the orientation of a specific page using openxml but also retain the headers and footnotes on the template?

My R markdown code is something like this:

---
params:
   issueDate: 'December 2022'
   report_mo: '202212'
title: ""
output: 
  word_document:
    reference_docx: "template.docx"
---

# Header 1
Lorem ipsum dolor sit amet. Et quia voluptatum ea quis enim cum repudiandae quidem aut adipisci doloremque ex veniam neque eos explicabo accusantium. Ut consequatur quae et voluptas magni aut enim facilis et perspiciatis suscipit ut voluptatem magni!

#this will maintain the portrait orientation on the 1st page
``{=openxml}
<w:p>
  <w:pPr>
    <w:sectPr>
      <w:pPr>
        <w:sectPr>
          <w:pgSz w:orient="portrait" w:h="15840" w:w="12240"/>
        </w:sectPr>
      </w:pPr>
    </w:sectPr>
  </w:pPr>
</w:p>
``

# Header 2
Lorem ipsum dolor sit amet. Et quia voluptatum ea quis enim cum repudiandae quidem aut adipisci doloremque ex veniam neque eos explicabo accusantium. Ut consequatur quae et voluptas magni aut enim facilis et perspiciatis suscipit ut voluptatem magni!

#this will change the orientation of the second page to landscape
``{=openxml}
<w:p>
  <w:pPr>
    <w:sectPr>
      <w:pPr>
        <w:sectPr>
          <w:pgSz w:orient="landscape" w:w="15840" w:h="12240"/>
        </w:sectPr>
      </w:pPr>
    </w:sectPr>
  </w:pPr>
</w:p>
``

Note that I purposely use two on the openxml code for this post for using 3 will end the code block.

The openxml code is working but as I mentioned earlier, it omits the headers and footers from the word template on the output.

  • I was able to find another way to change the orientation midway using the solution I found [here](https://stackoverflow.com/questions/66795576/making-word-documents-a3-and-landscape-orientation-with-officer-r-package) but the footnotes and headers appear on the page after I returned the orientation to portrait. e.g. page 1-4 - portrait, page 5 - landscape, page 6 - portait the footnotes and header started appears on page 6 and not on page 1. – albertbcs46 Apr 14 '23 at 03:11

0 Answers0