翻譯|使用教程|編輯:李顯亮|2021-07-20 11:11:29.643|閱讀 936 次
概述:在本文中,我們將了解如何在 C# 中從DXF文件中提取文本。通過從各種類型的實體中提取文本,可以輕松完成 C# 中的 DXF 到 TXT 轉換。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在本文中,我們將了解如何在 C# 中從DXF文件中提取文本。通過從各種類型的實體中提取文本,可以輕松完成 C# 中的 DXF 到 TXT 轉換。在本教程中,我們僅從 CadText 實體中提取文本。但是,其他實體的過程也相同。
Aspose.CAD for .NET允許開發(fā)者AutoCAD DWG和DXF文件轉換成PDF和光柵圖像。
在 DXF CAD 圖形格式中,文本僅存儲在實體內(nèi)部;例如 CadText、CadMText、CadInsertObject、CadAttDef 或 CadAttrib 等。因此要提取文本,您需要首先獲取每個實體,然后從中檢索文本。為簡單起見,我們在本教程中從 CadText 實體中提取文本。但是您可以對其他類型的實體使用相同的方法和代碼,只需將對象轉換為特定的實體類型即可。
代碼如下:
using System; using System.IO; //Add reference to Aspose.CAD for .NET API //Use following namespaces to extract text from DXF file using Aspose.CAD; using Aspose.CAD.FileFormats.Cad; using Aspose.CAD.FileFormats.Cad.CadObjects; using Aspose.CAD.FileFormats.Cad.CadConsts; namespace ExtractTextFromDXFFile { class Program { static void Main(string[] args) { //Set Aspose license before extracting text from DXF file format //using Aspose.CAD for .NET Aspose.CAD.License AsposeCADLicense = new Aspose.CAD.License(); AsposeCADLicense.SetLicense(@"c:\asposelicense\license.lic"); CadImage DXFCADImagFile = (CadImage) Image.Load("InputDXFCADImagingFile.dxf"); //string to store extracted text from the DXF file string TextExtractedFromDXFFile = ""; // Search for text in the entities section foreach (CadBaseEntity CadEntity in DXFCADImagFile.Entities) { if (CadEntity.TypeName == CadEntityTypeName.TEXT) { CadText CadTextObject = (CadText)CadEntity; TextExtractedFromDXFFile += CadTextObject.DefaultValue; } } //Save the text extracted from DXF file using File Stream FileStream FStream = new FileStream("ExtractTextFromDXFFormat.txt", FileMode.Create); StreamWriter SWriter = new StreamWriter(FStream); //Write extracted text to the file SWriter.WriteLine(TextExtractedFromDXFFile); SWriter.Flush(); //Close file objects SWriter.Close(); FStream.Close(); } } }
在上面從 DXF 文件格式中提取文本的 C# 代碼示例中,我們將提取的文本保存在一個字符串變量中,最后使用文件流將所有提取的文本保存在一個 TXT 文件中。在從 CAD 文件格式中提取文本的整個過程中,您不需要在您的系統(tǒng)上安裝 AutoCAD,并且該 API 在任何 C# 應用程序(包括 Web、桌面和 ASP.NET 等)中都可以在沒有 AutoCAD 的情況下工作。
獲取有關從 DXF 或其他 CAD 文件格式中提取文本和實體的更多問題,請在Aspose免費支持論壇中提問哦 。或者加入Aspose資源分享交流群(761297826)探討。
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn