当前位置:   article > 正文

iTextSharp 读取pdf_itextsharp reader.getpagecontent(i)

itextsharp reader.getpagecontent(i)

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;

            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);

                StringBuilder sb = new StringBuilder();
               
                //取得每一页的字节数组,将每一个字节转换为字符,并将数组转换为字符串
                for (int j = 0; j < b.Length; j++) sb.Append(Convert.ToChar(b[j]));
                str = sb.ToString();

                //循环寻找"["和"]",直到找不到"["为止
                while (bg > -1)
                {
                    //取得下一个"["和"]"的位置
                    bg = str.IndexOf("[", ed);
                    ed = str.IndexOf("]", bg + 1);

                    //如果没有下一个"["就跳出循环
                    if (bg == -1) break;

                    //取得一个"[]"里的内容,将开始寻找"("和")"的位置初始为0
                    subStr = str.Substring(bg + 1, ed - bg - 1);
                    subbg = 0;
                    subed = 0;

                    //循环寻找下一个"("和")",直到没有下一个"("就跳出循环
                    while (subbg > -1)
                    {
                        //取得下一对"()"的位置
                        subbg = subStr.IndexOf("(", subed);
                        subed = subStr.IndexOf(")", subbg + 1);

                        //如找不到下一对就跳出
                        if (subbg == -1) break;
                        //在返回字符串后面加上新找到的字符串
                        rtStr += subStr.Substring(subbg + 1, subed - subbg - 1);
                    }
                }
            }

            //PDF文档中读出来的数据没有换行符,可以根据需要把2个或3个连续的空格改成换行符
            rtStr = rtStr.Replace("  ", "/r/n");

            return rtStr;
        }       

    }
}
读取一个24页的文件, 这个文件中不包含中文(包含中文的还需要支持中文的插件), 以下是第一页读取的字节数组:
BT
/F1 12 Tf
1 0 0 1 70.944 758.74 Tm
0 g
0 G
[(Re)6(ad)-3(in)-5(g 1)] TJ
ET
BT
1 0 0 1 122.66 758.74 Tm
[( )] TJ
ET
BT
/F2 10.56 Tf
1 0 0 1 70.944 743.86 Tm
[( )] TJ
ET
BT
/F3 12 Tf
1 0 0 1 70.944 727.78 Tm
[(W)103(arming)] TJ
ET
BT
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
  

闽ICP备14008679号