DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 108 posts at DZone. View Full User Profile

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

04.02.2014
| 8666 views |
  • submit to reddit
//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

//[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

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.