C++でPDFファイルを読む方法
PDF(ポータブル・ドキュメント・フォーマット)ファイルは広く文書交換に使用されており、プログラムによってその内容を読み取ることができることは、様々なアプリケーションにおいて価値があります。 C++でPDFを読むために利用可能なライブラリには、Poppler、MuPDF、Haru無償PDFライブラリ、Xpdf、およびQpdfがあります。
この記事では、Xpdfコマンドラインツールを使用してC++でPDFファイルを読む方法を探ります。 Xpdfは、PDFファイルの作業に便利なユーティリティを幅広く提供しており、テキストコンテンツを抽出することができます。 C++プログラムにXpdfを統合することにより、PDFファイルからテキストを抽出し、プログラムによって処理することができます。
Xpdf - コマンドラインツール
Xpdfは、PDF(ポータブル・ドキュメント・フォーマット)ファイル用のツールとライブラリのコレクションを提供するオープンソースソフトウェアスイートです。 Xpdfスイートには、様々なPDF関連機能を実現するコマンドラインユーティリティとC++ライブラリが含まれており、解析、レンダリング、テキスト抽出などが可能です。 Xpdfの主要なコンポーネントには、pdfimages、pdftops、pdfinfo、およびpdftotextがあります。 ここでは、PDF文書を読むためにpdftotextを使用します。
pdftotextは、PDFファイルからテキストコンテンツを抽出し、それをプレーンテキストとして出力するコマンドラインツールです。 このツールは、PDFからテキスト情報を抽出してさらに処理や分析する必要がある場合に特に役立ちます。 オプションを使用すると、テキストを抽出するページやページの指定もできます。
前提条件
テキストを抽出するPDFリーダープロジェクトを作成するには、次の前提条件を整える必要があります。
- システムにGCCやClangなどのC++コンパイラがインストールされていること。 C++プログラミングをサポートする任意のIDEを使用できます。
- システムにインストールされたXpdfコマンドラインツール。 Xpdfは、Xpdfサイトから入手可能なPDFユーティリティのコレクションです。Xpdf Websiteからダウンロードしてください。 Xpdfのbinディレクトリを環境変数のパスに設定して、どこからでもコマンドラインツールを使用してアクセスできるようにします。
C++でPDFファイルフォーマットを読むための手順
ステップ1:必要なヘッダーのインクルード
まず、適切なヘッダーファイルをmain.cppファイルの先頭に追加します:
#include <cstdlib> // For system call
#include <iostream> // For basic input and output
#include <fstream> // For file stream operations#include <cstdlib> // For system call
#include <iostream> // For basic input and output
#include <fstream> // For file stream operationsステップ2:C++コードを書く
PDFドキュメントからテキストコンテンツを抽出するために、Xpdfコマンドラインツールを呼び出すC++コードを書きましょう。 以下のinput.pdfファイルを使用します。

コード例は次のとおりです:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Specify the input and output file paths
string pdfPath = "input.pdf";
string outputFilePath = "output.txt";
// Construct the command to run pdftotext
string command = "pdftotext " + pdfPath + " " + outputFilePath;
int status = system(command.c_str());
// Check if the command executed successfully
if (status == 0) {
cout << "Text extraction successful." << endl;
} else {
cout << "Text extraction failed." << endl;
return 1; // Exit the program with error code
}
// Open the output file to read the extracted text
ifstream outputFile(outputFilePath);
if (outputFile.is_open()) {
string textContent;
string line;
while (getline(outputFile, line)) {
textContent += line + "\n"; // Append each line to the textContent
}
outputFile.close();
// Display the extracted text
cout << "Text content extracted from PDF document:" << endl;
cout << textContent << endl;
} else {
cout << "Failed to open output file." << endl;
return 1; // Exit the program with error code
}
return 0; // Exit the program successfully
}#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Specify the input and output file paths
string pdfPath = "input.pdf";
string outputFilePath = "output.txt";
// Construct the command to run pdftotext
string command = "pdftotext " + pdfPath + " " + outputFilePath;
int status = system(command.c_str());
// Check if the command executed successfully
if (status == 0) {
cout << "Text extraction successful." << endl;
} else {
cout << "Text extraction failed." << endl;
return 1; // Exit the program with error code
}
// Open the output file to read the extracted text
ifstream outputFile(outputFilePath);
if (outputFile.is_open()) {
string textContent;
string line;
while (getline(outputFile, line)) {
textContent += line + "\n"; // Append each line to the textContent
}
outputFile.close();
// Display the extracted text
cout << "Text content extracted from PDF document:" << endl;
cout << textContent << endl;
} else {
cout << "Failed to open output file." << endl;
return 1; // Exit the program with error code
}
return 0; // Exit the program successfully
}コードの説明
上記のコードでは、入力PDFファイルのパスを保持するためのpdfPath変数を定義します。実際の入力PDFドキュメントの適切なパスに置き換えてください。
Xpdfによって生成される出力テキストファイルのパスを保持するためのoutputFilePath変数も定義します。
コードはsystem関数を使用してpdftotextコマンドを実行し、コマンドライン引数として入力PDFファイルパスと出力テキストファイルパスを渡します。 status変数はコマンドの終了ステータスをキャプチャします。
pdftotextが正常に実行された場合(ステータスが0で示される)、出力テキストファイルをifstreamを使用して開きます。 次に、テキストコンテンツを行ごとに読み取り、textContent文字列に格納します。
最終的に、生成された出力ファイルからコンソールに抽出されたテキストコンテンツを出力します。 編集可能な出力テキストファイルが不要な場合やディスクスペースを解放したい場合、プログラムの最後にmain関数を終了する前に、次のコマンドを使用してそれを削除します:
remove(outputFilePath.c_str());remove(outputFilePath.c_str());ステップ3:プログラムのコンパイルと実行
C++コードをコンパイルし、実行可能ファイルを実行します。 環境変数システムパスにpdftotextが追加されている場合、コマンドは正常に実行されます。 プログラムは出力テキストファイルを生成し、PDFドキュメントからテキストコンテンツを抽出します。 抽出されたテキストはその後コンソールに表示されます。
出力は次の通りです

C#でPDFファイルを読む
IronPDFライブラリ
IronPDFは、PDFドキュメントの操作に強力な機能を提供する人気のC# PDFライブラリです。 開発者がプログラムでPDFファイルを作成、編集、変更、および読み取ることを可能にします。
IronPDFライブラリを使用してPDF文書を読むことは、簡単なプロセスです。 ライブラリは様々なメソッドとプロパティを提供しており、開発者がPDFページからテキスト、画像、メタデータ、その他のデータを抽出することを可能にします。 抽出された情報はアプリケーション内でのさらなる処理、分析、または表示に使用できます。
次に示すコード例では、IronPDFを使ってPDFファイルを読みます:
// Import necessary namespaces
using IronPdf; // For PDF functionalities
using IronSoftware.Drawing; // For handling images
using System.Collections.Generic; // For using the List
// Example of extracting text and images from PDF using IronPDF
// Open a 128-bit encrypted PDF
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");
// Get all text from the PDF
string text = pdf.ExtractAllText();
// Extract all images from the PDF
var allImages = pdf.ExtractAllImages();
// Iterate over each page to extract text and images
for (var index = 0; index < pdf.PageCount; index++) {
int pageNumber = index + 1;
text = pdf.ExtractTextFromPage(index);
List<AnyBitmap> images = pdf.ExtractBitmapsFromPage(index);
// Perform actions with text and images...
}// Import necessary namespaces
using IronPdf; // For PDF functionalities
using IronSoftware.Drawing; // For handling images
using System.Collections.Generic; // For using the List
// Example of extracting text and images from PDF using IronPDF
// Open a 128-bit encrypted PDF
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");
// Get all text from the PDF
string text = pdf.ExtractAllText();
// Extract all images from the PDF
var allImages = pdf.ExtractAllImages();
// Iterate over each page to extract text and images
for (var index = 0; index < pdf.PageCount; index++) {
int pageNumber = index + 1;
text = pdf.ExtractTextFromPage(index);
List<AnyBitmap> images = pdf.ExtractBitmapsFromPage(index);
// Perform actions with text and images...
}PDFドキュメントの読み取り方法の詳細な情報については、IronPDF C# PDFリーディングガイドをご覧ください。
結論
この記事では、Xpdfコマンドラインツールを使用してC++でPDFドキュメントの内容を読む方法を学びました。 XpdfをC++プログラムに統合することで、PDFファイルからテキストコンテンツをプログラムで数秒で抽出できます。 このアプローチにより、C++アプリケーション内で抽出されたテキストを処理および分析することが可能になります。
IronPDFは、PDFファイルを読み取りおよび操作することを容易にする強力なC#ライブラリです。 その広範な機能、使いやすさ、および信頼性の高いレンダリングエンジンにより、C#プロジェクトでPDFドキュメントを扱う開発者に人気のある選択肢となっています。
IronPDFは開発のために無料であり、商業利用のための無償試用版を提供しています。 これを超えた場合は、商業目的で使用するためにライセンスを取得する必要があります。






