Export Map as PDF in C#?

2139
8
06-23-2021 10:06 AM
Rconrad
by
New Contributor II

有没有办法自动导出映射到一个吗folder as a pdf using an add-in? I have an add-in set up but can't find any documentation for how to accomplish the exporting part. Please let me know if you can help.

P.S: if you have any general tips for learning development with ArcObjects please leave a comment.

-Thanks.

0Kudos
8 Replies
BrentHoskisson
Occasional Contributor III

Here is what I use to export to a BMP. There is also an ExportPDFClass() for line 8 with similar functions.

Uses ESRI.ArcGIS.Output

and VerifyFile merely checks if a file is already there and deletes it if it is. Should be fairly easy to reproduce.

Good Luck

Brent Hoskisson

public static bool ExportActiveView(IMap map, string bmpFile) { string pathName = ""; string fileName = ""; if (AOUtilities.VerifyFile(bmpFile, "bmp", out pathName, out fileName)) File.Delete(bmpFile); IActiveView av = (IActiveView)map; IExport pExport = new ExportBMPClass(); pExport.ExportFileName = bmpFile; //pExport.Resolution = 96; tagRECT eRect = av.ExportFrame; IEnvelope pPBE = new EnvelopeClass(); pPBE.PutCoords(eRect.left, eRect.top, eRect.right, eRect.bottom); pExport.PixelBounds = pPBE; try { int hDC = pExport.StartExporting(); av.Output(hDC, Convert.ToInt32(pExport.Resolution), ref eRect, null, null); pExport.FinishExporting(); return true; } catch { return false; } finally { pExport.Cleanup(); } }

Rconrad
by
New Contributor II

Thank you so much for your reply, I just have a few questions though... I keep getting "Interop Type ExportBMPClass cannot be embedded" errors, and I am not sure where the variables "AOUtilities" or "File" come from. Do you have any ideas? Thanks again.

Rconrad_0-1624650117871.png

-Rylan Conrad

0Kudos
BrentHoskisson
Occasional Contributor III

For ExportBMPClass cannot be embedded:

Find ESRI.ArcGIS.Output Reference and view the properties. Set Embed Interop Types to false.

You can remove the VerifyFile type call and just enter the path name and file name of your output BMP into the variable initializers. Then just run a File.Exists on the file name to see if you need to delete it.

0Kudos
DuncanHornby
MVP Notable Contributor

Here is anothercode snippetfor you to explore this one creating a JPEG. Just a matter of adapting to a PDF format.

0Kudos
Rconrad
by
New Contributor II

That's super helpful, but what do I pass in for the IActiveView parameter when calling the function.

It looks something like this -- CreateJPEGFromActiveView(activeView, @"c:\\....")

Thanks@DuncanHornby

0Kudos
DuncanHornby
MVP Notable Contributor

Ryan

You need only to scroll down the snippet list and you will discover a function called....

Get ActiveView from ArcMap Snippet

:winking_face:

Theactive viewcould be either the Map or Layout as indicated by the object model in the help file

0Kudos
Rconrad
by
New Contributor II

Duncan,

Thanks for your response. On theGet ActiveView Snippet,where does the "application" parameter come from? Thanks again.

-Rylan

0Kudos
DuncanHornby
MVP Notable Contributor

The application parameter is the application, in VB you get a handle on it using the code:

My.ArcMap.Application

I don't code in c# but I would imagine its similar? Just look around at some code samples?

0Kudos