Saturday 7 August 2010

podofocrop: PDF cropping in 10 lines of code

The PoDoFo tools family got a new member during the last week: podofocrop. This little tool crops the white margins of pages in PDF files.

I use tools like these a lot, for example when writing scientific papers or the like, because I try to include all figures as PDF files to have better quality in the final PDF. To do so, it is often necessary to remove the margin around the figure before including it. This is exactly what podofocrop is doing.

The figure below illustrates what podofocrop is doing. Figure (a) shows the input PDF file, we can see a page with a little bit of text and a big white margin. In the next step, Figure (b), podofocrop calculates the bounding box for the final PDF, i.e. the area on the page that has content and the size to which we want to crop the PDF. Ghostscript is used for this step and has to be in your PATH. Finally, the PDF is cropped and the new page is much smaller without any disturbing margins.



If you are interested, get podofocrop from the PoDoFo SVN. In case of questions, please contact the mailing list.

Please note: Windows support is still experimental and untested so far. It will compile but there are no guarantees that it will work. Help with the Windows port would be greatly appreciated.

At the end, let we give you one little detail on the implementation, because it is so amazingly simple using PoDoFo. If you remove the code for parsing command line arguments, communication with Ghostscript and error handling, you see that the tool has only a few lines:


std::vector cropBoxes = get_crop_boxes_from_gs( pszInput );
PdfVariant var;
PdfMemDocument doc;
doc.Load( pszInput );

for( int i=0;i {
PdfPage* pPage = doc.GetPage( i );
cropBoxes[i].ToVariant( var );
pPage->GetObject()->GetDictionary().AddKey( PdfName("MediaBox"), var );
}

doc.Write( pszOutput );

5 comments:

Unknown said...

Wow, nice Feature.
I also use this for my LaTeX documents.
But I always asked a friend who uses MacOSX doing this for me...
Is there any chance to get this integrated to Okular?

anhkuzit said...

I give good love
I’ll buy your clothes
I’ll cook your dinner too
Soon as I get home from work
I’ll pay your rent
Your faithful lover
Soon as I get home, soon as I get hom
Kirk Sanford Sightline Paymentsbetting guide

Anonymous said...

I give good love
I’ll buy your clothes
I’ll cook your dinner too
Soon as I get home from work
I’ll pay your rent
Your faithful lover
Online Bingovalutahandel

uknowme said...

I give good love
I’ll buy your clothes
I’ll cook your dinner too
Soon as I get home from work
I’ll pay your rent
Your faithful lover
Soon as I get home, soon as I get hom
hottubs

footballbettingnews.co.uk

uknowme said...

I give good love
I’ll buy your clothes
I’ll cook your dinner too
Soon as I get home from work
I’ll pay your rent
Your faithful lover
Soon as I get home, soon as I get hom
D3 steel

heel pain Cibolo