PDFiumを語るのに日本語はいらない。プログラマーはプログラム言語で語る。PDFiumの処理は、tryとfinallyのところだけを見てくれればよい。たったこれだけでPDFからテキストを抽出できるのだ。しかもかなり処理が速いぞ。
// テキスト抽出 private void button3_Click(object sender, EventArgs e) { // 結果コントロール初期化 UpdateResult("", ""); Update(); // Waitカーソル Cursor preCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; // 時間計測開始 System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); bool stat = true; string text = ""; string strSrc = textBox1.Text; int pos = strSrc.LastIndexOf('.'); string strDst = strSrc.Substring(0, pos) + "[text]" + ".txt"; try { pdfDoc = PdfDocument.Load(strSrc); for (int i = 0; i < pdfDoc.PageCount; i++) text += pdfDoc.GetPdfText(i); if (text != null) { Encoding sjisEnc = Encoding.GetEncoding("Shift_JIS"); using (StreamWriter writer = new StreamWriter(strDst, false, sjisEnc)) { writer.WriteLine(text); } } } catch { UpdateResult("失敗", "PDFiumで例外エラー"); stat = false; } finally { if (stat) UpdateResult("成功", ""); Cursor.Current = preCursor; // Waitカーソル解除 if (!pdfDoc.Equals(null)) { pdfDoc.Dispose(); // 明示的に破棄する必要あり(PDFがロックされてしまう) pdfDoc = null; } } // 時間計測終了 sw.Stop(); TimeSpan ts = sw.Elapsed; int t1 = (int)ts.TotalMilliseconds; double t2 = t1 / 1000.0; textBox5.Text = t2.ToString(); }