Extract Text from DGN file

I used the following code to extract text as mentioned in Aspose.CAD for Java · GitHub. But i am getting following error. Attached the files used for testing

Exception in thread “main” com.aspose.cad.internal.Exceptions.NullReferenceException: Object reference not set to an instance of an object.

DgnImage img = (DgnImage) Image.load(“template.dgn”);
String TextExtractedFromFile = “”;

// Search for text in the entities section
for (String CadEntity : img.getStrings())
{
TextExtractedFromFile += CadEntity + System.lineSeparator();
}

//Save the text extracted from DGN file using File Stream
FileStream FStream = new FileStream(dataDir + “ExtractTextFromFormat.txt”, FileMode.Create);
StreamWriter SWriter = new StreamWriter(FStream);
//Write extracted text to the file
SWriter.write(TextExtractedFromFile);
SWriter.flush();
//Close file objects
SWriter.close();
FStream.close();
mygeodata (3).zip (4.1 MB)

@raviraja.m47

It seems you are encountering a NullReferenceException while trying to extract text from a DGN file using Aspose.CAD. This error typically indicates that you are trying to access an object that has not been initialized.

To extract text from a DGN file, you should ensure that you are correctly accessing the text elements within the DGN image. The code you provided attempts to retrieve strings directly, which may not be the correct approach. Instead, you should iterate through the elements of the DGN image and specifically check for text elements.

Here’s a revised version of your code that demonstrates how to extract text from a DGN file by iterating through the elements:

DgnImage img = (DgnImage) Image.load("template.dgn");
StringBuilder textExtractedFromFile = new StringBuilder();

// Iterate through the elements in the DGN image
for (DgnElement element : img.getElements()) {
    if (element instanceof DgnTextElement) {
        DgnTextElement textElement = (DgnTextElement) element;
        textExtractedFromFile.append(textElement.getText()).append(System.lineSeparator());
    }
}

// Save the extracted text to a file
try (FileStream fStream = new FileStream(dataDir + "ExtractTextFromFormat.txt", FileMode.Create);
     StreamWriter sWriter = new StreamWriter(fStream)) {
    sWriter.write(textExtractedFromFile.toString());
}

In this code:

  • We check if each element is an instance of DgnTextElement before attempting to access its text.
  • We use a StringBuilder for efficient string concatenation.
  • The file writing is done within a try-with-resources statement to ensure proper resource management.

Make sure that your DGN file contains text elements, as the absence of such elements could also lead to a NullReferenceException. If you continue to face issues, consider checking the version of Aspose.CAD you are using, as there may be updates or changes in the API that could affect functionality.

If you need further assistance, feel free to ask!

@raviraja.m47,
Hello,
thank you for the details provided. We can confirm the issue and have created CADJAVA-11965 to fix it.

Please try if this is useful (tested with Aspose.CAD for Java 25.1):

String filename = "...";

final DgnImage dgnImage = (DgnImage)Image.load(filename);

for (DgnDrawableEntityBase element : dgnImage.getEntities())
{
	if (element instanceof DgnTextElement)
	{
		DgnTextElement textElement = (DgnTextElement) element;
		System.out.println(textElement.getText() + System.lineSeparator());
	}
}