文檔半島外圍網(wǎng)上直營(yíng)>>E-iceblue中文文檔>>在C#中獲取word文檔中文本的高度和寬度
                在C#中獲取word文檔中文本的高度和寬度
通過使用 Spire.Doc,開發(fā)人員可以找到并突出顯示文本,提取word 文檔中的文本。本文將向您展示如何借助 Spire.Doc 在 C# 中獲取 word 文檔中文本的高度和寬度。
首先,下載并安裝 Spire.Doc for .NET,然后通過以下路徑在下載的 Bin 文件夾中添加 Spire.Doc.dll 作為參考:“..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll ”。下面詳細(xì)介紹如何在C#中獲取word文檔中文本的高度和寬度。
首先查看原word文檔:
  
 
	
第 1 步:創(chuàng)建一個(gè)新文檔并從文件加載。
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 步:獲取文本字符串并測(cè)量字符串。
//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();
}
}
}                
            
 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號(hào)
渝公網(wǎng)安備
            50010702500608號(hào)
             
            
 客服熱線
客服熱線