/* Antenna House PDF Tool API V7.0 .NET Interface sample program 概要:テキスト抽出 Copyright 2018-2021 Antenna House, Inc. */ using System; using PdfTkNet; namespace ExtractText { class Program { static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("usage: ExtractText.exe in-pdf-file"); return; } try { using (PtlParamInput inputFile = new PtlParamInput(args[0])) //元PDF using (PtlPDFDocument doc = new PtlPDFDocument()) { // PDFファイルをロードします。 doc.load(inputFile); using (PtlPages pages = doc.getPages()) { //ページコンテナが空かどうか if (pages.isEmpty()) { Console.WriteLine("ページコンテナが空"); return; } using (PtlPage page = pages.get(0)) // 先頭ページ { // テキスト抽出 ExtractText(page); } } } } catch (PtlException pex) { Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP()); pex.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("-- 完了 --"); } } static void ExtractText(PtlPage page) { using (PtlSize pageSize = page.getSize()) // ページサイズの取得 using (PtlContent content = page.getContent()) // ページコンテントの取得 using (PtlParamExtractText paramExtractText = new PtlParamExtractText()) // テキスト抽出パラメータ using (PtlRect rect = new PtlRect(0.0f, 0.0f, pageSize.getWidth() / 2, pageSize.getHeight() / 2)) // テキスト抽出範囲矩形 { //paramExtractText.appendRect(rect); // この行のコメントを外すと矩形領域内のテキストを抽出 // テキスト抽出 String text = content.extractText(paramExtractText); Console.WriteLine(text); } } } }