rcursorofficer

How to set the cursor in the beginning of the page with r package officer?


I'm inserting several paragraphs in a word document and the space between paragraphs must be two empty lines. As predict, sometimes one or two paragraphs are positioned at the beginning of a page. In these cases, I need to remove these empty paragraphs.

Does someone have any suggestion?

I was thinking to detect empty lines in the beginning of each page and remove them.


Solution

  • Instead of manually adjusting this, have you considered changing the paragraph style for all your paragraphs such that there's a 2 x (whatever your chosen font size is) space after each paragraph? This way, the spacing will appear within the same page, but not at the top of a new page.

    # Note: the following assumes you already have a document with content. If you
    # are creating from scratch, you should be able to apply the desired paragraph 
    # style whenever you add a new paragraph, using body_add_par.
    
    library(officer)
    library(dplyr)
    
    # load file
    dd <- read_docx("sample.docx")
    
    # check the name of the style currently used in the document (default should be 'Normal')
    View(docx_summary(dd)) 
    
    # optional: check all existing styles in the document & their names
    # View(styles_info(dd))  
    
    # define new paragraph style that adds 40pt spacing after each paragraph
    # (assuming font size of 20pt, this is equivalent to two empty lines)
    dd <- dd %>%
      docx_set_paragraph_style(style_id = "Normal1", style_name = "Normal1",
                               fp_p = fp_par(padding.bottom = 2 * 20)) 
    
    # for all paragraphs that use one or more old styles, change their style reference
    # to the newly created style with large spacing after each paragraph
    dd <- dd %>% change_styles(mapstyles = list(
      "Normal1" = c("Normal") # new style = c(old style 1, old style 2, ...)
    ))
    
    # optional: confirm that the desired paragraphs have changed their styles
    # View(docx_summary(dd)) 
    
    # save result
    print(dd, "new.docx") 
    

    Sample dataset (I used the starwars dataset as the basis for creating random chunks of text since it's readily available as part of the dplyr package):

    doc <- read_docx()
    
    sample.text <- paste(unlist(unname(starwars)), collapse = " ") %>%
      substring(., 
                first = seq(1, nchar(.), by = 1000), 
                last = c(seq(1000, nchar(.), by = 1000), nchar(.)))
    
    for(i in seq_along(sample.text)) {
      doc <- doc %>%
        body_add_par(value = sample.text[[i]])
    }
    
    print(doc, "sample.docx")
    
    rm(i, doc, sample.text)