How to Convert DWG to JPG using Java

This short tutorial provides details on how to convert DWG to JPG using Java. You can export DWG to JPG using Java by setting different properties like the height and width of image, background color, etc. and then create the output image file. You can also save the output to stream as well that you can further use in your application.

Steps to Convert DWG to JPG using Java

  1. Configure your application by adding the Aspose.CAD library from the Maven Repository
  2. Load the input DWG file with the Image class
  3. Create an instance of CadRasterizationOptions class
  4. Save the output JPG file

In this step-by-step tutorial to generate JPG from DWG in Java, firstly we load the input DWG file using the Image class object and then specify different properties for the rasterization options including image dimensions. Resultantly, the output JPG file is created by following all these simple steps.

Code to Convert DWG to JPG using Java

import com.aspose.cad.License;
import com.aspose.cad.Color;
import com.aspose.cad.Image;
import com.aspose.cad.ImageOptionsBase;
import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.JpegOptions;
public class ConvertDWGtoJPG {
public static void main(String[] args) throws Exception { // main method to convert DWG to JPG image using Java
// Set Aspose.CAD license before converting DWG to PNG Image
License CADLicenseJava = new License();
CADLicenseJava.setLicense("CADJavaLicense.lic");
// Load the DWG to export to JPG
Image image = Image.load("Test.dwg");
// Create an instance of CadRasterizationOptions
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
// Set page width & height
rasterizationOptions.setPageWidth(1200);
rasterizationOptions.setPageHeight(1200);
//Set background color and object colors
rasterizationOptions.setBackgroundColor(Color.getWhite());
rasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
// Create an instance of JpegOption for the converted Jpeg image
ImageOptionsBase options = new JpegOptions();
// Set rasterization options for exporting to JPEG
options.setVectorRasterizationOptions(rasterizationOptions);
// Save DWG to JPEG image
image.save("Exported_image_out.jpeg", options);
}
}

In the above code, the Image class object is used to load the source DWG file which can be used to save it as JPG image with desired background color and picture height or width. Likewise, you can render the output as a PNG image as well by initializing PngOptions class object and changing the extension for the output file name.

In addition to this feature to create JPG from DWG in Java you can export different type of files like explained in the article on how to convert OTG to PDF in Java. Please note that you do not need to install any other third-party tool or software for working with all these features.

 English