0

when creating documents with officer I am using body_end_section_portrait() and body_end_section_landscape() to set up orientations.

library(officer)

doc <- officer::read_docx()

doc <- officer::body_add_par(doc, "bla1", style = "Normal")
doc <- officer::body_end_section_portrait(doc)

doc <- officer::body_add_par(doc, "bla2", style = "Normal")
doc <- officer::body_end_section_landscape(doc)

doc <- officer::body_add_par(doc, "bla3", style = "Normal")
doc <- officer::body_end_section_portrait(doc)
print(doc, target = "bb.docx")

When I put page numbers in the created document they are not in the right order. E.g. in the provided example the numbers in the created document go 1, 3, 5, 6.

What am I doing wrong?

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=Croatian_Croatia.1250  LC_CTYPE=Croatian_Croatia.1250   
[3] LC_MONETARY=Croatian_Croatia.1250 LC_NUMERIC=C                     
[5] LC_TIME=Croatian_Croatia.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] officer_0.4.1 rJava_1.0-6  

1 Answers1

0

Ok, it seems that the way to go is by using body_end_block_section() where you can define the section break type.

First I defined how my sections will look like

#landscape section
landscape=block_section(
  prop_section(
    page_size = page_size(orient="landscape"), type = "nextPage"
  )
)

#portrait section
portrait=block_section(
  prop_section(
    page_size = page_size(orient="portrait"), type = "nextPage"
  )
)

Now the code is (I changed body_end_section_landscape()/portrait() with body_end_block_section()):

doc <- officer::read_docx()
doc <- officer::body_add_par(doc, "bla1", style = "Normal")
doc <- officer::body_end_block_section(doc, value=portrait)

doc <- officer::body_add_par(doc, "bla2", style = "Normal")
doc <- officer::body_end_block_section(doc, value=landscape)

doc <- officer::body_add_par(doc, "bla3", style = "Normal")
doc <- officer::body_end_block_section(doc, value=portrait)
print(doc, target = "bb.docx")
benson23
  • 16,369
  • 9
  • 19
  • 38