Specify rendering options for CAD files

GroupDocs.Viewer ships with the CadOptions class that allows you to specify different options for rendering CAD files. To access these options, use the CadOptions property for one of the following classes (depending on the output file format):

Specify the background color

Use the CadOptions.BackgroundColor property to specify the background color for the output file.

The following code snippet converts a CAD drawing to PDF and sets the background color of PDF pages to light yellow:

using GroupDocs.Viewer;
using GroupDocs.Viewer.Drawing;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("HousePlan.dwg"))
{
    // Convert the document to PDF.
    var viewOptions = new PdfViewOptions("output.pdf");

    // Specify the background color.
    viewOptions.CadOptions.BackgroundColor = Argb32Color.Transparent;
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Drawing
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("HousePlan.dwg")
            ' Convert the document to PDF.
            Dim viewOptions = New PdfViewOptions("output.pdf")
        
            ' Specify the background color.
            viewOptions.CadOptions.BackgroundColor = Argb32Color.Transparent
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

The following image illustrates the result:

Specify the background color for the output PDF file

Configure the output image size

When rendering a CAD drawing, GroupDocs.Viewer creates an image with the largest dimension (width or height) set to 2000 pixels. The other dimension is calculated based on the aspect ratio of the original drawing. You can use the following methods to change the width and height of the output file:

The following example converts a CAD drawing to PNG format and reduces the width and height of the output image by 50%:

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("HousePlan.dwg"))
{
    // Convert the diagram to PNG.
    var viewOptions = new PngViewOptions("output.png");
    // Specify a scale factor.
    viewOptions.CadOptions = CadOptions.ForRenderingByScaleFactor(0.5f);
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("HousePlan.dwg")
            ' Convert the diagram to PNG.
            Dim viewOptions = New PngViewOptions("output.png")
            ' Specify a scale factor.
            viewOptions.CadOptions = CadOptions.ForRenderingByScaleFactor(0.5F)
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

When you render all layouts/sheets contained in a CAD file (the CadOptions.RenderLayouts property is true), each layout/sheet is rendered as a separate page/image and has its own size. In this case, when you specify only the width or height value, the other side is scaled proportionally to maintain the aspect ratio of each layout/sheet. When you set both width and height, all generated images have the same size and may look distorted. To avoid this, use the CadOptions.LayoutName property to render each layout/sheet separately and set its size.

Apply the PC3 file settings

AutoCAD allows you to configure plotter settings and save them as a PC3 file (Plotter Configuration Version 3) for later use. With GroupDocs.Viewer, you can apply width and height values from a PC3 file to the output file when you convert your CAD drawing to HTML, PDF, or image format. Use the CadOptions.Pc3File property for a target view to specify a path to the PC3 file with required settings. The default location for PC3 files is “C:\Users\[Username]\AppData\Roaming\Autodesk\AutoCAD [Version]\[Version Code]\[Language]\Plotters”.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("sample.dwg"))
{
    // Convert the diagram to PDF.
    var viewOptions = new PdfViewOptions("output.pdf");
    // Specify a path to the PC3 file.
    viewOptions.CadOptions.Pc3File = "small_page.pc3";
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("sample.dwg")
            ' Convert the diagram to PDF.
            Dim viewOptions = New PdfViewOptions("output.pdf")
            ' Specify a path to the PC3 file.
            viewOptions.CadOptions.Pc3File = "small_page.pc3"
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

Split a drawing into tiles

With GroupDocs.Viewer, you can split a CAD drawing (in DWG or DWT format) into parts (tiles) and render each part separately. Tiled rendering allows you to reduce memory usage when you convert large drawings to HTML, PDF, or image format. When tiled rendering is enabled, GroupDocs.Viewer renders only the model space layout (Model) and ignores the CadOptions.RenderLayouts and CadOptions.LayoutName property values.

Split a CAD drawing into tiles

To create an individual tile, instantiate a Tile object. Specify the x- and y-coordinates of the tile’s lower-left corner and the tile width and height (in pixels). The image below illustrates a coordinate system used to define the tile position. The origin (0,0) is located in the lower-left corner of the drawing. The positive x-axis extends horizontally to the right, and the positive y-axis is oriented vertically from bottom to top.

Tile coordinates

After you create all tiles, add them to the ViewOptions.CadOptions.Tiles list and call the Viewer.View method to convert these tiles to a desired format. Each tile will be rendered as a separate page/image.

The following example demonstrates how to split a CAD drawing into four tiles (2x2) of equal size:

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
using GroupDocs.Viewer.Results;
// ...

using (var viewer = new Viewer("HousePlan.dwg"))
{
    var viewInfoOptions = ViewInfoOptions.ForHtmlView();
    var viewInfo = viewer.GetViewInfo(viewInfoOptions);

    // Get the width and height of the CAD drawing.
    int width = viewInfo.Pages[0].Width;
    int height = viewInfo.Pages[0].Height;

    // Specify the number of rows and columns to split the drawing into.
    int columns = 2;
    int rows = 2;

    // Calculate the width and height of each tile.
    int tileWidth = width / columns;
    int tileHeight = height / rows;
    int pointX = 0;
    int pointY = 0;

    // Split the drawing into tiles and convert them to HTML.
    // {0} is replaced with the tile number in the output file name.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("page_{0}.html");
    for (int i = 0; i < columns; i++)
        for (int j = 0; j < rows; j++)
        {
            Tile tile = new Tile(pointX + tileWidth * i, pointY + tileHeight * j,
                tileWidth, tileHeight);
            viewOptions.CadOptions.Tiles.Add(tile);
        }
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
Imports GroupDocs.Viewer.Results
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("HousePlan.dwg")
            Dim viewInfoOptions = ViewInfoOptions.ForHtmlView()
            Dim viewInfo = viewer.GetViewInfo(viewInfoOptions)
        
            ' Get the width and height of the CAD drawing.
            Dim width As Integer = viewInfo.Pages(0).Width
            Dim height As Integer = viewInfo.Pages(0).Height
        
            ' Specify the number of rows and columns to split the drawing into.
            Dim columns As Integer = 2
            Dim rows As Integer = 2
        
            ' Calculate the width and height of each tile.
            Dim tileWidth As Integer = width / columns
            Dim tileHeight As Integer = height / rows
            Dim pointX As Integer = 0
            Dim pointY As Integer = 0
        
            ' Split the drawing into tiles and convert them to HTML.
            ' {0} is replaced with the tile number in the output file name.
            Dim viewOptions = HtmlViewOptions.ForEmbeddedResources("page_{0}.html")
            For i As Integer = 0 To columns - 1
                For j As Integer = 0 To rows - 1
                    Dim tile As Tile = New Tile(pointX + tileWidth * i, pointY + tileHeight * j, tileWidth, tileHeight)
                    viewOptions.CadOptions.Tiles.Add(tile)
                Next
            Next
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

In the example above the GroupDocs.Viewer will generate four HTML files named “page_1.html”, “page_2.html”, “page_3.html”, and “page_4.html”, where each of these HTML file contains a single tile in a form of SVG vector image. The HtmlViewOptions.ForExternalResources() static method (with all its overloads) can also be used — in such case the SVG files will not be embedded inside the output HTML files, but will be saved separately, while HTML only references them through the A HTML element.

Prior the version 24.3 of the GroupDocs.Viewer the tiled rendering of CAD files was available only for the HTML rendering. Starting from the version 24.3 the CAD tiled rendering is also supported for the PDF — in that case the GroupDocs.Viewer generates a single PDF file, where one its page represents one tile. So, if in the example above we replace the HtmlViewOptions onto the PdfViewOptions, then the GroupDocs.Viewer will produce one PDF file with four pages inside it.

Choose rendering speed instead of quality

By default the GroupDocs.Viewer converts and renders all documents within CAD format family with the max possible quality. In case when the input CAD file (DWG, for example) is very complex, it may lead to quite significant processing time. Also, the size of the generated output HTML or image (vector or raster) also may be too heavy.

Starting from the version 24.2 the GroupDocs.Viewer introduces a new public property within the CadOptions class — the EnablePerformanceConversionMode boolean flag. By default it is set to false - the GroupDocs.Viewer behaves as in previous versions, preserving the maximum quality. But when setting its value to true, then the performance-oriented conversion mode is enabled, which leads to significantly lesser conversion time as well as lesser byte size of the output document.

Enabling this mode is pretty simple — just create an instance of the CadOptions class by using any of the static methods described above, and then set the value for the EnablePerformanceConversionMode property. Example is below:

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (Viewer viewer = new Viewer("input.dwg"))
{
   HtmlViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources("Output-Page#{0}.html");                    
   viewOptions.CadOptions = CadOptions.ForRenderingByWidth(1000);
   viewOptions.CadOptions.EnablePerformanceConversionMode = true;

   viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("input.dwg")
            Dim viewOptions = HtmlViewOptions.ForEmbeddedResources("page_{0}.html")

            viewOptions.CadOptions = CadOptions.ForRenderingByWidth(1000)
            viewOptions.CadOptions.EnablePerformanceConversionMode = True

            viewer.View(viewOptions)
        End Using
    End Sub
End Module

If taking an ordinary DWG file as a sample, the comparison between “quality” and “performance” modes are the next:

Conversion modeOutput file size, MiBProcessing time, sec
Quality-oriented (default)46.87.87
Performance-oriented (new)5.044.47

Screenshot below illustrates the visual differences between these modes, default quality-oriented mode is on the left side, and the new performance-oriented mode is on the right side:

DWG quality vs. performance