TransWikia.com

How to add page number for a pdf file whose page sizes are different

TeX - LaTeX Asked on February 6, 2021

I want to add page numbers at the bottom of each page in a pdf page. The page sizes are different

I left 15pt at the bottom of each page.

The following is my pdf how it looks

enter image description here

I have tried

documentclass{article}
usepackage{pdfpages}
usepackage[footskip = 14mm,showframe]{geometry}

usepackage{fancyhdr}
fancyhf{} 
cfoot{fontsize{20}{20}selectfont thepage}

begin{document}
eject pdfpagewidth=432pt pdfpageheight=179.64pt
includepdfmerge[fitpaper,pagecommand={thispagestyle{fancy}}]{/home/simha/latex/test.pdf, 1743}
eject pdfpagewidth=432pt pdfpageheight=71.63999999999999pt
includepdfmerge[fitpaper,pagecommand={thispagestyle{fancy}}]{/home/simha/latex/test.pdf, 1744}
eject pdfpagewidth=432pt pdfpageheight=192.12pt
includepdfmerge[fitpaper,pagecommand={thispagestyle{fancy}}]{/home/simha/latex/test.pdf, 1745}
end{document}

What I get is

enter image description here

How to add page number at the bottom when page sizes are different

If all page sizes are same size then the following code works
Here my entire document is 432pt, 226pt (W,H). Using footskip=14mm, i can position the pagenumber preciselt where i want.

batchmode
documentclass[a4paper,]{article}
usepackage[
    papersize={432pt, 226pt},
    footskip = 14mm,
    ]{geometry}
usepackage[utf8]{inputenc}
usepackage{pdfpages}

usepackage{fancyhdr}
fancyhf{} 
cfoot{fontsize{20}{20}selectfont thepage}
begin{document}
thetextwidth; thetextheight
includepdfmerge[pagecommand={thispagestyle{fancy}}]{/var/tmp/pdfjam-7FXkbO/source-1.pdf,-}
end{document}

What i get is as below

enter image description here

3 Answers

Here is an incgraph solution:

  • in the first example, the page number is put inside the picture,
  • in the second example, the page number is put outside the picture, hence enlarged the page size.
documentclass{article}
usepackage{incgraph}
usepackage{tikz}

igrset{
  my page number/.style={
    overlay={
      node[anchor=south] at ([yshift=5pt]page.south) {thepage};
    }
  }
}

begin{document}
content
incgraph[my page number][page=1]{example-image-a}

igrpage{%
  begin{tikzpicture}
    node[inner sep=0pt] (pic) {includegraphics{example-image-b}};
    node at ([yshift=-10pt]pic.south) {thepage};
  end{tikzpicture}%
}
end{document}

first example second example

Answered by muzimuzhi Z on February 6, 2021

After a lot of struggle i could do it by the following.

The answer is based on Change Page Size Mid-Document: Fancy Header and Footer Horizontal Width

But still i have to struggle a lot and finally using left=0mm,right=0mm,top=0mm, bottom=0mm,footskip=1mm in newgeometry did the job

Also showframe helper me to visualize the textarea.

The problem is fancyhdr does not adopt to the mid document page size changes. and then understading the geometry package was the key

But still i am half knowledge. and left few questions i have as comments in the script. I dont know what is their purpose. but trail and error i understood they are required.

documentclass{scrartcl} % THIS IS REQUIRED
% QUESTION:
% documentclass{article} pushes the pagenumber down
% I dont know the reason
usepackage{geometry}
usepackage{pdfpages}
usepackage[automark,headsepline,footsepline]{scrlayer-scrpage} % THIS IS REQUIRED
% QUESTION:
% without the above line the page number is pushed down /out of the page
% I dont know the reason
% usepackage{showframe}

begin{document}

newgeometry{layoutwidth  = 432pt,
layoutheight = 179.64pt,
left=0mm,right=0mm,top=0mm, bottom=0mm,footskip=1mm}
includepdfmerge[fitpaper,pagecommand={thispagestyle{plain}}]{/home/simha/latex/test.pdf, 1743}

newgeometry{layoutwidth  = 432pt,
layoutheight = 71.63999999999999pt,
left=0mm,right=0mm,top=0mm, bottom=0mm,footskip=1mm}
includepdfmerge[fitpaper,pagecommand={thispagestyle{plain}}]{/home/simha/latex/test.pdf, 1744}

newgeometry{layoutwidth  = 432pt,
layoutheight = 192.12pt,
left=0mm,right=0mm,top=0mm, bottom=0mm,footskip=1mm}
includepdfmerge[fitpaper,pagecommand={thispagestyle{plain}}]{/home/simha/latex/test.pdf, 1745}
end{document}

The output is

enter image description here

Also for how to add space at bottom (eg 15mm for page number) in a pdf with different page sizes and then add pagenumber, refer answer https://tex.stackexchange.com/a/556945/221200

Answered by Santhosh on February 6, 2021

My basic idea would be to wrap a tikzpicture in the pagecommand of the includepdfmerge. In addition you would need the remember picture, overlay argument to write on the pdf and the pagenodes function of tikz to get the position at the bottom of your current page. This mechanism should be able to handle different page sizes of your included pdfs and put the pagenumber always at the bottom (set the yshift argument to a value that suits you).

MWE based on your code

documentclass{article}
usepackage{pdfpages, tikz}

begin{document}
includepdfmerge[fitpaper,pagecommand={
   begin{tikzpicture}[remember picture, overlay]
     node[yshift=15pt] at (current page.south) {thepage};
   end{tikzpicture}}]{/var/tmp/pdfjam-7FXkbO/source-1.pdf,-}
end{document}

Answered by Nico on February 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP