I am trying to open and read a PDF file using Qt, but there is no specific way to do that.
Qt itself does not include PDF reading/rendering functionality as far as I know. You might want to have a look at libpoppler which has Qt bindings.
Qt Wiki — Handling PDF discusses various available options for working with PDF documents in a Qt application. The page does not exactly show how to "open and display an existing PDF document", but it can help you deduce something useful out of all that is explained there.
Using third-party libraries/tools
For rendering pages or elements from existing PDF documents to image files or in-memory pixmaps (useful e.g. for thumbnail generation or implementing custom viewers), third-party libraries can be used:
API parsing modifying creating platforms license poppler-qt4 [freedesktop.org] C+/Qt yes ? ? Win, Mac?, Linux, … GPL v2+ [strong copyleft] Hummus [pdfhummus.com] C++ yes yes yes Win, Mac, Linux Apache 2.0 [permissive] PoDoFo [podofo.sourceforge.net] C++ yes yes yes Win, Mac, Linux LGPL [weak copyleft] Using batch conversion tools
If all else fails, there is always the option of using an existing tool to automatically convert between PDF files and a more manageable format, and let your Qt application read/write that format instead. The conversion tool could be bundled with your application or specified as a prerequisite, and controlled via QProcess. Some possibilities are:
executable names .pdf to: … to .pdf platforms license poppler-utils [freedesktop.org] pdftotext, pdftocairo, pdftohtml .txt .svg .html … - Win, Mac?, Linux, … Inkscape [inkscape.org] inkscape .svg … .svg … Win, Mac, Linux, …
As of Qt 5.10
Check out the QPdfDocument Class. This class can open a PDF and you can use the render function to render a page to an image. I use the QQuickPaintedItem to then "draw" this image, but I am sure there are more ways to handle the QImage output.
Prior to Qt 5.10
I used libpoppler to do a VERY similar process.
#include <poppler/qt5/poppler-qt5.h>
Use the Poppler::Document Class to load and handle the entire PDF document and look at the Poppler::Page::renderToImage function to output the page as a QImage.
It is not supported in the Qt API.
You can code it, I suggest you have a look at this Qt application which uses Ghostscript.
© 2022 - 2024 — McMap. All rights reserved.