How can I extract the page dimensions from a PDF file with a C++ program ?
Is there any lightweight, cross-platform, easy to build/install, library that could help ?
Thanks :)
[EDIT]
I tried to build podofo under windows, but it didn't work. LibHaru and PDFHummus are for rendering pdfs, not parsing their data.
[EDIT & SOLUTION]
I've finally built podofo under windows!
(btw, under Ubuntu, it was just a matter of sudo apt-get install libpodofo0.9.0 libpodofo-dev libpodofo-utils
)
It was actually a CMake 2.8.10 bug, switching back to 2.8.3 solved the problem ( thanks )
If anyone is interested, here's how to get started with podofo under windows (I'll demonstrate how to build the debug/shared version):
- download the required libraries (freetype, jpeg, openssl, tiff, zlib) and install them (I suggest, copying all the files under
LIB_NAME\lib
inC:\libraries\lib
, same thing for\include
) - install CMake 2.8.3
- add MinGW\bin and CMake\bin to the
PATH
- download podofo and uzip it somewhere, let's say in
C:\downloads\podofo-0.9.2
- create a new directory
C:\downloads\podofo-0.9.2\podofo-shared-debug
- open the command prompt then type
cd C:\downloads\podofo-0.9.2\podofo-shared-debug
run this command to prepare a MinGW makefile for the debug/shared version of podofo
cmake -G "MinGW Makefiles" .. -DCMAKE_INCLUDE_PATH=C:\libraries\include -DCMAKE_LIBRARY_PATH=C:\libraries\lib -DPODOFO_BUILD_SHARED:BOOL=TRUE -DCMAKE_BUILD_TYPE=DEBUG
then just call make
mingw-make.exe
if you're using codeblocks, here's how to install/use podofo:
9.1. copy the following
podofo-0.9.2\podofo --> C:\libraries\podofo-0.9.2\include\podofo
podofo-0.9.2\src --> C:\libraries\podofo-0.9.2\include\src
podofo-0.9.2\podofo-shared-debug\podofo_config.h --> C:\libraries\podofo-0.9.2\include\podofo_config.h
podofo-0.9.2\podofo-shared-debug\src\libpodofo.dll.a --> C:\libraries\podofo-0.9.2\lib-shared-debug\libpodofo.dll.a
podofo-0.9.2\podofo-shared-debug\src\libpodofo.dll --> C:\libraries\podofo-0.9.2\bin-shared-debug\libpodofo.dll
9.2. use -lpodofo
in the linker options then add C:\libraries\podofo-0.9.2\include
to the compiler's search directories and C:\libraries\podofo-0.9.2\lib-shared-debug
to the linker's search directories
Getting back to my initial question, the closest answer to "how to extract the page dimensions from a PDF file with a C++ program (using podofo) ?" is this:
#include <iostream>
#include <podofo/podofo.h>
int main (int argc, char* argv[])
{
PoDoFo::PdfMemDocument document;
try
{
document.Load("path/to/the/file.pdf");
}
catch (PdfError& e )
{
// a password is required to read this pdf
return -1;
}
try
{
PoDoFo::PdfRect pageSize = document.GetPage(0)->GetPageSize(); // yes, it's "0-based"
std::cout << "width: " << pageSize.GetWidth() << "\t\theight: " << pageSize.GetHeight() << std::endl;
}
catch (PdfError& e )
{
// page number error
return -2;
}
return 0;
}