[R] Adding page numbers to existing PDFs
Paul Murrell
p@u| @end|ng |rom @t@t@@uck|@nd@@c@nz
Fri Oct 28 06:39:08 CEST 2022
Hi
Below is a script that does the job for a very simple R plot, making use
of R and a couple of free software tools (ghostscript and poppler). It
may or may not help with your real problem (NOTE the WARNING) ...
library(grImport)
## Multipage PDF created by R
pdf("plot-in.pdf")
plot(1)
plot(2)
dev.off()
## Separate into individual pages
system("pdfseparate plot-in.pdf plot-in-%d.pdf")
inFiles <- list.files(pattern="plot-in-[0-9]+.pdf")
for (i in seq_along(inFiles)) {
## Convert to PostScript
## (WARNING: may cause [some] rasterization, depending on content
## of plot and on the conversion tool; anything that is
## rasterized will NOT get imported)
system(paste0("pdf2ps plot-in-", i, ".pdf plot-in-", i, ".ps"))
## Import to R
PostScriptTrace(paste0("plot-in-", i, ".ps"),
paste0("plot-in-", i, ".xml"))
plot <- readPicture(paste0("plot-in-", i, ".xml"))
## Add page number (in new PDF)
pdf(paste0("plot-out-", i, ".pdf"))
grid.picture(plot)
grid.text(paste0("Page ", i), .5, unit(1, "lines"))
dev.off()
}
outFiles <- list.files(pattern="plot-out-[0-9]+.pdf")
## Recombine into single, multi-page PDF
system(paste0("pdfunite ", paste(outFiles, collapse=" "),
" plot-out.pdf"))
If you do want to use something like this, let me know, because there
are some other limitations that you might want to be aware of.
Paul
On 22/10/2022 6:45 am, Dennis Fisher wrote:
> R 4.2.1
> OS X
>
> Colleagues
>
> I have multipage PDF files that were created in R — the files do NOT
> have page numbers. I would like to add page numbers after the fact,
> i.e., read a file into R, add margin text, then output as a PDF.
> Can this be done in R (base R or a package)?
> It can be done in Python using reportlab.pdfgen — however, the file size
> increases prohibitively.
>
> Dennis
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
> <http://www.PLessThan.com>
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> <http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
--
Dr Paul Murrell
Te Kura Tatauranga | Department of Statistics
Waipapa Taumata Rau | The University of Auckland
Private Bag 92019, Auckland, New Zealand
64 9 3737599 x85392
paul using stat.auckland.ac.nz
www.stat.auckland.ac.nz/~paul/
More information about the R-help
mailing list