127

I've got a PDF that is automatically generated from a certain piece of software, that I want to include in the report I'm writing. I'm using pdfpages which gets the job done elegantly, but the problem is, the PDF has a lot of white space around the actual content (which is a graphics) and I always have to do some tedious post-processing of the automatically generated document so that it fits well in my report. Changing the output of the software to eliminate the white space is not an option (or at least, would be much more trouble than it's worth).

I checked the pdfpages manual, but wasn't able to find an option that does that. Ideally, I'd like to be able to set margins from each direction, and the PDF is then cropped accordingly and included.

What other options do I have to control what portion of the included PDF is visible in the final document?

3

8 Answers 8

69

Run pdfcrop on the whole document, which will crop every page individually. I always use the pdfcrop from Heiko Oberdiek which is already part of every TeX distribution. You may also need to install perl.

You can then input the resulting PDF as usual with pdfpages or alternatively with \includegraphics[page=...]{<image>}.

1
129

You can crop/trim a pdf when including it using the trim=left botm right top.

Full example:

\begin{figure}[htbp]
    \centering
        \includegraphics[clip, trim=0.5cm 11cm 0.5cm 11cm, width=1.00\textwidth]{gfx/BI-yourfile.pdf}
    \caption{Title}
    \label{fig:somthing}
\end{figure}

Note: Figuring out how far to trim can take time. To speed things up a bit it helps to draw a box around the image:

   \fbox{\includegraphics[trim=0.5cm 11cm 0.5cm 11cm]{gfx/BI-yourfile.pdf}}
4
  • 14
    I think you are missing the clip key, otherwise the trimmed content is still displayed. Ok, for the OP document that content is only whitespace, but other people might have something else, maybe a page number, which should be removed. Jul 3, 2013 at 10:43
  • 21
    It should be noted that this way the complete original image (AFAIK for PDF just the selected page) is added to the output PDF and the clipping is done using added PDF instructions executed by the PDF viewer. For smaller clipping amounts that would be perfectly reasonable, but I wouldn't do it if I only want a small amount of a larger image in order to keep the output PDF small. It's also not recommended if the part which is clipped away contains sensitive informations, because the original image can be extracted using a suitable PDF tool. Jul 3, 2013 at 10:43
  • 2
    Thanks Martin for pointing this out. In my use case I had an auto generated pdf with too much whitespace around it and didn't want manually crop it each time it was generated. So feel free to edit my post accordingly.
    – s.Daniel
    Jul 4, 2013 at 12:02
  • When I add clip, trim=0.5cm 11cm 0.5cm 11cm, to my existing includegraphics call inside the brackets before my existing (and working) width= parameter, I get ! Undefined control sequence. \ETE@OrgGin@setfile ...\fi \fi \Gin@viewport@code \Gin@nat@height \Gin@ury b... Is there some special package that one needs in order to use trim?
    – sh37211
    Apr 5, 2023 at 2:20
20

If the coordinates of the valuable parts in your PDF images is fixed, then the following method can be automated.

Use the following template to trim or crop images and compile it with xelatex. You will get 2 pages, one for navigation and the other one is the cropped image.

% cropping.tex
\documentclass{article}
\usepackage{pstricks}
\usepackage{graphicx}


\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}

% Set the border to 0pt if you don't like paddings.
\PreviewBorder=1cm\relax


% If you need 4 colored paddings, uncomment the following.
% But  remmember that \PreviewBorder=0 will ignore it.
%\pagecolor{yellow!10}

\newsavebox\IBox
\savebox\IBox{\includegraphics[width=\linewidth]{leibniz.jpg}}

\def\N{15}% columns
\def\M{15}% rows

\psset
{
   xunit=\dimexpr\wd\IBox/\N\relax,
   yunit=\dimexpr\ht\IBox/\M\relax
}


\begin{document}
    % First page for navigation
    \begin{pspicture}(\N,\M)
        \rput[bl](0,0){\usebox\IBox}
        \psgrid[style=gridstyle]
    \end{pspicture}

    % Second page for the final output
    % Cropping coordinates
    \def\Left{6}
    \def\Bottom{8}
    \def\Right{10}
    \def\Top{13}
    \begin{pspicture}(\Left,\Bottom)(\Right,\Top)
        \begin{psclip}{\psframe[linestyle=none](\Left,\Bottom)(\Right,\Top)}
            \rput[bl](0,0){\usebox\IBox}
        \end{psclip}
    \end{pspicture}
\end{document}

enter image description here

From within your main TeX document, you can import the second page of cropping.pdf simply by using \includegraphics[page=2,scale=<number>]{cropping.pdf}.

Please read the comments in the source file to modify the padding thickness or padding color.

Note: This example uses Gottfried Leibniz's picture.

2
  • Note: Clipping with PSTricks is more robust than \includegraphics[viewport=...,clip]{} when using xelatex. Aug 16, 2011 at 10:00
  • How can this be automated to multipage pdfs? Mar 24, 2020 at 15:08
14

You can do this with pdfpages. The following example takes a two-up scan of a book, and crops and collates it to a one-up document.

\documentclass[letterpaper]{minimal}
\usepackage[pdftex,letterpaper]{geometry}
\usepackage{pdfpages}
\usepackage{ifthen}

\newcounter{pg}
\begin{document}
\setcounter{pg}{1}
%% my pdf file has 132 pages:
%% my pdf file has size 780 x 610 points
\whiledo{\value{pg}<133}{%
\includepdf[pages=\thepg,viewport=0 0 390 610]{scan.pdf}
\includepdf[pages=\thepg,viewport=390 0 780 610]{scan.pdf}
\addtocounter{pg}{1}
}%
\end{document}
1
  • 3
    This works, but any print outside the viewport is still printed unless you add the clip keyword. Mar 8, 2015 at 0:49
11

Since you'd like to "...set margins from each direction, and the PDF is then cropped accordingly..." in order to "...control what portion of the included PDF is visible in the final document..." I would suggest you try Briss. It's easy to use and gives you much more control than pdfcrop.

6

You can do it inside your tex document using a boundig box:

 \includegraphics[bb = 0 10 612 7925,clip=true]{yourfile.pdf}

This means that the figure fits inside a rectangle with lower left corner at (0,10) and upper right corner (612,792). The unit is 1/72 inches (.3528 mm) measured from the lower left corner of the paper.

6

If you check now the pdfpages manual, you will read:

Internally the command \includepdf makes use of the \includegraphics command from the graphicx (actually graphics) package. Hence it is possible to use all the options of \includegraphics, too. Options which are not interpreted by \includepdf are passed directly to \includegraphics. Especially the trim and clip options of \includegraphics are quite useful (...).

So you can simply use \includepdf[trim=5cm 10cm 0 0, clip]{./YourSource.pdf}

1
  • 1
    This answer is underrated! I think this is the best and most straight forward answer someone can get for this problem.
    – agentsmith
    Jan 31, 2022 at 12:06
5

On Mac OS-X you can use Skim or the built-in Preview to crop PDFs: Just open the PDF, select the crop area and choose Tools->Crop (⌘+K).

If you need to keep the original, just copy the selection (⌘+C) and choose File->New from Clipboard (⌘+N).

If you want to use one of the LaTeX-based methods, Skim might still be helpful to determine the bounding box coordinates (it prints them in the status bar when selecting an area).

1
  • For macOS Preview, you may need to enter select mode (Tools > Rectangular Selection) before Crop works.
    – Allison
    Dec 15, 2022 at 6:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .