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)
}
Run the code above in your browser using DataLab