将文本字符串添加到现有的 pdf 文档

Adding text string to existing pdf doc

有人知道用 R 将文本字符串添加到现有 pdf(或多个 pdf)的方法吗?例如,可能想在页面的指定位置添加标题或水印。

这些 similar-sounding 问题不是我要找的,因为我想将文本添加到现有的 pdf - 不是我在 Rmd 中编写的,这样会更直接:

如果 R 中 pdftk is an acceptable auxiliary tool, then you can automate the solution from this question 的(自由命令行)版本(即,在 pdftk 中使用 stamp 操作):

生成 PDF 以进行试验:

pdf("test1.pdf")
plot(1:10,1:10)
dev.off()

生成“水印”叠加层:

pdf("stamp.pdf")
par(fg="white")
## to match spacing etc - 'phantom' plot
plot(1:10,1:10, type="n", axes=FALSE, ann=FALSE)
par(fg="black")
text(5,5, "watermark", col=adjustcolor("gray", alpha.f=0.2), cex=5)
dev.off()

调用pdftk进行叠加:

system("pdftk test1.pdf stamp stamp.pdf output test1S.pdf")

结果:

我认为挑剔的部分是按照您想要的方式在覆盖文件中获取间距...尽管您可以使用网格图形,或者创建一个 zero-margin/zero-annotation 绘图以便绘图区域有边界(0,1) x (0,1) ...