Including pages from PDF documents

From Wiki
Revision as of 11:48, 25 October 2013 by Halifax80 (talk | contribs)
Jump to navigation Jump to search

< Graphics | Using_Graphics | Overlays | Layers | Manipulation >

Introduction

page-imp.tex defines some handy macros for handling import of existing PDF documents.

To include only a specific page from a PDF document you can write

\externalfigure[file.pdf][page=1,width=...]

or

\startTEXpage
    \externalfigure[file.pdf][page=1,width=...]
\stopTEXpage

which is equal to

\startpagefigure[file.pdf][page=1,...]\stoppagefigure

This one inserts the first 30 pages of the document and adds a blank page after the first and the third page.

\insertpages[file.pdf][1,3][n=30,width=18cm]

If you want to insert a couple of pages use this:

\filterpages[file.pdf][1,3,4:6][width=18cm]

to insert pages 1, 3, 4, 5 and 6 or

\filterpages[file.pdf][even][width=18cm] % even/odd

to insert even/odd pages up to the page 30. (See also Taco's hack in Processing Lists for handling ranges of numbers or this thread in the mailing list.)

Yet another alternative is

\copypages[file.pdf][n=30][scale=950]

to insert the first 30 pages scaled down to 95%.

Page numbering with \startpagefigure

Experience shows that when you include a page from a PDF file using \startpagefigure, the page number of your document is not increased. The included PDF page is completely excluded from the page numbering process. This is annoying if you include a PDF page in the middle of your document, and expect your page numbers to be correct after the PDF page.

One solution is to use the commands \insertpages, \filterpages, and \copypages. They correctly increment the page numbers. But those commands include the PDF pages like additional "layers" on your page, so it means that your headers, footers, crop marks, etc. will be printed on top of or behind the included PDF pages.

Another solution is to fix the page number manually after using \startpagefigure.

Let's say we include 3 pages from a PDF like this:

% Several pages of text here
\startpagefigure[include_this.pdf][page=4]\stoppagefigure
\startpagefigure[include_this.pdf][page=6]\stoppagefigure
\startpagefigure[include_this.pdf][page=11]\stoppagefigure
% Several pages of text here

Notice that \startpagefigure can include only 1 page at a time. After compiling the above file with ConTeXt, your page numbers will look like this:

...
Page 10
Page 11
Page 12
include_this.pdf page 4
include_this.pdf page 6
include_this.pdf page 11
Page 13
Page 14
...

This behavior is also reflected in the command line output of ConTeXt. The value of userpage is the page number which will be printed in your header/footer. Continuing the above example:

pages           > flushing realpage 12, userpage 10
pages           > flushing realpage 13, userpage 11
pages           > flushing realpage 14, userpage 12
 <include_this.pdf>
pages           > flushing realpage 15, userpage 13 <-- include_this.pdf page 4
pages           > flushing realpage 16, userpage 13 <-- include_this.pdf page 6
pages           > flushing realpage 17, userpage 13 <-- include_this.pdf page 11
pages           > flushing realpage 18, userpage 13 <-- First ConTeXt page after PDF pages
pages           > flushing realpage 19, userpage 14

Therefore, you must "fix" your page number after you finish including PDF pages. You can either set it to a fixed number by saying:

% Several pages of text here
\startpagefigure[include_this.pdf][page=4]\stoppagefigure
\startpagefigure[include_this.pdf][page=6]\stoppagefigure
\startpagefigure[include_this.pdf][page=11]\stoppagefigure
\setcounter [userpage] [16]
% Several pages of text here

Or, which is even better, increment the page number by the number of included PDF pages:

% Several pages of text here
\startpagefigure[include_this.pdf][page=4]\stoppagefigure
\startpagefigure[include_this.pdf][page=6]\stoppagefigure
\startpagefigure[include_this.pdf][page=11]\stoppagefigure
\setcounter [userpage] [\pagenumber+3]
% Several pages of text here

This way your command line output will be:

pages           > flushing realpage 12, userpage 10
pages           > flushing realpage 13, userpage 11
pages           > flushing realpage 14, userpage 12
 
pages           > flushing realpage 15, userpage 13 <-- include_this.pdf page 4
pages           > flushing realpage 16, userpage 13 <-- include_this.pdf page 6
pages           > flushing realpage 17, userpage 13 <-- include_this.pdf page 11
pages           > flushing realpage 18, userpage 16 <-- First ConTeXt page after PDF pages
pages           > flushing realpage 19, userpage 17

This \setcounter command was tested and worked correctly with ConTeXt ver: 2013.10.01 20:08 MKIV beta. Other commands like \incrementpagenumber or \page[+1] or \page[16] did not help to solve this problem correctly.

Printing Slides from a presentation

If you want to make a 2*3 matrix from all the slides in the document you can use the following example:

\setuppapersize
  [A4][A4] % or [A4,landscape][A4,landscape]

\setuplayout
  [header=0pt,footer=1cm,
   backspace=1cm,topspace=1cm,
   width=middle,height=middle]

\setupfootertexts
  [presentation---\currentdate\space---\space\pagenumber]

\starttext
  \combinepages[slides][nx=2,ny=3,frame=on]
\stoptext

Slicing the pages

The opposite of combining the pages together is to slice them in pieces.

This will make 3*3 pages out of your document (well, it depends on proportions).

\starttext
    \slicepages[slice1.pdf][n=3]
\stoptext

Adding comments to pages

\setuppagecomment[state=start,location=right]

\startpagecomment
\input knuth
\stoppagecomment

TODO: prepare a graphics for each example (See: To-Do List)



Reformatting a Single-Sided pdf for Double-Sided Printing

Context can be used as a pdf postprocessor.

This example takes an original single-sided document and applies differing horizontal offsets to odd and even pages. This simultaneously

  • compensates for the fixed offset in the original, and
  • introduces a new alternating one as required for double-sided printing

It is useful for printing reference manuals downloaded from the web.


% replace "Manual" with the name of your original pdf
\define\MySingleSidedDocument{Manual}

% can adjust topspace to move page etc
\setuplayout[\MySingleSidedDocument,topspace=1cm,header=0cm,backspace=0cm]
% suppress page numbering
\setuppagenumbering[location=]

\starttext

\getfiguredimensions[\MySingleSidedDocument]

\dorecurse\noffigurepages
  {\hbox to \textwidth
     {\ifodd\recurselevel
%adjust number to set odd page horizontal position
        \hskip-0.1cm\externalfigure[\MySingleSidedDocument][page=\recurselevel]\hss
      \else
%adjust number to set even page horizontal position
        \hskip-1.8cm\externalfigure[\MySingleSidedDocument][page=\recurselevel]\hss
      \fi}}
\stoptext