/* 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()); }