Home > Code > C# > Export AutoCAD DXF Drawing to PDF & Set Image Size in .NET Applications

Export AutoCAD DXF Drawing to PDF & Set Image Size in .NET Applications

by sher azam   on Apr 02, 2014   Category: C#   |  Views: 2215    |  Points: 25   |  Gold 


This Technical tip explains how .NET developers can export DXF Drawings to PDF using Aspose.Imaging for .NET. Aspose.Imaging includes a feature for reading AutoCAD DXF drawing entities and rendering them as an entire drawing into PDF format. It is possible to customize the conversion process to achieve specific results.
We have introduced a new Cad class in the Aspose.Imaging.FileFormats namespace. The Cad class is a family of AutoCAD drawings and since they all share similar schemes, it was decided to adopt a similar object model for the AutoCAD drawings family. To accomplish DXF to PDF export, a new class, PdfOptions, has been introduced to the Aspose.Imaging.ImageOptions namespace.

This approach works as follows:

• Open DXF drawing file using the Aspose.Imaging.Image.Load factory method.
• Export it to PDF using the Image.Save(<stream/file>, <PdfOptions instance>) method.

The programming sample below shows how to DXF Drawings to PDF file using default settings.

[C# Code Sample]

using Aspose.Imaging.FileFormats.Cad;
using Aspose.Imaging.ImageOptions;

namespace DxfExamples
{
/// <summary>
/// Represents test examples for DXF -> PDF export
/// </summary>
public class DxfExamples
{
/// <summary>
/// Default export.
/// </summary>
public void DefaultExport()
{
// Name of the file
string filename = "Drawing1.dxf";

// Load an image
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(filename))
{
// Create options
PdfOptions pdfOptions = new PdfOptions();

// Set output file size
pdfOptions.PageWidth = 800;
pdfOptions.PageHeight = 600;

// Export
image.Save(filename + ".pdf", pdfOptions);
}
}
}
}



[VB.NET Code Sample]

Imports Aspose.Imaging.FileFormats.Cad
Imports Aspose.Imaging.ImageOptions

Namespace DxfExamples
''' <summary>
''' Represents test examples for DXF -> PDF export
''' </summary>
Public Class DxfExamples
''' <summary>
''' Default export.
''' </summary>
Public Sub DefaultExport()
' Name of the file
Dim filename As String = "Drawing1.dxf"

' Load an image
Using image As Aspose.Imaging.Image = Aspose.Imaging.Image.Load(filename)
' Create options
Dim pdfOptions As New PdfOptions()

' Set output file size
pdfOptions.PageWidth = 800
pdfOptions.PageHeight = 600

' Export
image.Save(filename & Convert.ToString(".pdf"), pdfOptions)
End Using
End Sub
End Class
End Namespace

Setting the Image Size

Export from DXF to PDF is not a trivial task and since the PDF image requires the canvas size to be defined, we need to specify the output dimensions for the PDF image to render properly. Set the PdfOptions.PageWidth and PdfOptions.PageHeight properties explicitly, or you may get an ImageSaveException.

[C# Code Sample]

//The code sample below shows the auto-scaling option in use.
using Aspose.Imaging.FileFormats.Cad;
using Aspose.Imaging.ImageOptions;

namespace DxfExamples
{
/// <summary>
/// Represents test examples for DXF -> PDF export
/// </summary>
public class DxfExamples
{
/// <summary>
/// Export with automatical up-scaling.
/// </summary>
public void ExportAutoUpScale()
{
// Name of the file
string filename = "Drawing2.dxf";

// Load an image
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(filename))
{
// Create options
PdfOptions pdfOptions = new PdfOptions();

// Set output file size
pdfOptions.PageWidth = 850;
pdfOptions.PageHeight = 600;

// Allow upscaling
pdfOptions.ScaleMethod = ScaleType.GrowToFit;

// Export
image.Save(filename + "_UpScale.pdf", pdfOptions);
}
}
}
}



[VB.NET Code Sample]

Imports Aspose.Imaging.FileFormats.Cad
Imports Aspose.Imaging.ImageOptions

Namespace DxfExamples
''' <summary>
''' Represents test examples for DXF -> PDF export
''' </summary>
Public Class DxfExamples
''' <summary>
''' Export with automatical up-scaling.
''' </summary>
Public Sub ExportAutoUpScale()
' Name of the file
Dim filename As String = "Drawing2.dxf"

' Load an image
Using image As Aspose.Imaging.Image = Aspose.Imaging.Image.Load(filename)
' Create options
Dim pdfOptions As New PdfOptions()

' Set output file size
pdfOptions.PageWidth = 850
pdfOptions.PageHeight = 600

' Allow upscaling
pdfOptions.ScaleMethod = ScaleType.GrowToFit

' Export
image.Save(filename & Convert.ToString("_UpScale.pdf"), pdfOptions)
End Using
End Sub
End Class
End Namespace


Overview: Aspose.Imaging for .NET

Aspose.Imaging for .NET is an image processing & manipulation component that allows developers to create, edit, draw or convert images in their .NET application. It allows developers to convert image files to PSD, BMP, JPEG, PNG, TIFF and GIF formats. Moreover a set of pens, brushes and fonts can be used to draw images or add new elements & text to existing images. Aspose.Imaging for .NET works well with both web & windows applications. Moreover, it adds the support for Silverlight platform.

- Homepage of Aspose.Imaging for .NET: http://www.aspose.com/.net/imaging-component.aspx

- Download Aspose.Imaging for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.imaging-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts