GroupDocs.Viewer ships with the CadOptions class that allows you to specify different options for rendering CAD files. To access these options, use the setCadOptions methods for one of the following classes (depending on the output file format):
The following code snippet converts a CAD drawing to PDF and sets the background color of PDF pages to light yellow:
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.PdfViewOptions;importcom.groupdocs.viewer.options.CadOptions;// ...
try(Viewerviewer=newViewer("HousePlan.dwg")){// Convert the document to PDF.
PdfViewOptionsviewOptions=newPdfViewOptions("output.pdf");// Specify the background color.
viewOptions.getCadOptions().setBackgroundColor(Color.YELLOW);viewer.view(viewOptions);}
The following image illustrates the result:
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:
CadOptions.forRenderingByWidth—Specifies the output image width in pixels. The image height is calculated based on the aspect ratio of the original CAD drawing.
CadOptions.forRenderingByHeight—Specifies the output image height in pixels. The image width is calculated based on the aspect ratio of the original CAD drawing.
CadOptions.forRenderingByScaleFactor—Specifies a scale factor to apply to the output image. Values between 0 and 1 decrease the image size, and values greater than 1 increase the image.
The following example converts a CAD drawing to PNG format and reduces the width and height of the output image by 50%:
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.PngViewOptions;importcom.groupdocs.viewer.options.CadOptions;// ...
try(Viewerviewer=newViewer("HousePlan.dwg")){// Create a PNG image for the drawing.
PngViewOptionsviewOptions=newPngViewOptions("output.png");// Specify a scale factor.
viewOptions.setCadOptions(CadOptions.forRenderingByScaleFactor(0.5f));viewer.view(viewOptions);}
When you render all layouts/sheets contained in a CAD file (pass true to the CadOptions.setRenderLayouts method), 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.setLayoutName method 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.setPc3File method 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”.
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.PdfViewOptions;importcom.groupdocs.viewer.options.CadOptions;// ...
try(Viewerviewer=newViewer("sample.dwg")){// Convert the diagram to PDF.
PdfViewOptionsviewOptions=newPdfViewOptions("output.pdf");// Specify a path to the PC3 file.
viewOptions.getCadOptions().setPc3File("small_page.pc3");viewer.view(viewOptions);}
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.setRenderLayouts(…) and CadOptions.setLayoutName(…) property values.
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.
The following example demonstrates how to split a CAD drawing into four tiles (2x2) of equal size:
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.HtmlViewOptions;importcom.groupdocs.viewer.options.Tile;importcom.groupdocs.viewer.options.ViewInfoOptions;importcom.groupdocs.viewer.results.ViewInfo;// ...
ViewInfoOptionsviewInfoOptions=ViewInfoOptions.forHtmlView();try(Viewerviewer=newViewer("HousePlan.dwg")){ViewInfoviewInfo=viewer.getViewInfo(viewInfoOptions);// Get the width and height of the CAD drawing.
intwidth=viewInfo.getPages().get(0).getWidth();intheight=viewInfo.getPages().get(0).getHeight();// Calculate the width and height of each tile.
inttileWidth=width/2;inttileHeight=height/2;intpointX=0;intpointY=0;// Split the drawing into tiles and convert them to HTML.
// {0} is replaced with the tile number in the output file name.
HtmlViewOptionsviewOptions=HtmlViewOptions.forEmbeddedResources("page_{0}.html");Tiletile=newTile(pointX,pointY,tileWidth,tileHeight);viewOptions.getCadOptions().getTiles().add(tile);pointX+=tileWidth;tile=newTile(pointX,pointY,tileWidth,tileHeight);viewOptions.getCadOptions().getTiles().add(tile);pointX=0;pointY+=tileHeight;tile=newTile(pointX,pointY,tileWidth,tileHeight);viewOptions.getCadOptions().getTiles().add(tile);pointX+=tileWidth;tile=newTile(pointX,pointY,tileWidth,tileHeight);viewOptions.getCadOptions().getTiles().add(tile);viewer.view(viewOptions);}
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 to the version 24.10 of the GroupDocs.Viewer the tiled rendering of CAD files was available only for the HTML rendering. Starting from the version 24.10 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.10 the GroupDocs.Viewer introduces a new public property within the CadOptions class — the setEnablePerformanceConversionMode(...) 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 and then set the value for the setEnablePerformanceConversionMode(...) property. Example is below:
If taking an ordinary DWG file as a sample, the comparison between “quality” and “performance” modes are the next:
Conversion mode
Output file size, MiB
Processing time, sec
Quality-oriented (default)
46.8
7.87
Performance-oriented (new)
5.04
4.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:
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.