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

提供: MochiuWiki : SUSE, EC, PCB

ページの作成:「== 概要 == Qtにおいて、PDFを作成する手順を記載する。<br> <br><br> == PDFの作成 == Qtでは、簡単にPDFを作成する機能が用意されて…」
 
文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag…
 
(同じ利用者による、間の3版が非表示)
1行目: 1行目:
== 概要 ==
== 概要 ==
Qtにおいて、PDFを作成する手順を記載する。<br>
Qtは、PDFファイルの作成、表示、操作のための複数のクラスとモジュールを提供している。<br>
これらは主に、QtPrintSupportモジュールとQtGuiモジュールに含まれている。<br>
<br>
QtのPDFサポートは基本的なPDF作成や表示において基本的な機能に限られているため、より高度な操作が必要な場合は、追加のライブラリやツールの使用を推奨する。<br>
<br>
例えば、QPdfWriterライブラリは小規模から中規模のPDFファイル生成に適している。<br>
大規模で複雑なPDFファイルの場合、または、複雑なPDF操作や高度な機能が必要な場合は、サードパーティ製ライブラリの使用を検討する必要がある。<br>
<br><br>
<br><br>


== PDFの作成 ==
== QtのPDFモジュール ==
==== 主要なクラス ====
* QPdfWriterクラス
*: PDFファイルを作成するためのクラスである。
* QPrinterクラス
*: プリンタやPDFファイルへの出力を扱うクラスである。
* QPagedPaintDeviceクラス
*: ページベースのペイントデバイスの基本クラスである。
* QPdfDocumentクラス (Qt 5.10以降)
*: PDFドキュメントを読み込み、操作するためのクラスである。
<br>
==== PDF作成 ====
QPdfWriterクラス、または、QPrinterクラスを使用して、PDFファイルを作成することができる。<br>
QPainterクラスと組み合わせて使用することにより、テキスト、図形、画像などをPDFに描画できる。<br>
<br>
==== PDF表示 ====
Qt 5.10以降、Qt WidgetsではQPdfViewerウィジェットが提供されており、PDFファイルを表示することができる。<br>
<br>
==== PDF操作 ====
<code>QPdfDocument</code>クラスを使用して、PDFファイルのページ数の取得、ページの抽出、メタデータの読み取り等の操作が可能である。<br>
<br>
==== 印刷サポート ====
QtPrintSupportモジュールを使用することで、PDFファイルの作成だけでなく、実際のプリンタへの印刷も可能である。<br>
<br>
==== サードパーティ製ライブラリとの統合 ====
Qtは、Popplerライブラリ、MuPDFライブラリ等のサードパーティ製PDFライブラリと統合することも可能であり、より高度なPDF操作を行うことができる。<br>
<br><br>
 
== Qtライブラリ ==
==== PDFの作成 ====
Qtでは、簡単にPDFを作成する機能が用意されている。<br>
Qtでは、簡単にPDFを作成する機能が用意されている。<br>
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
42行目: 77行目:
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>
== Popplerライブラリ ==
Popplerライブラリは、QPdfWriterライブラリと比較して、より高度なPDF操作や読み込みに適している。<br>
<br>
==== PDFの作成 ====
以下の例では、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++">
#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>
<br><br>
{{#seo:
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux
|image=/resources/assets/MochiuLogo_Single_Blue.png
}}


__FORCETOC__
__FORCETOC__
[[カテゴリ:Qt]]
[[カテゴリ:Qt]]

2024年10月14日 (月) 10:58時点における最新版

概要

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>