cat_bookmarks() concatenates a list of bookmarks
into a single bookmarks data frame while updating the page numbers.
Useful if wanting to concatenate multiple pdf files together and
would like to preserve the bookmarks information.
cat_bookmarks(
l,
method = c("flat", "filename", "title"),
open = NA,
color = NA_character_,
fontface = NA_character_
)A list of bookmark data frames as returned by get_bookmarks().
Each data frame should have a "total_pages" attribute.
If method = "filename" each data frame should have a "filename" attribute.
If method = "title" each data frame should have a "title" attribute.
If "flat" simply concatenate the bookmarks while updating page numbers. If "filename" place each file's bookmarks a level under a new bookmark matching the (base)name of the filename and then concatenate the bookmarks while updating page numbers. If "title" place each file's bookmarks a level under a new bookmark matching the title of the file and then concatenate the bookmarks while updating page numbers.
If method = "filename" or method = "title" a logical for whether the new top level bookmarks should start open?
If missing will default to open.
Note some pdf viewers quietly ignore the initially open/closed feature.
If method = "filename" or method = "title" the color of the new top level bookmarks.
If NA_character_ will be unset (presumably defaults to "black").
Note many pdf viewers quietly ignore this feature.
If method = "filename" or method = "title" should the fontface of the new top level bookmarks.
If NA_character_ or NA_integer_ will be unset (defaults to "plain").
"plain" or 1 is plain, "bold" or 2 is bold, "italic" or 3 is italic,
Note many pdf viewers quietly ignore this feature.
A data frame of bookmark data (as suitable for use with set_bookmarks()).
A "total_pages" attribute will be set for the theoretical total pages of
the concatenated document represented by the concatenated bookmarks.
get_bookmarks() and set_bookmarks() for setting bookmarks.
cat_pages() for concatenating pdf files together.
if (supports_get_bookmarks() &&
supports_set_bookmarks() &&
supports_pdftk() &&
require("grid", quietly = TRUE)) {
# Create two different two-page pdf files
make_pdf <- function(f, title) {
pdf(f, onefile = TRUE, title = title)
grid.text(paste(title, "Page 1"))
grid.newpage()
grid.text(paste(title, "Page 2"))
invisible(dev.off())
}
f1 <- tempfile(fileext = "_doc1.pdf")
on.exit(unlink(f1))
make_pdf(f1, "Document 1")
f2 <- tempfile(fileext = "_doc2.pdf")
on.exit(unlink(f2))
make_pdf(f2, "Document 2")
# Add bookmarks to the two two-page pdf files
bookmarks <- data.frame(title = c("Page 1", "Page 2"),
page = c(1L, 2L))
set_bookmarks(bookmarks, f1)
set_bookmarks(bookmarks, f2)
l <- get_bookmarks(c(f1, f2))
print(l)
bm <- cat_bookmarks(l, method = "flat")
cat('\nmethod = "flat":\n')
print(bm)
bm <- cat_bookmarks(l, method = "filename")
cat('\nmethod = "filename":\n')
print(bm)
bm <- cat_bookmarks(l, method = "title")
cat('\nmethod = "title":\n')
print(bm)
# `cat_bookmarks()` is useful for setting concatenated pdf files
# created with `cat_pages()`
if (supports_cat_pages()) {
fc <- tempfile(fileext = "_cat.pdf")
on.exit(unlink(fc))
cat_pages(c(f1, f2), fc)
set_bookmarks(bm, fc)
unlink(fc)
}
unlink(f1)
unlink(f2)
}
#> $`/tmp/Rtmp6IsRgI/file607071d7e071_doc1.pdf`
#> title page level count open color fontface
#> 1 Page 1 1 1 NA NA <NA> <NA>
#> 2 Page 2 2 1 NA NA <NA> <NA>
#>
#> $`/tmp/Rtmp6IsRgI/file60701dbe79b8_doc2.pdf`
#> title page level count open color fontface
#> 1 Page 1 1 1 NA NA <NA> <NA>
#> 2 Page 2 2 1 NA NA <NA> <NA>
#>
#>
#> method = "flat":
#> title page level count open color fontface
#> 1 Page 1 1 1 0 NA <NA> <NA>
#> 2 Page 2 2 1 0 NA <NA> <NA>
#> 3 Page 1 3 1 0 NA <NA> <NA>
#> 4 Page 2 4 1 0 NA <NA> <NA>
#>
#> method = "filename":
#> title page level count open color fontface
#> 1 file607071d7e071_doc1.pdf 1 1 2 NA <NA> <NA>
#> 2 Page 1 1 2 0 NA <NA> <NA>
#> 3 Page 2 2 2 0 NA <NA> <NA>
#> 4 file60701dbe79b8_doc2.pdf 3 1 2 NA <NA> <NA>
#> 5 Page 1 3 2 0 NA <NA> <NA>
#> 6 Page 2 4 2 0 NA <NA> <NA>
#>
#> method = "title":
#> title page level count open color fontface
#> 1 Document 1 1 1 2 NA <NA> <NA>
#> 2 Page 1 1 2 0 NA <NA> <NA>
#> 3 Page 2 2 2 0 NA <NA> <NA>
#> 4 Document 2 3 1 2 NA <NA> <NA>
#> 5 Page 1 3 2 0 NA <NA> <NA>
#> 6 Page 2 4 2 0 NA <NA> <NA>