PDF ツール C++でPDFファイルを読む方法 Curtis Chau 更新日:7月 28, 2025 Download IronPDF NuGet Download テキストの検索と置換 テキストと画像のスタンプ Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 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 C++ ステップ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 } C++ コードの説明 上記のコードでは、入力PDFファイルのパスを保持するためのpdfPath変数を定義します。実際の入力PDFドキュメントの適切なパスに置き換えてください。 Xpdfによって生成される出力テキストファイルのパスを保持するためのoutputFilePath変数も定義します。 コードはsystem関数を使用してpdftotextコマンドを実行し、コマンドライン引数として入力PDFファイルパスと出力テキストファイルパスを渡します。 status変数はコマンドの終了ステータスをキャプチャします。 pdftotextが正常に実行された場合(ステータスが0で示される)、出力テキストファイルをifstreamを使用して開きます。 次に、テキストコンテンツを行ごとに読み取り、textContent文字列に格納します。 最終的に、生成された出力ファイルからコンソールに抽出されたテキストコンテンツを出力します。 編集可能な出力テキストファイルが不要な場合やディスクスペースを解放したい場合は、プログラムの終了前に次のコマンドを使用して削除します。 remove(outputFilePath.c_str()); remove(outputFilePath.c_str()); C++ ステップ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... } ' Import necessary namespaces Imports IronPdf ' For PDF functionalities Imports IronSoftware.Drawing ' For handling images Imports System.Collections.Generic ' For using the List ' Example of extracting text and images from PDF using IronPDF ' Open a 128-bit encrypted PDF Private pdf = PdfDocument.FromFile("encrypted.pdf", "password") ' Get all text from the PDF Private text As String = pdf.ExtractAllText() ' Extract all images from the PDF Private allImages = pdf.ExtractAllImages() ' Iterate over each page to extract text and images For index = 0 To pdf.PageCount - 1 Dim pageNumber As Integer = index + 1 text = pdf.ExtractTextFromPage(index) Dim images As List(Of AnyBitmap) = pdf.ExtractBitmapsFromPage(index) ' Perform actions with text and images... Next index $vbLabelText $csharpLabel PDFドキュメントの読み取り方法の詳細な情報については、IronPDF C# PDFリーディングガイドをご覧ください。 結論 この記事では、Xpdfコマンドラインツールを使用してC++でPDFドキュメントの内容を読む方法を学びました。 XpdfをC++プログラムに統合することで、PDFファイルからテキストコンテンツをプログラムで数秒で抽出できます。 このアプローチにより、C++アプリケーション内で抽出されたテキストを処理および分析することが可能になります。 IronPDFは、PDFファイルを読み取りおよび操作することを容易にする強力なC#ライブラリです。 その広範な機能、使いやすさ、および信頼性の高いレンダリングエンジンにより、C#プロジェクトでPDFドキュメントを扱う開発者に人気のある選択肢となっています。 IronPDFは開発のために無料であり、商業利用のための無償試用版を提供しています。 これを超えた場合は、商業目的で使用するためにライセンスを取得する必要があります。 Curtis Chau 今すぐエンジニアリングチームとチャット テクニカルライター Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。 関連する記事 更新日 6月 22, 2025 2025年の最高のPDF訂正ソフトウェアを発見する 2025年のトップPDF訂正ソリューションを探る。Adobe Acrobat Pro DC、Nitro PDF Pro、Foxit PDF Editor、およびPDF-XChange Editorを含みます。.NETでの自動訂正によるセキュリティとコンプライアンスの強化について学びます。 詳しく読む 更新日 6月 22, 2025 iPhone向けのベストPDFリーダー(無料&有料ツールの比較) この記事では、iPhoneの最高のPDFリーダーを調査し、IronPDFが最良の選択肢である理由を結論付けます。 詳しく読む 更新日 6月 26, 2025 Windows用のベスト無料PDFエディタ(無料&有料ツールの比較) この記事は、2025年に利用可能な最高の無料PDF編集ソフトを探り、最も強力で柔軟な選択肢であるIronPDFで締めくくります。 詳しく読む C++でPDFファイルを作成する方法C++でHTMLをPDFに変換する方法
更新日 6月 22, 2025 2025年の最高のPDF訂正ソフトウェアを発見する 2025年のトップPDF訂正ソリューションを探る。Adobe Acrobat Pro DC、Nitro PDF Pro、Foxit PDF Editor、およびPDF-XChange Editorを含みます。.NETでの自動訂正によるセキュリティとコンプライアンスの強化について学びます。 詳しく読む
更新日 6月 22, 2025 iPhone向けのベストPDFリーダー(無料&有料ツールの比較) この記事では、iPhoneの最高のPDFリーダーを調査し、IronPDFが最良の選択肢である理由を結論付けます。 詳しく読む
更新日 6月 26, 2025 Windows用のベスト無料PDFエディタ(無料&有料ツールの比較) この記事は、2025年に利用可能な最高の無料PDF編集ソフトを探り、最も強力で柔軟な選択肢であるIronPDFで締めくくります。 詳しく読む