TransWikia.com

How can I crop included PDF documents?

TeX - LaTeX Asked on May 31, 2021

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?

8 Answers

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

Correct answer by user2478 on May 31, 2021

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=1cmrelax


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

newsaveboxIBox
saveboxIBox{includegraphics[width=linewidth]{leibniz.jpg}}

defN{15}% columns
defM{15}% rows

psset
{
   xunit=dimexprwdIBox/Nrelax,
   yunit=dimexprhtIBox/Mrelax
}


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

    % Second page for the final output
    % Cropping coordinates
    defLeft{6}
    defBottom{8}
    defRight{10}
    defTop{13}
    begin{pspicture}(Left,Bottom)(Right,Top)
        begin{psclip}{psframe[linestyle=none](Left,Bottom)(Right,Top)}
            rput[bl](0,0){useboxIBox}
        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.

Answered by xport on May 31, 2021

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.

Answered by DJP on May 31, 2021

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.00textwidth]{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}}

Answered by s.Daniel on May 31, 2021

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}

Answered by anon on May 31, 2021

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

Answered by Daniel on May 31, 2021

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.

Answered by dorien on May 31, 2021

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}

Answered by Andrestand on May 31, 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