[R] adding "Page X of XX" to PDFs

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sun Dec 3 00:14:38 CET 2023


On 02/12/2023 5:37 p.m., CALUM POLWART wrote:
> You could easily omit the Page X of xX, but leave the timestamp
> 
> Then add Page X of XX programmatically using pdftools or some similar pdf
> command line tools.

You don't need to use command line tools -- I showed how to do it by 
creating an R Markdown document with each page of the PDF on a numbered 
page of the result.

Here's a minor improvement of my post:

    ---
    title: "Numbered"
    output:
      pdf_document:
        extra_dependencies: ["pdfpages", "fancyhdr", "lastpage"]
    ---

    \cfoot{Page \thepage\ of \pageref{LastPage}}

    \addtocounter{page}{-1}

    \includepdf[pages={1-},pagecommand={\thispagestyle{fancy}}]{Rplots.pdf}


This works regardless of the number of pages in Rplots.pdf.

Duncan Murdoch

> 
> On Sat, 2 Dec 2023, 22:35 , <avi.e.gross using gmail.com> wrote:
> 
>> Having read all of the replies, it seems there are solutions for the
>> question and the OP points out that some solutions such as making the
>> document twice will affect the creation date.
>>
>> I suspect the additional time to do so is seconds or at most minutes so it
>> may not be a big deal.
>>
>> But what about the idea of creating a PDF with a placeholder like "Page N
>> of
>> XXX" and after the file has been created, dates and all, perhaps edit it
>> programmatically and replace all instances of XXX with something of the
>> same
>> length like " 23" as there seem to be tools like the pdftools package that
>> let you get the number of pages. I have no idea if some program, perhaps
>> external, can do that and retain the date you want.
>>
>> -----Original Message-----
>> From: R-help <r-help-bounces using r-project.org> On Behalf Of Dennis Fisher
>> Sent: Friday, December 1, 2023 3:53 PM
>> To: r-help using r-project.org
>> Subject: [R] adding "Page X of XX" to PDFs
>>
>> OS X
>> R 4.3.1
>>
>> Colleagues
>>
>> I often create multipage PDFs [pdf()] in which the text "Page X" appears in
>> the margin.  These PDFs are created automatically using a massive R script.
>>
>> One of my clients requested that I change this to:
>>          Page X of XX
>> where XX is the total number of pages.
>>
>> I don't know the number of expected pages so I can't think of any clever
>> way
>> to do this.  I suppose that I could create the PDF, find out the number of
>> pages, then have a second pass in which the R script was fed the number of
>> pages.  However, there is one disadvantage to this -- the original PDF
>> contains a timestamp on each page -- the new version would have a different
>> timestamp -- so I would prefer to not use this approach.
>>
>> Has anyone thought of some terribly clever way to solve this problem?
>>
>> Dennis
>>
>> Dennis Fisher MD
>> P < (The "P Less Than" Company)
>> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
>> www.PLessThan.com
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list