/* IDE: VS 2022 17.5 OS: windows 10 .net: 8.0 生成PDF文档,从PDF文档中获取文字内容 控制台下测试 */ // See https://aka.ms/new-console-template for more information using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using UglyToad.PdfPig; using UglyToad.PdfPig.AcroForms; using UglyToad.PdfPig.AcroForms.Fields; using UglyToad.PdfPig.Content; using UglyToad.PdfPig.Outline; using System.IO; using UglyToad.PdfPig.Core; using UglyToad.PdfPig.Fonts.Standard14Fonts; using UglyToad.PdfPig.Fonts.SystemFonts; using UglyToad.PdfPig.Writer; using System.Drawing; using System.Drawing.Text; Console.WriteLine("Hello,CSharp World! Geovin Du,geovindu, 涂聚文\n\t"); try { PdfDocumentBuilder builder = new PdfDocumentBuilder(); //string fontfile = Server.MapPath("fonts/MHeiHK-Light.TTF"); //byte[] robotoBytes = File.ReadAllBytes(fontfile); // PdfDocumentBuilder.AddedFont MHeiHK = builder.AddTrueTypeFont(robotoBytes); // 读取宋体字体文件到字节数组 中文必须是中文字体,相应文字语言,用相关的字体 simsunb.ttf byte[] simSunFontBytes; using (FileStream fontFileStream = File.OpenRead("C:\\Windows\\Fonts\\STSONG.TTF")) { simSunFontBytes = new byte[fontFileStream.Length]; fontFileStream.Read(simSunFontBytes, 0, simSunFontBytes.Length); } // 添加支持中文的字体 PdfDocumentBuilder.AddedFont font = builder.AddTrueTypeFont(simSunFontBytes); PdfDocumentBuilder.AddedFont helvetica = builder.AddStandard14Font(Standard14Font.Helvetica); PdfDocumentBuilder.AddedFont helveticaBold = builder.AddStandard14Font(Standard14Font.HelveticaBold); // PdfDocumentBuilder.AddedFont song = builder.AddStandard14Font(Standard14Font.simsunb); PdfPageBuilder page = builder.AddPage(PageSize.A4); PdfPoint closeToTop = new PdfPoint(15, page.PageSize.Top - 25); page.AddText("My first PDF document!", 12, closeToTop, helvetica); page.AddText("Hello CSharp World!,Geovin Du!", 10, closeToTop.Translate(0, -15), helveticaBold); page = builder.AddPage(PageSize.A4); page.AddText("geovindu!", 12, closeToTop, helvetica); //中文用中文系统字体 page = builder.AddPage(PageSize.A4); //写入 page.AddText("你好,这是一个PDF文档。涂聚文欢迎你!", 12, new PdfPoint(25, 520), font); //byte[] b = builder.Build(); string fiel = "file.pdf"; File.WriteAllBytes(fiel, builder.Build()); Console.WriteLine("文档生成ok\n\t"); //从PDF文件中读取文字内容 string fileout ="1.pdf"; using (PdfDocument document = PdfDocument.Open(fileout)) { foreach (UglyToad.PdfPig.Content.Page pagedu in document.GetPages()) { IEnumerable<Word> words = pagedu.GetWords(); foreach (Word word in words) { Console.WriteLine(word.Text); } } } Console.WriteLine("\n\t从PDF文件中读取文字内容ok"); } catch(Exception ex) { Console.WriteLine(ex.Message.ToString()); }
标签:int,builder,UglyToad,System,net8,PdfPig,using,page From: https://www.cnblogs.com/geovindu/p/17990393