// this routine will convert the given document to an xml file and then look inside it to see if // one of the list of font names are used within it. If so, then it will also set the doc output param protected bool HasFont(string strDocFilename, Word.Application wrdApp, List<string> astrFontsToSearchFor, out DocXmlDocument doc) { try { // convert the document to XML and get an XmlDoc for it (on which we can do XPath queries doc = ConvertDocToXml(wrdApp, strDocFilename); // put it in a map if it exists if ((doc != null) && doc.HasFonts(astrFontsToSearchFor)) return true; } catch (Exception ex) { string strMsg = String.Format("Unable to process the file '{0}'. Reason: {1}", m_strCurrentDocument, ex.Message); if (ex.InnerException != null) strMsg += String.Format("{0}{0}{1}", Environment.NewLine, ex.InnerException.Message); strMsg += String.Format("{0}{0}{1}", Environment.NewLine, "Do you want to continue, skipping this file?"); DialogResult res = MessageBox.Show(strMsg, cstrCaption, MessageBoxButtons.YesNoCancel); if (res != DialogResult.Yes) throw new ApplicationException(cstrCancelSearch); } doc = null; return false; }
protected void GetTextIteratorStyleType(string strStyleType, DocXmlDocument doc, List<string> lstStyleIds, string strXPathFormatText) { foreach (string strStyleId in lstStyleIds) { // get the id of this style (which we've already looked up) // (it may not occur in this particular document... so skip it if not) if (doc.mapStyleId2Name.ContainsKey(strStyleId)) { string strStyleName = doc.mapStyleId2Name[strStyleId]; UpdateStatusBarDocNamePlusTwo("Examining '{0}'... Searching for {1} style-based formatting based on style '{2}'...", strStyleType, strStyleName); if (doc.GetTextIteratorForName(strStyleId, strXPathFormatText, ref mapStyleId2Iterator, false)) { if (!mapName2Font.ContainsKey(strStyleName)) { System.Diagnostics.Debug.Assert(doc.mapStyleName2FontName.ContainsKey(strStyleName)); string strFontName = doc.mapStyleName2FontName[strStyleName]; Font font = CreateFontSafe(strFontName); mapName2Font.Add(strStyleName, font); RowMaxHeight = Math.Max(RowMaxHeight, font.Height); } } } } }
// this gets the text iterators for Styles (based on a certain font) runs public void GetTextIteratorListFontStyle(DocXmlDocument doc) { if (!String.IsNullOrEmpty(doc.m_strDefaultStyleFontName)) { UpdateStatusBarDocNamePlusOne("Examining '{0}'... Searching for default paragraph style-based formatting based on font '{1}'...", doc.m_strDefaultStyleFontName); doc.GetTextIteratorForName(doc.m_strDefaultStyleFontName, doc.XPathFormatGetDefaultPStyleFontText, ref mapDefStyleFontNames2Iterator, false); } foreach (string strFontName in doc.lstFontNamesPStyle) { UpdateStatusBarDocNamePlusOne("Examining '{0}'... Searching for paragraph style-based formatting based on font '{1}'...", strFontName); doc.GetTextIteratorForName(strFontName, doc.XPathFormatGetPStyleFontText, ref mapPStyleFontNames2Iterator, false); } foreach (string strFontName in doc.lstFontNamesCStyle) { UpdateStatusBarDocNamePlusOne("Examining '{0}'... Searching for character style-based formatting based on font '{1}'...", strFontName); doc.GetTextIteratorForName(strFontName, doc.XPathFormatGetCStyleFontText, ref mapCStyleFontNames2Iterator, false); } }
// this gets the text iterators for style-based formatting that has text runs // (i.e. "Style only" -- "only Style-based runs") public void GetTextIteratorListStyle(DocXmlDocument doc) { GetTextIteratorStyleType("paragraph", doc, doc.lstPStyleIdList, doc.XPathFormatGetPStyleText); GetTextIteratorStyleType("character", doc, doc.lstCStyleIdList, doc.XPathFormatGetCStyleText); }
// this gets the text iterators for custom font runs // (i.e. "Fonts only" -- "only custom font runs") public void GetTextIteratorListFontCustom(DocXmlDocument doc) { // now m_astrFullFontNameList is loaded, so let's see which of these has any text associated foreach (string strFontName in doc.lstFontNamesCustom) { UpdateStatusBarDocNamePlusOne("Examining '{0}'... Searching for custom formatting with font '{1}'...", strFontName); doc.GetTextIteratorForName(strFontName, doc.XPathFormatGetFontText, ref mapFontNames2Iterator, false); } // also check for inserted symbols foreach (string strFontName in doc.lstFontNamesSymbolText) { UpdateStatusBarDocNamePlusOne("Examining '{0}'... Searching for inserted symbols with font '{1}'...", strFontName); doc.GetTextIteratorForName(strFontName, doc.XPathFormatGetSymbolFontText, ref mapSymbolFontNames2Iterator, true); } }