MochiuWiki : SUSE, EC, PCB
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
Qtの基礎 - PDFのソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
Qtの基礎 - PDF
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== 概要 == Qtは、PDFファイルの作成、表示、操作のための複数のクラスとモジュールを提供している。<br> これらは主に、QtPrintSupportモジュールとQtGuiモジュールに含まれている。<br> <br> QtのPDFサポートは基本的なPDF作成や表示において基本的な機能に限られているため、より高度な操作が必要な場合は、追加のライブラリやツールの使用を推奨する。<br> <br> 例えば、QPdfWriterライブラリは小規模から中規模のPDFファイル生成に適している。<br> 大規模で複雑なPDFファイルの場合、または、複雑なPDF操作や高度な機能が必要な場合は、サードパーティ製ライブラリの使用を検討する必要がある。<br> <br><br> == 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> <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> <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> __FORCETOC__ [[カテゴリ:Qt]]
Qtの基礎 - PDF
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse