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.

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?

share|improve this question
3  
Incidentally, there are two separate pdfcrop projects in existence, and both are Perl scripts: here and on CTAN –  Martin Tapankov Aug 16 '11 at 8:53
2  
Perhaps this post is related? Then you can use the clip from \includegraphics when inserting the image. –  Werner Aug 16 '11 at 15:35

7 Answers 7

up vote 22 down vote accepted

run pdfcrop on the whole document, then every page is cropped and you can input it as usual with pdfpages. I always use the pdfcrop from Heiko Oberdiek which is already part of every TeX distribution. And, of course, Windows user need an installed Perl, eg http://www.activestate.com/perl

share|improve this answer

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.

share|improve this answer
    
Note: Clipping with PSTricks is more robust than \includegraphics[viewport=...,clip]{} when using xelatex. –  xport Aug 16 '11 at 10:00

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

Full example:

\begin{figure}[htbp]
    \centering
        \includegraphics[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}}
share|improve this answer
4  
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. –  Martin Scharrer Jul 3 '13 at 10:43
3  
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. –  Martin Scharrer Jul 3 '13 at 10:43
    
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 '13 at 12:02

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.

share|improve this answer

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}
share|improve this answer

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).

share|improve this answer

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.

share|improve this answer

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.