翻譯|使用教程|編輯:胡濤|2022-08-02 11:22:31.083|閱讀 279 次
概述:本文將向您展示如何借助 Spire.Doc 在 C# 中獲取 word 文檔中文本的高度和寬度。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
通過使用 Spire.Doc,開發人員可以找到并突出顯示文本,提取word 文檔中的文本。本文將向您展示如何借助 Spire.Doc 在 C# 中獲取 word 文檔中文本的高度和寬度。
首先,下載并安裝 Spire.Doc for .NET,然后通過以下路徑在下載的 Bin 文件夾中添加 Spire.Doc.dll 作為參考:“..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll ”。下面詳細介紹如何在C#中獲取word文檔中文本的高度和寬度。
首先查看原word文檔:
第 1 步:創建一個新文檔并從文件加載。
Document doc = new Document(); doc.LoadFromFile("Word.doc");
第 2 步:定義我們需要獲取高度和寬度的文本字符串。
string text = "Microsoft Word is a word processor designed by Microsoft.";string text = "Microsoft Word is a word processor designed by Microsoft.";
第 3 步:獲取文本字符串并測量字符串。
//finds and returns the string with formatting TextSelection selection = doc.FindString(text, true, true); //get the font Font font = selection.GetAsOneRange().CharacterFormat.Font; //initialize graphics object Image fakeImage = new Bitmap(1, 1); Graphics graphics = Graphics.FromImage(fakeImage); //measure string SizeF size = graphics.MeasureString(text, font);
第 4 步:獲取文本高度和寬度并閱讀。
Console.WriteLine("text width:{0}", size.Width); Console.ReadLine();
有效截圖:
Console.WriteLine("text height:{0}",size.Height); Console.WriteLine("text width:{0}", size.Width); Console.ReadLine();
完整代碼:
using Spire.Doc; using Spire.Doc.Documents; using System; using System.Drawing; namespace GetHeightandWidth { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("Word.doc"); string text = "Microsoft Word is a word processor designed by Microsoft."; TextSelection selection = doc.FindString(text, true, true); Font font = selection.GetAsOneRange().CharacterFormat.Font; Image fakeImage = new Bitmap(1, 1); Graphics graphics = Graphics.FromImage(fakeImage); SizeF size = graphics.MeasureString(text, font); Console.WriteLine("text height:{0}", size.Height); Console.WriteLine("text width:{0}", size.Width); Console.ReadLine(); } } }
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn