//Checks the element names of the page layout //returns boolean true if duplicates exist public static Boolean checkLayoutTextElementsForDuplicates(IMxDocument pMxDoc, string pFrameName) { //create list to store name of text elements List <string> lst = new List <string>(); Boolean duplicates; //check if the frame passed exists in the map document if (PageLayoutProperties.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 lst.Add(pElementProp.Name); } } element = pGraphics.Next() as IElement; } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Error detecting duplicate elements from the map frame"); System.Diagnostics.Debug.WriteLine(e); } } //Find if duplicates exist int duplicateCount = lst.Count() - lst.Distinct().Count(); if (duplicateCount != 0) { duplicates = true; return(duplicates); } else { duplicates = false; return(duplicates); } }
//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 (PageLayoutProperties.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 && PageLayoutProperties.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") { IExportJPEG m_export; docExport = new ExportJPEGClass(); if (docExport is IExportJPEG) { m_export = (IExportJPEG)docExport; m_export.ProgressiveMode = false; //hardcoded progressive mode value here m_export.Quality = 80; //hardcoded quality value here docExport = (IExport)m_export; } } 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); }