DWG DXF to PDF C# | Convert Auto CAD Files to PDF JPEG PNG in C# .NET

Convert DWG or DXF to PNG JPEG BMP GIF or TIFF in C#

Aspose.CAD for .NET can convert AutoCAD drawing formats such as DXF and DWG to PNG, BMP, TIFF, JPEG and GIF. It has exposed efficient and easy to use API to achieve this goal.

You can convert any supported AutoCAD drawing format to raster image formats using the simple steps as elaborated below.

  1. Load the AutoCAD DWG or DXF file into the Image class.
  2. Create an instance of CadRasterizationOptions.
  3. Set/change the size of the image using PageWidth and PageHeight
  4. Create an instance of ImageOptionsBase
  5. Set VectorRasterizationOptions property to the CadRasterizationOptions created in the previous step.
  6. Save AutoCAD drawing as PDF with Image.Save by passing the file path (or an object of MemoryStream) as well as the instance of ImageOptionsBase created in the previous step.

Here is the complete source code.

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set page width & height
rasterizationOptions.PageWidth = 1200;
rasterizationOptions.PageHeight = 1200;
// Create an instance of PngOptions for the resultant image
ImageOptionsBase options = new Aspose.CAD.ImageOptions.PngOptions();
// Set rasterization options
options.VectorRasterizationOptions = rasterizationOptions;
MyDir = MyDir + "conic_pyramid_raster_image_out.png";
// Save resultant image
image.Save(MyDir, options);
}

By default, the API renders only the “Model” layout. However, you may also specify the layouts of your choice while converting CAD drawings to image formats.

Customizing CAD Conversion

The CAD to PDF & CAD to raster image conversion procedures are highly configurable because the CadRasterizationOptions class has been implemented in such a way that it provides many optional features that upon setting can override the rendering process according to the application needs.

CadRasterizationOptions Class

The CadRasterizationOptions class is common for all supported CAD formats such as DWG & DXF therefore, the information shared in this article is valid for both aforesaid CAD formats.

The most useful CadRasterizationOptions class properties are:

PropertyDefault ValueRequiredDescription
PageWidth0YesSpecifies the page width.
PageHeight0YesSpecifies the page height
ScaleMethodScaleType.ShrinkToFitNoSpecifies whether the drawing should be automatically scaled. The default value automatically shrinks the image to fit the canvas size. Switch to GrowToFit mode, or use the None setting to disable automatic scaling.
BackgroundColorColor.WhiteNoSpecifies the output image’s background color.
DrawTypeCadDrawTypeMode.UseDrawColorNoSpecifies the entity’s colorization mode. Specify the UseObjectColor option to draw entities using their native color, or the UseDrawColor option to over-ride native colors.
DrawColorColor.BlackNoSpecifies the overridden entity’s color (only if DrawType is set to the UseDrawColor property value).
AutomaticLayoutsScalingFalseNoSpecifies if auto-layout scaling has to be performed to match the Model layout.

Setting the Canvas Size & Mode

Export from CAD to PDF or CAD to raster image formats is not a trivial task. Since the resultant PDF or image requires the canvas size to be defined, we need to specify the output dimensions for the PDF page to render the drawing properly. Set the CadRasterizationOptions.PageWidth and CadRasterizationOptions.PageHeight properties explicitly, or you may get an ImageSaveException.

Additionally, you may specify dimension scale options. The scaling options are set by the CadRasterizationOptions.ScaleMethod property. Use this option to automatically adjust the image dimensions according to the CadRasterizationOptions.PageWidth and CadRasterizationOptions.PageHeight values. By default CadRasterizationOptions.ScaleMethod is set to ScaleType.ShrinkToFit mode. This property defines the following behavior:

  • If the CAD drawing dimensions are greater than the resultant canvas size, then the drawing dimensions are reduced to fit into the resultant canvas while preserving the aspect ratio.
  • If the CAD drawing dimensions are smaller than the resultant canvas size, set the CadRasterizationOptions.ScaleMethod property to ScaleType.GrowToFit in order to increase the drawing size to fit into the PDF canvas while preserving the aspect ratio.
  • Or disable automatic scaling with the ScaleType.None option.

The code sample below shows the auto-scaling option in use.

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
rasterizationOptions.PageWidth = 1600;
rasterizationOptions.PageHeight = 1600;
rasterizationOptions.AutomaticLayoutsScaling = true;
rasterizationOptions.NoScaling = false;
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to PDF
image.Save(MyDir + "result_out.pdf", pdfOptions);
// Create an instance of TiffOptions
Aspose.CAD.ImageOptions.TiffOptions tiffOptions = new Aspose.CAD.ImageOptions.TiffOptions(Aspose.CAD.FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
// Set the VectorRasterizationOptions property
tiffOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to TIFF
image.Save(MyDir + "result_out.tiff", tiffOptions);
}

Setting the Background & Drawing Colors

By default, the color palette for the resultant canvas is set to the common document scheme. That means that all entities inside the CAD drawing are drawn with a black color pen on a solid white background. These settings can be changed with CadRasterizationOptions.BackgroundColor and CadRasterizationOptions.DrawColor properties. Changing the CadRasterizationOptions.DrawColor property also requires setting the CadRasterizationOptions.DrawType property to make use of the drawing color to be used. The CadRasterizationOptions.DrawType property controls whether CAD entities preserve their colors or are converted to custom colors. To preserve entity colors, specify CadRasterizationOptions.DrawType as CadDrawTypeMode.UseObjectColor, otherwise specify the CadDrawTypeMode.UseDrawColor value.

The code sample below shows how to use different color properties.

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
rasterizationOptions.PageWidth = 1600;
rasterizationOptions.PageHeight = 1600;
rasterizationOptions.BackgroundColor = Aspose.CAD.Color.Beige;
rasterizationOptions.DrawType = Aspose.CAD.FileFormats.Cad.CadDrawTypeMode.UseDrawColor;
rasterizationOptions.DrawColor = Aspose.CAD.Color.Blue;
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to PDF
image.Save(MyDir + "result_out.pdf", pdfOptions);
// Create an instance of TiffOptions
Aspose.CAD.ImageOptions.TiffOptions tiffOptions = new Aspose.CAD.ImageOptions.TiffOptions(Aspose.CAD.FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
// Set the VectorRasterizationOptions property
tiffOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to TIFF
image.Save(MyDir + "result_out.tiff", tiffOptions);
}

Setting Auto Layout Scaling

Most of the CAD drawings have more than one layouts stored in a single file, and each layout could have different dimensions. While rendering such CAD drawings to PDF, each page of the PDF could have different scaling according to the layout size. In order to make the rendering homogeneous, the Aspose.CAD APIs have exposed CadRasterizationOptions.AutomaticLayoutsScaling property. Its default value is false but when true, the API will try to search for a corresponding scale for each separate layout and draw them in a corresponding manner by performing automatic re-size operation according to page size.

Here is how the CadRasterizationOptions.AutomaticLayoutsScaling property works in collaboration with CadRasterizationOptions.ScaleMethod property.

  1. If ScaleMethod is set to ScaleType.ShrinkToFit or ScaleType.GrowToFit with AutomaticLayoutsScaling set to false then all layouts (including the Model) will be processed according to the first option.
  2. If ScaleMethod is set to ScaleType.ShrinkToFit or ScaleType.GrowToFit with AutomaticLayoutsScaling set to true then all layouts (except Model) will be processed according to their size whereas the Model will be processed according to the first option.
  3. If ScaleMethod is set to ScaleType.None with AutomaticLayoutsScaling set to true or false then no scaling will be performed.

The code sample below shows how to set the auto layout scaling for CAD to PDF conversion.

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
rasterizationOptions.PageWidth = 1600;
rasterizationOptions.PageHeight = 1600;
// Set Auto Layout Scaling
rasterizationOptions.AutomaticLayoutsScaling = true;
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
MyDir = MyDir + "result_out.pdf";
//Export the CAD to PDF
image.Save(MyDir, pdfOptions);
}

Convert AutoCAD DXF or DWG Layouts to PNG or Other Image Formats in C#

Aspose.CAD for .NET API can convert CAD layouts of supported formats such as DXF and DWG to PNG BMP TIFF JPEG and GIF. The API also provides the support to render the specific layouts of a CAD drawing to different PSD layers.

Here is how you can achieve the same in the following simple steps.

  • Load the AutoCAD DWG or DXF file using Image class.
  • Set/change width and height of the image.
  • Set the desired layout name(s) using the CadRasterizationOptions.Layouts property.
  • Create an instance of ImageOptionsBase and set its VectorRasterizationOptions property to the instance of CadRasterizationOptions created in the previous step.
  • Save the CAD layout as TIFF or image.

Here is the complete source code.

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set page width & height
rasterizationOptions.PageWidth = 1200;
rasterizationOptions.PageHeight = 1200;
// Specify a list of layout names
rasterizationOptions.Layouts = new string[] { "Model", "Layout1" };
// Create an instance of TiffOptions for the resultant image
ImageOptionsBase options = new Aspose.CAD.ImageOptions.TiffOptions(Aspose.CAD.FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
// Set rasterization options
options.VectorRasterizationOptions = rasterizationOptions;
MyDir = MyDir + "conic_pyramid_layoutstorasterimage_out.tiff";
// Save resultant image
image.Save(MyDir, options);
}

Enabling Tracking for CAD Rendering Process

Aspose.CAD has introduced a series of classes and supporting enumeration fields to assist with the tracking of CAD rendering process. With these changes in place, the CAD to PDF conversion can now be achieved as follow while enabling the tracking.

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
MemoryStream stream = new MemoryStream();
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions cadRasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
pdfOptions.VectorRasterizationOptions = cadRasterizationOptions;
cadRasterizationOptions.PageWidth = 800;
cadRasterizationOptions.PageHeight = 600;
image.Save(stream, pdfOptions);
}

Tracking of CAD rendering process can detect the following possible problems.

  1. Missing or corrupted header information.
  2. Missing layout information.
  3. Missing block entities.
  4. Missing dimension styles.
  5. Missing styles.

Substituting Fonts while Converting CAD Drawings

It is quite possible that a particular CAD drawing uses some specific font that isn’t available on the machine where CAD to PDF or CAD to raster image conversion is taking place. In such situations, the Aspose.CAD API will trigger an appropriate exception to highlight the missing font(s) and stop the conversion process because the API requires these fonts to properly render the contents onto the resultant PDF or images.

Aspose.CAD API provides an easy way to use the mechanism to substitute the required font(s) with the available font(s). The CadImage.Styles property returns an instance of CadStylesDictionary that in turn contains the CadStyleTableObject for each style in the CAD drawing, whereas the CadStyleTableObject.PrimaryFontName can be used to specify the available font name.

The following code snippet demonstrates the usage of Aspose.CAD for .NET API to change the font of all styles in a CAD drawing.

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Iterate over the items of CadStyleDictionary
foreach (CadStyleTableObject style in cadImage.Styles)
{
// Set the font name
style.PrimaryFontName = "Arial";
}
}
Console.WriteLine("\nFont changed successfully.");
}
public static void SubstitutingFontByName()
{
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Iterate over the items of CadStyleDictionary
foreach (CadStyleTableObject style in cadImage.Styles)
{
if (style.StyleName == "Roman")
{
// Specify the font for one particular style
style.PrimaryFontName = "Arial";
}
}
}
}

It is also possible to change the font of only one particular style by accessing it via the style name. The following code snippet demonstrates the usage of this approach.

// For complete examples and data files, please go to https://github.com/aspose-cad/Aspose.CAD-for-.NET
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Iterate over the items of CadStyleDictionary
foreach (CadStyleTableObject style in cadImage.Styles)
{
if (style.StyleName == "Roman")
{
// Specify the font for one particular style
style.PrimaryFontName = "Arial";
}
}
}

Converting CAD Layers to Raster Image Formats

Aspose.CAD for .NET API has exposed an efficient & easy to use means to specify the name of the required CAD layer and render it to raster image formats. Here is how you can achieve the same in 5 simple steps as listed below.

  1. Load the CAD file into an instance of Image using the factory method Load.
  2. Create an instance of CadRasterizationOptions and set its mandatory properties such as PageWidth & PageHeight.
  3. Add the desired layer name using the CadRasterizationOptions.Layers.Add method.
  4. Create an instance of ImageOptionsBase and set its VectorRasterizationOptions property to the instance of CadRasterizationOptions created in the previous step.
  5. Call the Image.Save method by passing the file path (or an object of MemoryStream) as well as the instance of ImageOptionsBase created in the previous step.

Here is the complete source code.

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of Image
using (var image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
var rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set image width & height
rasterizationOptions.PageWidth = 500;
rasterizationOptions.PageHeight = 500;
// Add the layer name to the CadRasterizationOptions's layer list
rasterizationOptions.Layers= new string[] { "LayerA" };
// Create an instance of JpegOptions (or any ImageOptions for raster formats)
var options = new Aspose.CAD.ImageOptions.JpegOptions();
// Set VectorRasterizationOptions property to the instance of CadRasterizationOptions
options.VectorRasterizationOptions = rasterizationOptions;
//Export each layer to Jpeg format
MyDir = MyDir + "CADLayersToRasterImageFormats_out.jpg";
image.Save(MyDir, options);
}
Console.WriteLine("\nCAD layers converted successfully to raster image format.\nFile saved at " + MyDir);
}
public static void ConvertAllLayersToRasterImageFormats()
{
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (var image = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
var rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set image width & height
rasterizationOptions.PageWidth = 500;
rasterizationOptions.PageHeight = 500;
// Get the layers in an instance of CadLayersDictionary
var layersList = image.Layers;
// Iterate over the layers
foreach (var layerName in layersList.GetLayersNames())
{
// Display layer name for tracking
Console.WriteLine("Start with " + layerName);
// Add the layer name to the CadRasterizationOptions's layer list
rasterizationOptions.Layers = new string[] { "LayerA" };
// Create an instance of JpegOptions (or any ImageOptions for raster formats)
var options = new Aspose.CAD.ImageOptions.JpegOptions();
// Set VectorRasterizationOptions property to the instance of CadRasterizationOptions
options.VectorRasterizationOptions = rasterizationOptions;
//Export each layer to Jpeg format
image.Save(layerName + "_out.jpg", options);
}
}
Console.WriteLine("\nCAD all layers converted successfully to raster image format.");

Converting All CAD Layers to Separate Images

You may get all the layers from a CAD drawing using the CadImage.Layers and render each layer to separate image as demonstrated below.

// For complete examples and data files, please go to https://github.com/aspose-cad/Aspose.CAD-for-.NET
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (var image = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
var rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set image width & height
rasterizationOptions.PageWidth = 500;
rasterizationOptions.PageHeight = 500;
// Set the drawing to render at the center of image
rasterizationOptions.CenterDrawing = true;
// Get the layers in an instance of CadLayersDictionary
var layersList = image.Layers;
// Iterate over the layers
foreach (var layerName in layersList.GetLayersNames())
{
// Display layer name for tracking
Console.WriteLine("Start with " + layerName);
// Add the layer name to the CadRasterizationOptions's layer list
rasterizationOptions.Layers.Add(layerName);
// Create an instance of JpegOptions (or any ImageOptions for raster formats)
var options = new Aspose.CAD.ImageOptions.JpegOptions();
// Set VectorRasterizationOptions property to the instance of CadRasterizationOptions
options.VectorRasterizationOptions = rasterizationOptions;
//Export each layer to Jpeg format
image.Save(layerName + "_out.jpg", options);
}
}

Converting DWF CAD Layers to Raster Image Formats

Aspose.CAD for .NET API has exposed an efficient & easy to use means to specify the name of the required CAD layer and render it to raster image formats. Here is how you can achieve the same in 5 simple steps as listed below.

  1. Load the DWF CAD file into an instance of Image using the factory method Load.
  2. Create an instance of CadRasterizationOptions and set its mandatory properties such as PageWidth & PageHeight.
  3. Add the desired layer name using the CadRasterizationOptions.Layers.Add method.
  4. Create an instance of ImageOptionsBase and set its VectorRasterizationOptions property to the instance of CadRasterizationOptions created in the previous step.
  5. Call the Image.Save method by passing the file path (or an object of MemoryStream) as well as the instance of ImageOptionsBase created in the previous step.

Here is the complete source code.

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string inputFile = MyDir + "18-12-11 9644 - site.dwf";
using (Image image = Image.Load(inputFile))
{
BmpOptions bmpOptions = new BmpOptions();
var dwfRasterizationOptions = new CadRasterizationOptions();
bmpOptions.VectorRasterizationOptions = dwfRasterizationOptions;
dwfRasterizationOptions.PageHeight = 500;
dwfRasterizationOptions.PageWidth = 500;
// export
string outPath = MyDir + "18-12-11 9644 - site.bmp";
image.Save(outPath, bmpOptions);
}

Aspose.CAD for .NET directly writes the information about API and Version Number in output documents. For example, upon rendering Document to PDF, Aspose.CAD for .NET populates Application field with value ‘Aspose.CAD’ and PDF Producer field with a value, e.g ‘Aspose.CAD v 17.10’.

Please note that you cannot instruct Aspose.CAD for .NET to change or remove this information from output Documents.