Render CAD drawings and models as HTML, PDF, and image files
Leave feedback
On this page
GroupDocs.Viewer for .NET allows you to render your CAD drawings and 3D models in HTML, PDF, PNG, and JPEG formats. You do not need to use AutoCAD or other CAD software to load and view CAD files within your .NET application (web or desktop).
Create a Viewer class instance to get started with the GroupDocs.Viewer API. Pass a CAD drawing you want to view to the class constructor. You can load the drawing from a file or stream. Call one of the Viewer.View method overloads to convert the drawing to HTML, PDF, or image format.
GroupDocs.Viewer can detect the document format automatically based on information in the file header.
Render CAD drawings as HTML
Create an HtmlViewOptions class instance and pass it to the Viewer.View method to convert a CAD file to HTML. During the conversion, GroupDocs.Viewer creates an SVG image from the CAD drawing and embeds this image in an HTML document.
The HtmlViewOptions class properties allow you to control the conversion process. For instance, you can embed all external resources in the generated HTML file, minify the output file, and optimize it for printing. Refer to the following documentation section for details: Rendering to HTML.
Create an HTML file with embedded resources
To save all elements of an HTML page (including text, graphics, and stylesheets) into a single file, call the HtmlViewOptions.ForEmbeddedResources method and specify the output file name.
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(Viewerviewer=newViewer("HousePlan.dwg")){// Create an HTML file for the drawing.// Specify the HTML file name.HtmlViewOptionsviewOptions=HtmlViewOptions.ForEmbeddedResources("output.html");viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("HousePlan.dwg")' Create an HTML file for the drawing.
' Specify the HTML file name.
DimviewOptionsAsHtmlViewOptions=HtmlViewOptions.ForEmbeddedResources("output.html")viewer.View(viewOptions)EndUsingEndSubEndModule
The following image demonstrates the result:
Create an HTML file with external resources
If you want to store an HTML file and additional resource files (such as fonts, images, and style sheets) separately, call the HtmlViewOptions.ForExternalResources method and pass the following parameters:
The output file path format
The path format for the folder with external resources
The resource URL format
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(Viewerviewer=newViewer("HousePlan.dwg")){// Create an HTML file for the drawing.// Specify the HTML file name and location of external resources.// {0} is replaced with the resource name.HtmlViewOptionsviewOptions=HtmlViewOptions.ForExternalResources("output.html","output/resource_{0}","output/resource_{0}");viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("HousePlan.dwg")' Create an HTML file for the drawing.
' Specify the HTML file name and location of external resources.
' {0} is replaced with the resource name.
DimviewOptionsAsHtmlViewOptions=HtmlViewOptions.ForExternalResources("output.html","output/resource_{0}","output/resource_{0}")viewer.View(viewOptions)EndUsingEndSubEndModule
The image below demonstrates the result. External resources are placed in a separate folder.
Render CAD drawings as PDF
Create a PdfViewOptions class instance and pass it to the Viewer.View method to convert a CAD file to PDF. The PdfViewOptions class properties allow you to control the conversion process. For instance, you can protect the output PDF file, reorder its pages, and specify the quality of document images. Refer to the following documentation section for details: Rendering to PDF.
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(Viewerviewer=newViewer("HousePlan.dwg")){// Create a PDF file for the drawing.// Specify the PDF file name.PdfViewOptionsviewOptions=newPdfViewOptions("output.pdf");viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("HousePlan.dwg")' Create a PDF file for the drawing.
' Specify the PDF file name.
DimviewOptionsAsPdfViewOptions=NewPdfViewOptions("output.pdf")viewer.View(viewOptions)EndUsingEndSubEndModule
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(Viewerviewer=newViewer("HousePlan.dwg")){// Create a PNG image for the drawing.PngViewOptionsviewOptions=newPngViewOptions("output.png");// Set width and height.viewOptions.Width=1500;viewOptions.Height=1000;viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("HousePlan.dwg")' Create a PNG image for the drawing.
DimviewOptionsAsPngViewOptions=NewPngViewOptions("output.png")' Set width and height.
viewOptions.Width=1500viewOptions.Height=1000viewer.View(viewOptions)EndUsingEndSubEndModule
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(Viewerviewer=newViewer("HousePlan.dwg")){// Create a JPG image for the drawing.JpgViewOptionsviewOptions=newJpgViewOptions("output.jpg");// Set width and height.viewOptions.Width=1500;viewOptions.Height=1000;viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("HousePlan.dwg")' Create a JPG image for the drawing.
DimviewOptionsAsJpgViewOptions=NewJpgViewOptions("output.jpg")' Set width and height.
viewOptions.Width=1500viewOptions.Height=1000viewer.View(viewOptions)EndUsingEndSubEndModule
Get information about existing layouts and layers
Follow the steps below to obtain information about layouts and layers contained in a CAD drawing. You can use this information to specify which layers and layouts to display in the output file.
Call the Viewer.GetViewInfo method, pass the ViewInfoOptions instance to this method as a parameter, and cast the returned object to the CadViewInfo type.
usingSystem;usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;usingGroupDocs.Viewer.Results;// ...using(Viewerviewer=newViewer("HousePlan.dwg")){ViewInfoOptionsviewInfoOptions=ViewInfoOptions.ForHtmlView();// Enable this option to see the list of all layouts contained in the CAD file.viewInfoOptions.CadOptions.RenderLayouts=true;CadViewInfoviewInfo=viewer.GetViewInfo(viewInfoOptions)asCadViewInfo;if(viewInfo!=null){// Display information about the CAD file.Console.WriteLine($"File type: {viewInfo.FileType}");Console.WriteLine($"The number of pages: {viewInfo.Pages.Count}");// Display the list of existing layouts.Console.WriteLine("The drawing contains the following layout(s):");foreach(LayoutlayoutinviewInfo.Layouts)Console.WriteLine(layout.Name);// Display the list of existing layers.Console.WriteLine("The drawing contains the following layer(s):");foreach(LayerlayerinviewInfo.Layers)Console.WriteLine(layer);}}
ImportsSystemImportsGroupDocs.ViewerImportsGroupDocs.Viewer.OptionsImportsGroupDocs.Viewer.Results' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("HousePlan.dwg")DimviewInfoOptionsAsViewInfoOptions=ViewInfoOptions.ForHtmlView()' Enable this option to see the list of all layouts contained in the CAD file.
viewInfoOptions.CadOptions.RenderLayouts=TrueDimviewInfoAsCadViewInfo=TryCast(viewer.GetViewInfo(viewInfoOptions),CadViewInfo)IfviewInfoIsNotNothingThen' Display information about the CAD file.
Console.WriteLine($"File type: {viewInfo.FileType}")Console.WriteLine($"The number of pages: {viewInfo.Pages.Count}")' Display the list of existing layouts.
Console.WriteLine("The drawing contains the following layout(s):")ForEachlayoutAsLayoutInviewInfo.LayoutsConsole.WriteLine(layout.Name)Next' Display the list of existing layers.
Console.WriteLine("The drawing contains the following layer(s):")ForEachlayerAsLayerInviewInfo.LayersConsole.WriteLine(layer)NextEndIfEndUsingEndSubEndModule
The following image shows a sample console output:
Render all or specific layouts
When you convert a CAD drawing to HTML, PDF, or image format, GroupDocs.Viewer renders only the model-space layout (Model). If you also need to render all paper space layouts contained in your CAD file, enable the CadOptions.RenderLayouts option for one of the following classes (depending on the output file format):
The following example renders all layouts when converting a CAD drawing to PDF:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(Viewerviewer=newViewer("sample.dwg")){// Convert the document to PDF.PdfViewOptionsviewOptions=newPdfViewOptions("output.pdf");// Render the Model and all non-empty paper space layouts. viewOptions.CadOptions.RenderLayouts=true;viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("sample.dwg")' Convert the document to PDF.
DimviewOptionsAsPdfViewOptions=NewPdfViewOptions("output.pdf")' Render the Model and all non-empty paper space layouts.
viewOptions.CadOptions.RenderLayouts=Trueviewer.View(viewOptions)EndUsingEndSubEndModule
To render a specific layout, assign the layout name to the CadOptions.LayoutName property of a target view.
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(Viewerviewer=newViewer("sample.dwg")){// Convert the document to PDF.PdfViewOptionsviewOptions=newPdfViewOptions("output.pdf");// Specify the name of the layout to render.// If the specified layout is not found,// an exception occurs.viewOptions.CadOptions.LayoutName="Layout1";viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("sample.dwg")' Convert the document to PDF.
DimviewOptionsAsPdfViewOptions=NewPdfViewOptions("output.pdf")' Specify the name of the layout to render.
' If the specified layout is not found,
' an exception occurs.
viewOptions.CadOptions.LayoutName="Layout1"viewer.View(viewOptions)EndUsingEndSubEndModule
A CAD drawing can contain many layers. Each layer is used to draw a specific object type. For example, the drawing below contains layers for walls, furniture, plants, and so on. You can control the visibility of objects on the drawing by turning specific layers on or off.
When you convert a CAD drawing to HTML, PDF, or image format, GroupDocs.Viewer renders all available layers. You can specify which layers to display in the output file, as described below:
Assign the list of layers you want to render to the CadOptions.Layers property.
The following example renders layers with walls and furniture to PDF:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;usingGroupDocs.Viewer.Results;usingSystem.Collections.Generic;// ...using(Viewerviewer=newViewer("HousePlan.dwg")){// Convert the document to PDF.PdfViewOptionsviewOptions=newPdfViewOptions("output.pdf");// Specify a list of layers to display.viewOptions.CadOptions.Layers=newList<Layer>{newLayer("Base"),newLayer("Walls"),newLayer("Furniture")};viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.OptionsImportsGroupDocs.Viewer.ResultsImportsSystem.Collections.Generic' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("HousePlan.dwg")' Convert the document to PDF.
DimviewOptionsAsPdfViewOptions=NewPdfViewOptions("output.pdf")' Specify a list of layers to display.
viewOptions.CadOptions.Layers=NewList(OfLayer)From{NewLayer("Base"),NewLayer("Walls"),NewLayer("Furniture")}viewer.View(viewOptions)EndUsingEndSubEndModule
The image below illustrates the result.
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.