Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

So I just learned about \pdffilesize from this answer.

Are there other similar things like this? I learned about \the\inputlineno a while ago which prints the line number of the input file.

In particular, is there a pdf page count? What other information about the file can you extract in this way?

To clarify, what I'm asking is what other commands like \pdffilesize exist?

share|improve this question
    
No fair: you completely changed the question :-) –  Joseph Wright Nov 15 '12 at 13:07
    
texdoc pdftex –  AlexG Nov 15 '12 at 13:18
    
@JosephWright Not really: I only added one sentence and changed the title. –  Seamus Nov 15 '12 at 16:42
add comment

3 Answers 3

up vote 12 down vote accepted

You can print the number of pages of foo.pdf with the following trick:

\sbox0{\pdfximage{foo.pdf}}\the\pdflastximagepages

Here's the excerpt from the pdftex manual:

\pdflastximagepages (read-only integer)
This read-only register returns the highest page number from a file previously accessed via the \pdfximage command. This is useful only for PDF files; it always returns 1 for PNG, JPEG, or JBIG2 files.

Here is a bigger list of file related primitives. Note that they are unavailable in LuaTeX, but they can be emulated with the pdftexcmds package. Where I write {file} I mean a <general text> such that the expansion of the <balanced text> inside it is a file name; in particular {test.tex} can be used, but also {\jobname.tex}.

  • \pdffilemoddate{file} expands to the modification date of the file in the form D:20101122164111-08’00’ that is D:YYYYMMDDHHMMSSXhh'mm' where X is + or - and hhmm is the difference with UTC. All characters have category code 12.

  • \pdfcreationdate expands to the date of the PDF file being created (same format as before)

  • \pdffilesize{file} expands to the file size (in bytes)

  • \pdfmdfivesum file {file} expands to the MD5 of the file (here the file following \pdfmdfivesum is a keyword, so it must appear explicitly)

  • \pdffiledump offset <number> length <number> {file} expands to a hexadecimal dump of the file starting at 0 if offset <number> is missing; if present, <number> bytes will be left out. All the file will be dumped if length <number> is missing; otherwise only <number> bytes will be dumped.

share|improve this answer
1  
\pdfmdfivesum{<text>} expands to the MD5 of <text> and \pdfmdfivesum file{<file>} expands to the MD5 of the file <file>. –  Heiko Oberdiek Nov 16 '12 at 13:26
    
@HeikoOberdiek Thanks, I'll fix. –  egreg Nov 16 '12 at 13:32
add comment

Egreg has provided a way to obtain the page count of a PDF using pdfTeX to extract this information from the PDF itself, but it's worth adding to this why a simple 'core TeX' approach does not work.

You may know that TeX uses \count0 to provide information about which page is being output, and that this is used for adding page number information to printed pages. However, TeX does not use this to track the 'absolute page from start of document'. You can see that with a short demo file

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\count0 10 %
\lipsum
\end{document}

This produces two pages of output, but as I've set the first page as '10' they are listed as pages 10 and 11. TeX writes the value of \count0 to the log when a page is shipped out (in square brackets), and it shows that as far as TeX is concerned these are indeed pages ten and eleven:

This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012)
 \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2011/06/27>
Babel <v3.8m> and hyphenation patterns for english, dumylang, nohyphenation, ge
rman-x-2012-05-30, ngerman-x-2012-05-30, afrikaans, ancientgreek, ibycus, arabi
c, armenian, basque, bulgarian, catalan, pinyin, coptic, croatian, czech, danis
h, dutch, ukenglish, usenglishmax, esperanto, estonian, ethiopic, farsi, finnis
h, french, friulan, galician, german, ngerman, swissgerman, monogreek, greek, h
ungarian, icelandic, assamese, bengali, gujarati, hindi, kannada, malayalam, ma
rathi, oriya, panjabi, tamil, telugu, indonesian, interlingua, irish, italian, 
kurmanji, latin, latvian, lithuanian, mongolian, mongolianlmc, bokmal, nynorsk,
 polish, portuguese, romanian, romansh, russian, sanskrit, serbian, serbianc, s
lovak, slovenian, spanish, swedish, turkish, turkmen, ukrainian, uppersorbian, 
welsh, loaded.
(/usr/local/texlive/2012/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2012/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2012/texmf-dist/tex/latex/lipsum/lipsum.sty) (./test.aux)
[10{/usr/local/texlive/2012/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
[11] (./test.aux) )</usr/local/texlive/2012/texmf-dist/fonts/type1/public/amsfo
nts/cm/cmr10.pfb>
Output written on test.pdf (2 pages, 22281 bytes).
SyncTeX written on test.synctex.gz.
Transcript written on test.log.

Indeed, it is also possible to have the same page number twice

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum
\count0 1 %
\lipsum
\end{document}

Thus \count0 is only reliable if you can be certain that nothing has messed about with it, which in general is not the case.

share|improve this answer
1  
There is a more reliable method based on \shipout to count the absolute number of pages, e.g. see modules zref-abspage and zref-totpages of package zref. –  Heiko Oberdiek Nov 16 '12 at 13:29
1  
@HeikoOberdiek I'm sure, but I assume this still requires a count that you set up and manage (i.e. the engine does not directly provide the absolute number of pages). –  Joseph Wright Nov 16 '12 at 13:31
add comment

Simply put \usepackage{lastpage} in the preamble and \pageref{LastPage} somewhere (usually in "page \thepage of \pageref{LastPage}" on the footer).

share|improve this answer
    
lastpage’s label LastPage only gives the page number of the last page; it doesn't report the number of pages between the start of the document and its end. (The zref module totpages offers that.) –  wasteofspace Nov 15 '12 at 14:05
1  
Well... but if there is any difference, I prefer to print the number of the last page and not the total number of pages. That is, if for some reason I have pages designed to be not counted (the cover, for example), then propably I do not want to count them. The opposite is less likely (It sounds strange a last page like "page 190 of 200"). –  Fran Nov 15 '12 at 21:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.