iTextSharp讀取PDF(一)

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.