How to Extract Text from DXF File in C#

In this step by step guide, we’ll see how to extract text from DXF file in C#. The DXF to TXT conversion in C# can be easily done by extracting text from Entities of various types. In this tutorial, we’re extracting text from CadText entity only. However, the process is same for other entities as well.

Steps to Extract Text from DXF File in C#

  1. Get Aspose.CAD for .NET package from NuGet.org
  2. Include the required namespaces before running the code
  3. Set Aspose license using SetLicense method
  4. Load DXF file using Image class
  5. Convert the object to CadImage type
  6. Loop through all the entities to fetch text
  7. Check for CadText entity type and get DefaultValue property
  8. Finally, save the extracted text output as TXT file

In DXF CAD graphics format, the text is stored inside the entities only; for example CadText, CadMText, CadInsertObject, CadAttDef, or CadAttrib etc. So to extract text, you need to first get each entity and then retrieve text from it. For the sake of simplicity, we’re extracting text from CadText entity in this tutorial. But you can use the same approach and code for other types of entities, just cast the object to particular entity type.

Code to Extract Text from DXF File in C#

In the above C# code sample to extract text from DXF file format, we keep the extracted text in a string variable and at the end save all the extracted text in a TXT file using file streams. In this whole process to extract text from CAD file formats, you do not need to have AutoCAD installed on your system and the API works without AutoCAD in any C# application including Web, Desktop, and ASP.NET etc.

 English