1

I want to replace an image in the header/footer in a .docx document, with R and preferably using the officer package by david gohel.

I am using the body_replace_text_at_bkm function. I have tried to use the the example code from (https://davidgohel.github.io/officer/reference/body_replace_text_at_bkm.html).

template <- system.file(package = "officer", "doc_examples/example.docx")
img.file <- file.path( R.home("doc"), "html", "logo.jpg" )
doc <- read_docx(path = template)
doc <- headers_replace_img_at_bkm(x = doc, bookmark = "bmk_header",
                                  value = external_img(src = img.file, width = .53, height = .7))
doc <- footers_replace_img_at_bkm(x = doc, bookmark = "bmk_footer",
                                  value = external_img(src = img.file, width = .53, height = .7))
print(doc, target = tempfile(fileext = ".docx"))

I expect the R logo in the header and the footer. But i get the text "a" in the header and "another" in the footer. The bookmarks are still present.

The word document looks like this: example.docx

Can you help me out? Sorry for my english and sorry in case my question is not clear. This is my first question I ask on stackoverflow.

  • 1
    can you add a screen shot of the result header or footer please? This example works as expected on my machine – David Gohel Apr 30 '19 at 09:53
  • 1
    wouw thanks for your fast reply. I am actually a big fan of the officer package. It works great so far. – Lennart Jongen Apr 30 '19 at 10:11
  • 1
    Try the latest version of github. There was an issue with image sizes - the images may be there but tiny tiny... This is fixed in the dev version – David Gohel Apr 30 '19 at 10:51
  • 1
    Thank you David Gohel. It works now. It was indeed realated to the size. In my case the image was incredibly big. So i made the width and height smaller. I used `width = 0,01` and `height = 0,015`. Then it showed up in the header and footer. I am very happy that it works so great. Thanks again! – Lennart Jongen Apr 30 '19 at 13:10

0 Answers0