//Returns the map title as string given the IApplciation variable of the current mxd public static string getPageSize(IMxDocument pMxDoc, string pFrameName) { string pageSize = null; Dictionary <string, string> mapProps = LayoutElements.getDataframeProperties(pMxDoc, pFrameName); string pageFormId = mapProps["page_size"]; Dictionary <string, string> pageSizes = new Dictionary <string, string>(); pageSizes.Add("esriPageFormLetter", "Letter"); pageSizes.Add("esriPageFormLegal", "Legal"); pageSizes.Add("esriPageFormTabloid", "Tabloid"); pageSizes.Add("esriPageFormC", "C"); pageSizes.Add("esriPageFormD", "D"); pageSizes.Add("esriPageFormE", "E"); pageSizes.Add("esriPageFormA5", "A5"); pageSizes.Add("esriPageFormA4", "A4"); pageSizes.Add("esriPageFormA3", "A3"); pageSizes.Add("esriPageFormA2", "A2"); pageSizes.Add("esriPageFormA1", "A1"); pageSizes.Add("esriPageFormA0", "A0"); pageSizes.Add("esriPageFormCUSTOM", "Custom"); pageSizes.Add("esriPageFormSameAsPrinter", "Same as printer"); foreach (var i in pageSizes) { if (pageFormId == i.Key) { pageSize = i.Value; } } return(pageSize); }
//Returns the map title as string given the IApplciation variable of the current mxd public static string getScale(IMxDocument pMxDoc, string pFrameName) { Dictionary <string, string> mapProps = LayoutElements.getDataframeProperties(pMxDoc, pFrameName); string scale; int temp_scale; int.TryParse(mapProps["scale"], out temp_scale); scale = "1: " + temp_scale.ToString("n0"); return(scale); }
//Gets all the text elements and their values from a map frame. //returns a dictionary with key value pairs of these element / values public static Dictionary <string, string> getLayoutTextElements(IMxDocument pMxDoc, string pFrameName) { //create dictionary to story element values Dictionary <string, string> dict = new Dictionary <string, string>(); //check if the frame passed exists in the map document if (LayoutElements.detectMapFrame(pMxDoc, pFrameName)) { //Declare variables IPageLayout pLayout = pMxDoc.PageLayout; IGraphicsContainer pGraphics = pLayout as IGraphicsContainer; pGraphics.Reset(); IElement element = new TextElementClass(); IElementProperties2 pElementProp; ITextElement pTextElement; //loop through the text elements in the frame try { element = (IElement)pGraphics.Next(); while (element != null) { if (element is ITextElement) { pTextElement = element as ITextElement; pElementProp = element as IElementProperties2; //where the name is not blank //System.Diagnostics.Debug.WriteLine(pElementProp.Name); if (pElementProp.Name != "") { //store the name of the elements and the values in the dictionary as pairs dict.Add(pElementProp.Name, pTextElement.Text); } } element = pGraphics.Next() as IElement; } //return the dictionary return(dict); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Error getting elements from the map frame"); System.Diagnostics.Debug.WriteLine(e); return(null); } } else { return(null); } }
// Exports a given page layout or map frame to a variety of image formats, returns the image file path public static string exportImage(IMxDocument pMxDoc, string exportType, string dpi, string pathDocumentName, string mapFrameName) { // Define the activeView as either the page layout or the map frame // If the mapFrameName variable is null then the activeView is the page, otherwise it is set to the map frame name specified IMap pMap; IActiveView pActiveView = null; IMaps pMaps = pMxDoc.Maps; // Also construct output filename depending on the activeView / mapFrame input string pathFileName = string.Empty; if (mapFrameName == null) { pActiveView = pMxDoc.ActiveView; pathFileName = @pathDocumentName + "-" + dpi.ToString() + "dpi." + exportType; } else if (mapFrameName != null && LayoutElements.detectMapFrame(pMxDoc, mapFrameName)) { for (int i = 0; i <= pMaps.Count - 1; i++) { pMap = pMaps.get_Item(i); if (pMap.Name == mapFrameName) { pActiveView = pMap as IActiveView; } } pathFileName = @pathDocumentName + "-mapframe-" + dpi.ToString() + "dpi." + exportType; } else { return(null); } //Declare the export variable and set the file and path from the parameters IExport docExport; //parameter check if (pActiveView == null) { return(null); } // The Export*Class() type initializes a new export class of the desired type. if (exportType == "pdf") { docExport = new ExportPDFClass(); } else if (exportType == "eps") { docExport = new ExportPSClass(); } else if (exportType == "ai") { docExport = new ExportAIClass(); } else if (exportType == "bmp") { docExport = new ExportBMPClass(); } else if (exportType == "tiff") { docExport = new ExportTIFFClass(); } else if (exportType == "svg") { docExport = new ExportSVGClass(); } else if (exportType == "png") { docExport = new ExportPNGClass(); } else if (exportType == "gif") { docExport = new ExportGIFClass(); } else if (exportType == "emf") { docExport = new ExportEMFClass(); } else if (exportType == "jpeg") { docExport = new ExportJPEGClass(); } else { return(pathFileName); } docExport.ExportFileName = pathFileName; // Because we are exporting to a resolution that differs from screen // resolution, we should assign the two values to variables for use // in our sizing calculations System.Int32 screenResolution = 96; System.Int32 outputResolution = Convert.ToInt32(dpi); docExport.Resolution = outputResolution; // If input type is map frame calculate the export rectangle tagRECT exportRECT; // This is a structure exportRECT.left = 0; exportRECT.top = 0; exportRECT.right = pActiveView.ExportFrame.right * (outputResolution / screenResolution); exportRECT.bottom = pActiveView.ExportFrame.bottom * (outputResolution / screenResolution); // Set up the PixelBounds envelope to match the exportRECT IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); docExport.PixelBounds = envelope; try { System.Int32 hDC = docExport.StartExporting(); pActiveView.Output(hDC, (System.Int16)docExport.Resolution, ref exportRECT, null, null); // Explicit Cast and 'ref' keyword needed docExport.FinishExporting(); docExport.Cleanup(); } catch (Exception e) { Debug.WriteLine("Error writing to file, probably a file permissions error. Check the exception message below for details."); Debug.WriteLine(e.Message); } //Return the path of the file return(pathFileName); }