当前位置: 首页 > 工具软件 > iTextSharp > 使用案例 >

iTextSharp

屠德宇
2023-12-01

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using iTextSharp;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
//itextsharp 获取文本
//https://www.nuget.org/packages/iTextSharp/5.5.13.1
//Install-Package iTextSharp -Version 5.5.13.1
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string txt = GetTextFromAllPages(AppDomain.CurrentDomain.BaseDirectory + "Ajax.in.Action.pdf");
            Console.WriteLine(txt);
            Console.ReadLine();
        }
        public static string GetTextFromAllPages(String pdfPath)
        {
            PdfReader reader = new PdfReader(pdfPath);

            StringWriter output = new StringWriter();

            for (int i = 1; i <= reader.NumberOfPages; i++)
                output.WriteLine(PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy()));

            return output.ToString();
        }
    }
}
 

 类似资料: