「Qtの基礎 - PDF」の版間の差分

ナビゲーションに移動 検索に移動
編集の要約なし
編集の要約なし
37行目: 37行目:
<br><br>
<br><br>


== PDFの作成 ==
== Qtライブラリ ==
==== QPdfWriterライブラリ ====
==== PDFの作成 ====
Qtでは、簡単にPDFを作成する機能が用意されている。<br>
Qtでは、簡単にPDFを作成する機能が用意されている。<br>
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
76行目: 76行目:
  painter.end();
  painter.end();
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
== Popplerライブラリ ==
Popplerライブラリは、QPdfWriterライブラリと比較して、より高度なPDF操作や読み込みに適している。<br>
<br>
<br>
==== Poppler-Qt5ライブラリ ====
==== PDFの作成 ====
Poppler-Qt5は、QPdfWriterライブラリと比較して、より高度なPDF操作や読み込みに適している。<br>
以下の例では、HTMLコンテンツを含むPDFファイルを作成している。<br>
<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("<h1>Hello, PDF!</h1>"
                "<p>This is a sample PDF created with Poppler and Qt.</p>");
    doc.setPageSize(writer.pageRect().size());
    doc.drawContents(&painter);
    painter.end();
    return 0;
}
</syntaxhighlight>
<br>
==== PDFの表示 ====
以下の例では、指定されたPDFファイルの最初のページを画像として表示している。<br>
<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>
<br>
==== PDFの操作 ====
以下の例では、PDFファイルのページ数の取得、メタデータの読み取り、特定のページからのテキストを抽出している。<br>
<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>
<br>
==== その他 : PDFを画像に変換 ====
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  #include <QCoreApplication>
  #include <QCoreApplication>

案内メニュー