[Back to Blog](/blog) # Exporting part of a CAD drawing to a .pdf file

Sergey Sherstnev January 26, 2017 [\#pdf](/blog/tag/pdf) [\#Example](/blog/tag/example)

<a class="a2a_button_facebook"></a><a class="a2a_button_twitter"></a><a class="a2a_button_linkedin"></a>

Sometimes Teigha subscribers ask: How do I export to a .pdf file only a certain part of a drawing instead of the whole drawing? Maybe it is not obvious because there is a default option ZoomToExtents in the PDF Export settings, and the same option is set by default in the PDFExportDialog of the OdaMfcApp sample application.

![](/files/images/blog/2017-01-26-pic1.png)

### What is that option and how to get rid of it?

The option is a flag in PdfExportSettings, which makes it easy to start using the PDF Export functionality. If this flag is unset, all settings relating to plot area are taken from PlotSetting, which is stored in the drawing file. More details about those settings are in the documentation. Here we just mention that the ZoomToExtents option makes the PdfExport module export a drawing as if the following options are set:

- Plot area – extents
- Scaled to fit
- Center the plot

So, take the next drawing:

[![](/files/images/blog/2017-01-26-pic2.png)](/files/images/blog/2017-01-26-pic2.png)

Export it to PDF with the ZoomToExtents option, and you get this .pdf file:

[![](/files/images/blog/2017-01-26-pic3.png)](/files/images/blog/2017-01-26-pic3.png)

However, what if you want to export only part of the drawing, for example the entity located in the center? We won’t describe here how to do it using the OdaMfcApp sample application but instead explain with program code:

#### Read the drawing:

```
<span class="hljs-title">OdDbDatabasePtr</span> pDb = m_pHostApp.<span class="hljs-keyword">readFile</span>(inputFile);
```

#### Get the pointer to the entity using its ID:

```
<span class="hljs-title">OdDbEntityPtr</span> pRect = <span class="hljs-title">OdDbEntity</span>::<span class="hljs-keyword">cast</span>(pDb-><span class="hljs-keyword">getOdDbObjectId</span>( <span class="hljs-title">OdDbHandle</span>(0x2AB)).<span class="hljs-keyword">safeOpenObject</span>(<span class="hljs-title">OdDb</span>::kForRead));

if (pRect.<span class="hljs-keyword">isNull</span>())
    throw <span class="hljs-keyword">OdError</span>(eNullEntityPointer);
```

Please note, that in this example OdDbHandle(0x2AB) - is the hardcoded handle, it was copied from a particular drawing. You should replace 0x2AB with your own handle from the drawing you are working with.

#### Get the entity extents:

```
<span class="hljs-title">OdGeExtents3d</span> ext;
pRect-><span class="hljs-keyword">getGeomExtents</span>(ext);
```

For the paper space that’s enough, because the entity coordinates are correspond with the paper coordinates.

For the model space the extents should be projected to the to the eye plane for active viewport:

```
OdGePoint3d pt1, pt2;
OdRxObjectPtr pObj;
OdDbViewportTablePtr pVPT = pDb->getViewportTableId().safeOpenObject();
OdDbViewportTableRecordPtr pActiveVP = pVPT->getActiveViewportId().safeOpenObject();

OdAbstractViewPEPtr pVp = OdAbstractViewPEPtr(pObj = pActiveVP);
OdGeVector3d vecY = pVp->upVector(pObj);
OdGeVector3d vecZ = pVp->direction(pObj);
OdGeVector3d vecX = vecY.crossProduct(vecZ).normal();
OdGeVector2d offset = pVp->viewOffset(pObj);
OdGePoint3d prTarg = pVp->target(pObj) - vecX * offset.x - vecY * offset.y;

pt1.x = vecX.dotProduct(ext.minPoint() - prTarg);
pt1.y = vecY.dotProduct(ext.minPoint() - prTarg);
pt1.z = 0.;
pt2.x = vecX.dotProduct(ext.maxPoint() - prTarg);
pt2.y = vecY.dotProduct(ext.maxPoint() - prTarg);
pt2.z = 0.;
ext.set(pt1, pt2);
```

#### Get access to the plot settings:

```
<span class="hljs-title">OdDbObjectId</span> idLayout =
<span class="hljs-title">OdDbBlockTableRecordPtr</span>(pDb-><span class="hljs-keyword">getActiveLayoutBTRId</span>().<span class="hljs-keyword">safeOpenObject</span>())-><span class="hljs-keyword">getLayoutId</span>();
<span class="hljs-title">OdDbLayoutPtr</span> pLayout = idLayout.<span class="hljs-keyword">safeOpenObject</span>(<span class="hljs-title">OdDb</span>::kForWrite);
<span class="hljs-title">OdDbPlotSettings</span> *pPlotSettings = pLayout.<span class="hljs-keyword">get</span>();
```

#### Set the necessary plot area:

```
<span class="hljs-title">OdDbPlotSettingsValidatorPtr</span> pValidator = pDb-><span class="hljs-keyword">appServices</span>()-><span class="hljs-keyword">plotSettingsValidator</span>();
pValidator-><span class="hljs-keyword">setPlotWindowArea</span>(pPlotSettings, ext.minPoint().x, ext.minPoint().y, ext.<span class="hljs-keyword">maxPoint</span>().x, ext.<span class="hljs-keyword">maxPoint</span>().y);
pValidator-><span class="hljs-keyword">setPlotType</span>(pPlotSettings, <span class="hljs-title">OdDbPlotSettings</span>::kWindow); 
pValidator-><span class="hljs-keyword">setPlotCentered</span>(pPlotSettings, true);
pValidator-><span class="hljs-keyword">setStdScaleType</span>(pPlotSettings, <span class="hljs-title">OdDbPlotSettings</span>::kScaleToFit);
```

#### Then export the drawing:

```
<span class="hljs-title">OdPdfExportModulePtr</span> pPdfModule = ::<span class="hljs-keyword">odrxDynamicLinker</span>()-><span class="hljs-keyword">loadApp</span>(OdPdfExportModuleName);
<span class="hljs-title">OdPdfExportPtr</span> exporter = pPdfModule-><span class="hljs-keyword">create</span>();

<span class="hljs-title">PDFExportParams</span> params;
params.<span class="hljs-keyword">setDatabase</span>(pDb);
params.<span class="hljs-keyword">setExportFlags</span>((<span class="hljs-title">PDFExportParams</span>::PDFExportFlags)(params.<span class="hljs-keyword">exportFlags</span>() & (~<span class="hljs-title">PDFExportParams</span>::kZoomToExtentsMode)));

<span class="hljs-title">OdUInt32</span> errCode = exporter-><span class="hljs-keyword">exportPdf</span>(params);
```

The following .pdf file is the result:

[![](/files/images/blog/2017-01-26-pic4.png)](/files/images/blog/2017-01-26-pic4.png)

This is a simple and easy way to select any part of a drawing and export it to a .pdf file.

## Get started today

**Try ODA software free for 60 days.  
No risk, no credit card required.**

[Try for Free](https://www.opendesign.com/free-trial)
