使用Aspose.CAD如何在 C# 中从 DXF 文件中提取文本?

Aspose.CAD for .NET允许开发者AutoCAD DWG和DXF文件转换成PDF和光栅图像。

在 C# 中从 DXF 文件中提取文本的步骤
  1. 使用Aspose.CAD for .NET NuGet 包
  2. 在运行代码之前包含所需的命名空间。
  3. 使用 SetLicense 方法设置 Aspose 许可证
  4. 使用Image类加载 DXF 文件
  5. 将对象转换为CadImage类型
  6. 遍历所有实体以获取文本
  7. 检查CadText实体类型并获取 DefaultValue 属性
  8. 最后,将提取的文本输出保存为TXT文件

在 DXF CAD 图形格式中,文本仅存储在实体内部;例如 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 fileusing 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:asposelicenselicense.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 文件格式中提取文本的整个过程中,您不需要在您的系统上安装 AutoCAD,并且该 API 在任何 C# 应用程序(包括 Web、桌面和 ASP.NET 等)中都可以在没有 AutoCAD 的情况下工作。

获取有关从 DXF 或其他 CAD 文件格式中提取文本和实体的更多问题,请在Aspose免费支持论坛中提问哦 。或者加入Aspose资源分享交流群(761297826)探讨。

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年6月17日
下一篇 2021年6月17日

相关推荐

发表回复

登录后才能评论