【问题标题】:How to make a border around an image in Word with officer如何在Word中与官员的图像周围制作边框
【发布时间】:2020-01-01 10:24:24
【问题描述】:

我想创建一个 Word 文件,左边有一个文本段落,一个 右侧的传单地图的图像。

到目前为止我可以做到,但我还想在整个传单图像周围做一个边框

我的示例中的边框只有图像大小的一半,因此看起来只有左侧有边框。

如何让边框和图片一样大?

library(shiny)
library(officer)
library(leaflet)
library(mapview)

ui <- fluidPage(
  leafletOutput("leafletmap"),
  downloadLink("downloadWord", "Download Word Docx")
)

getLeafletMap <- function() {
  leaflet() %>%
    addTiles() %>% 
    addPopups(-93.65, 42.0285, "Here is the <b>Department of Statistics</b>, ISU")
}

server <- function(input, output, session) {
  output$leafletmap <- renderLeaflet({
    getLeafletMap()
  })

  output$downloadWord <- downloadHandler(
    filename = 'Report.docx',
    content = function(file) {
      ## Map #########################
      map <- getLeafletMap()
      mapshot(x = map, file=paste0(getwd(), "/map.png"),
              remove_controls = c("layersControl"))

      ## Title & Texts #########################
      subtitle <- "Report Map"
      str5 <- "Lorem ipsum dolor sit amet, ligula iaculis mollis lacus consectetur, urna vitae potenti tortor!
      Sit commodo, venenatis leo at et. Ligula ac pulvinar sollicitudin gravida, lobortis lectus ligula et.
      Nisl tristique est erat lectus. Sodales egestas amet ac, ultricies nulla eu, risus blandit."

      ## Styles #########################
      text_style_title <- fp_text(font.size = 20, bold = FALSE, font.family = "Arial", color = "#808080")
      text_style <- fp_text(font.size = 10, bold = FALSE, font.family = "Arial")
      par_style <- fp_par(text.align = "justify")

      ## Make Word Docs #########################
      doc <- read_docx() %>%
        body_add_fpar(fpar(ftext("Report with Map", prop = text_style_title), fp_p = par_style)) %>%
        body_add_par("", style = "Normal") %>% # blank paragraph
        body_end_section_continuous() %>%

        body_add_fpar(fpar(ftext(str5, prop = text_style), fp_p = par_style)) %>%
        body_add_fpar(fpar(external_img(src = paste0(getwd(), "/map.png"), height = 3, width = 4.52),
                           fp_p = fp_par(text.align = "right", padding.top = 6,
                                         border = fp_border(width = 1, color = "red")))) %>%
        body_end_section_columns(widths = c(1.5,2), sep = FALSE, space = 0.2) %>%
        print(doc, target = file)
    }
  )
}

shinyApp(ui, server)

【问题讨论】:

  • 它是否适用于其他图像?
  • 据我所知,它没有。
  • 我个人在专栏方面遇到过很多问题。一种解决方法可能是使用具有 2 列的 flextable。
  • 我也想过,那我怎么只给右栏边框上色呢?

标签: r shiny ms-word officer


【解决方案1】:

我可以通过 flextablevlinehline 做到这一点。一项免责声明,由于我无法将hline 用于i=0,因此i=0 的表格中有一个空行。

library(flextable)
library(officer)

inputFolder <- "test/"
std_border = fp_border(color="orange")

testtable <- data.frame("text" = c("",""), "image" = c("",""))

testft <- flextable(testtable) %>%
  delete_part(part = "header") %>%
  theme_box() %>%
  compose(i = 2, j = 2, value= as_paragraph(as_image(src = paste(inputFolder, "smiley.png", sep = ""), width = 1, height = 1))) %>%
  compose(i = 2, j = 1, value = as_paragraph(as_chunk("Just some random text"))) %>%
  border_remove() %>%
  vline(i=2, j=1, border = std_border) %>%
  vline(i=2, j=2, border = std_border) %>%
  hline(i=1, j=2, border = std_border) %>%
  hline(i=2, j=2, border = std_border)

这给了我以下信息:

当然,您可以根据需要设置文本、图像和边框的格式。

编辑:

当然officer 部分只是:

doc <- read_docx() %>%
body_add_flextable(testft)

【讨论】:

  • 不幸的是,这适用于这个小例子,但如果列应该有不同的宽度并且图像更大,它就不起作用。以及如何左对齐文本?
  • 只需添加行align(j=1, align = "left", part="body")
  • 不知道为什么它不适用于不同的宽度和更大的图像?您可以根据需要创建任意数量的列和行,并为它们提供所需的宽度和高度。您还可以合并单元格。因此,最后您可以将其准确定位在您想要的位置,只需在图像的单元格周围放置一个边框,并使用正确的 i 和 j 值。
  • 我的最终解决方案有点不同,没有使用hlinevline,而是使用border,但无论如何我认为你应该得到赏金,因为你指出了我正确的方向! :) 再次感谢您
  • 啊,我还不知道border,谢谢你的提示!
猜你喜欢
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
相关资源
最近更新 更多