概要

Qtは、PDFファイルの作成、表示、操作のための複数のクラスとモジュールを提供している。
これらは主に、QtPrintSupportモジュールとQtGuiモジュールに含まれている。

QtのPDFサポートは基本的なPDF作成や表示において基本的な機能に限られているため、より高度な操作が必要な場合は、追加のライブラリやツールの使用を推奨する。

例えば、QPdfWriterライブラリは小規模から中規模のPDFファイル生成に適している。
大規模で複雑なPDFファイルの場合、または、複雑なPDF操作や高度な機能が必要な場合は、サードパーティ製ライブラリの使用を検討する必要がある。


QtのPDFモジュール

主要なクラス

  • QPdfWriterクラス
    PDFファイルを作成するためのクラスである。
  • QPrinterクラス
    プリンタやPDFファイルへの出力を扱うクラスである。
  • QPagedPaintDeviceクラス
    ページベースのペイントデバイスの基本クラスである。
  • QPdfDocumentクラス (Qt 5.10以降)
    PDFドキュメントを読み込み、操作するためのクラスである。


PDF作成

QPdfWriterクラス、または、QPrinterクラスを使用して、PDFファイルを作成することができる。
QPainterクラスと組み合わせて使用することにより、テキスト、図形、画像などをPDFに描画できる。

PDF表示

Qt 5.10以降、Qt WidgetsではQPdfViewerウィジェットが提供されており、PDFファイルを表示することができる。

PDF操作

QPdfDocumentクラスを使用して、PDFファイルのページ数の取得、ページの抽出、メタデータの読み取り等の操作が可能である。

印刷サポート

QtPrintSupportモジュールを使用することで、PDFファイルの作成だけでなく、実際のプリンタへの印刷も可能である。

サードパーティ製ライブラリとの統合

Qtは、Popplerライブラリ、MuPDFライブラリ等のサードパーティ製PDFライブラリと統合することも可能であり、より高度なPDF操作を行うことができる。


Qtライブラリ

PDFの作成

Qtでは、簡単にPDFを作成する機能が用意されている。

<syntaxhighlight lang="c++">
#include <QPdfWriter>

// PDFを作成
QPdfWriter pdfWriter("ファイル名");

// レイアウトオブジェクト
QPageLayout pdfLayout;

// 単位をポイントに設定
pdfLayout.setUnits(QPageLayout::Point);

// 紙サイズ、余白を設定
pdfLayout.setPageSize(QPageSize(QPageSize::A4), QMarginsF(105.0, 40.0, 40.0, 20.0));

// 縦に設定
pdfLayout.setOrientation(QPageLayout::Portrait);

// レイアウトオブジェクトをPDFに設置
pdfWriter.setPageLayout(pdfLayout);

// DPIを取得する場合は以下のようにする
double dPixToPoints = (double)pdfWriter.resolution() / 72.0;

// QPainterクラスを使用してPDFを書き込む
QPainter painter;
if(painter.begin(&pdfWriter))
{
   painter.drawText(QPoint(100, 100), "何か文字");

   // 改ページ
   pdfWriter.newPage();
}

painter.end();
</syntaxhighlight>



Popplerライブラリ

Popplerライブラリは、QPdfWriterライブラリと比較して、より高度なPDF操作や読み込みに適している。

PDFの作成

以下の例では、HTMLコンテンツを含むPDFファイルを作成している。

<syntaxhighlight lang="c++">
#include <QCoreApplication>
#include <QPainter>
#include <QPdfWriter>
#include <QTextDocument>

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   QPdfWriter writer("output.pdf");
   writer.setPageSize(QPageSize(QPageSize::A4));

   QPainter painter(&writer);
   painter.setPen(Qt::black);

   QTextDocument doc;

doc.setHtml("

Hello, PDF!

" "

This is a sample PDF created with Poppler and Qt.

");

   doc.setPageSize(writer.pageRect().size());
   doc.drawContents(&painter);

   painter.end();

   return 0;
}
</syntaxhighlight>


PDFの表示

以下の例では、指定されたPDFファイルの最初のページを画像として表示している。

<syntaxhighlight lang="c++">
#include <QCoreApplication>
#include <QLabel>
#include <QImage>
#include <poppler-qt5.h>

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   Poppler::Document *document = Poppler::Document::load("input.pdf");
   if (!document || document->isLocked()) {
      delete document;
      return -1;
   }

   Poppler::Page *page = document->page(0);
   if (page == nullptr) {
      delete document;
      return -1;
   }

   QImage image = page->renderToImage(72.0, 72.0);

   QLabel label;
   label.setPixmap(QPixmap::fromImage(image));
   label.show();

   delete page;
   delete document;

   return a.exec();
}
</syntaxhighlight>


PDFの操作

以下の例では、PDFファイルのページ数の取得、メタデータの読み取り、特定のページからのテキストを抽出している。

<syntaxhighlight lang="c++">
#include <QCoreApplication>
#include <poppler-qt5.h>
#include <QDebug>

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   Poppler::Document *document = Poppler::Document::load("input.pdf");
   if (!document || document->isLocked()) {
      qDebug() << "Could not open PDF file";
      delete document;
      return -1;
   }

   // ページ数の取得
   int pageCount = document->numPages();
   qDebug() << "Number of pages:" << pageCount;

   // メタデータの読み取り
   qDebug() << "Title:" << document->info("Title");
   qDebug() << "Author:" << document->info("Author");
   qDebug() << "Subject:" << document->info("Subject");
   qDebug() << "Keywords:" << document->info("Keywords");
   qDebug() << "Creator:" << document->info("Creator");
   qDebug() << "Producer:" << document->info("Producer");
   qDebug() << "Creation date:" << document->info("CreationDate");
   qDebug() << "Modification date:" << document->info("ModDate");

   // 特定のページのテキスト抽出(例:最初のページ)
   if (pageCount > 0) {
      Poppler::Page *page = document->page(0);
      if (page) {
         QString text = page->text(QRectF());
         qDebug() << "Text from first page:" << text;
         delete page;
      }
   }

   delete document;

   return 0;
}
</syntaxhighlight>


その他 : PDFを画像に変換

<syntaxhighlight lang="c++">
#include <QCoreApplication>
#include <QPainter>
#include <poppler-qt5.h>

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   // PDFファイルを読み込む
   Poppler::Document *document = Poppler::Document::load("input.pdf");
   if (!document || document->isLocked()) {
      delete document;
      return -1;
   }

   // 最初のページを取得
   Poppler::Page *page = document->page(0);
   if (page == nullptr) {
      delete document;
      return -1;
   }

   // ページを画像として描画
   QImage image = page->renderToImage(72.0, 72.0);

   // 画像を保存
   image.save("output.png");

   delete page;
   delete document;

   return 0;
}
</syntaxhighlight>