Export Images to DXF Format Using Aspose.CAD for Java

Introduction

Welcome to a comprehensive tutorial on exporting images to DXF format using Aspose.CAD for Java. Aspose.CAD is a powerful Java library that allows developers to work with CAD drawings programmatically. In this tutorial, we’ll walk you through the process of exporting images to DXF format, demonstrating various steps and techniques to achieve this task.

Prerequisites

Before you begin, make sure you have the following:

  • Basic understanding of Java programming.
  • Aspose.CAD for Java library installed. You can download it here.
  • A valid license or temporary license for Aspose.CAD. Obtain it here.
  • Some sample images in DXF format for testing.

Import Namespaces

In your Java project, import the necessary namespaces for Aspose.CAD:

import com.aspose.cad.Image;

import com.aspose.cad.fileformats.cad.CadImage;
import com.aspose.cad.fileformats.cad.cadconsts.CadEntityTypeName;
import com.aspose.cad.fileformats.cad.cadobjects.CadBaseEntity;
import com.aspose.cad.fileformats.cad.cadobjects.CadText;
import com.aspose.cad.fileformats.cad.cadtables.CadStyleTableObject;
import java.io.File;
import static java.lang.System.in;

Step 1: Set New Font per Document

// The path to the resource directory.
String dataDir = "Your Document Directory" + "DXFDrawings/";

File[] files = new File(dataDir).listFiles();
for (File file : files) {
    String extension = GetFileExtension(file);
    if (extension.equals(".dxf")) {
        CadImage cadImage = (CadImage)Image.load(file.getName());
        for (Object style : cadImage.getStyles()) {
            ((CadStyleTableObject)style).setPrimaryFontName("Broadway");
        }
        cadImage.save(file.getName() + "_font.dxf");
    }
}

Step 2: Hide All “Straight” Lines

CadImage cadImageEntity = (CadImage)Image.load(file.getName());
for (CadBaseEntity entity : cadImageEntity.getEntities()) {
    if (entity.getTypeName() == CadEntityTypeName.LINE) {
        entity.setVisible((short)0);
    }
}
cadImageEntity.save(file.getName() + "_lines.dxf");

Step 3: Manipulations with Text

CadImage cadImageText = (CadImage)Image.load(file.getName());
for (CadBaseEntity entity : cadImageText.getEntities()) {
    if (entity.getTypeName() == CadEntityTypeName.TEXT) {
        ((CadText)entity).setDefaultValue("New text here!!! :)");
        break;
    }
}
cadImageText.save(file.getName() + "_text.dxf");

Repeat these steps for each DXF file in your directory.

Conclusion

Congratulations! You’ve successfully learned how to export images to DXF format using Aspose.CAD for Java. This tutorial covered essential steps, including setting fonts, hiding lines, and manipulating text within CAD images.

FAQ’s

Q1: Can I use Aspose.CAD for Java without a license?

A1: You can use it with a temporary license available here.

Q2: Where can I find Aspose.CAD documentation?

A2: The documentation is available here.

Q3: How do I get support for Aspose.CAD?

A3: Visit the support forum here.

Q4: Where can I download Aspose.CAD for Java?

A4: Download the library here.

Q5: Is there a free trial available?

A5: Yes, you can get a free trial here.