itextsharp-all-5.1.2.zip點擊下載 引入itextsharp.dll
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using iTextSharp;using iTextSharp.text;using iTextSharp.text.pdf;using iTextSharp.text.xml;namespace PDFR{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //退出程式的按鈕 private void button2_Click(object sender, EventArgs e) { Application.Exit(); } //開始運行程式的按鈕 private void button1_Click(object sender, EventArgs e) { string pdfStr = readPDF(檔案名稱); System.IO.File.WriteAllText(檔案名稱, pdfStr); } private string readPDF(string fn) { PdfReader p = new PdfReader(fn); //從每一頁讀出的字串 string str = String.Empty; //"[......]"內部字串 string subStr = String.Empty; //函數返回的字串 string rtStr = String.Empty; //從每一頁讀出的8位位元組數組 byte[] b = new byte[0]; //"[","]","(",")"在字串中的位置 Int32 bg = 0, ed = 0, subbg = 0, subed = 0; //取得文檔總頁數 int pg = p.NumberOfPages; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= pg; i++) { bg = 0; ed = 0; Array.Resize(ref b, 0); //取得第i頁的內容 b = p.GetPageContent(i); //下一行是把每一頁的取得的位元組資料寫入一個txt的檔案,僅供研究時用 //System.IO.File.WriteAllBytes(Application.StartupPath + "//P" + i.ToString() + ".txt", b); //取得每一頁的位元組數組,將每一個位元組轉換為字元,並將數群組轉換為字串 for (int j = 0; j < b.Length; j++) sb.Append(Convert.ToChar(b[j])); str = sb.ToString(); } return str; } } }
只返回str寫進text中
1 g/GS1 gs0 818 360 12 refBT/F1 1 Tf12 0 0 12 0 820.3403 Tm0 g0 Tc0 Tw( DELIVERY AUTHORISATION)TjET1 g0 806 561.6 12 refBT12 0 0 12 0 808.3403 Tm0 g( PAGE 32)TjET
只有()中的內容和pdf中一樣,所以要截取()中的內容,一個()是一行
另一種情況:
BT/F1 12 Tf1 0 0 1 70.944 758.74 Tm0 g0 G[(Re)6(ad)-3(in)-5(g 1)] TJETBT1 0 0 1 122.66 758.74 Tm[( )] TJET
一個[ ]是一行
參考:http://blog.csdn.net/dkchg/article/details/4575076