/// <summary> /// 清空所有未使用的占位符用空字符串 /// </summary> /// <param name="doc"></param> /// <param name="listAllPlaceHolder"></param> private static void ClearPlaceholders(IEnumerable <string> placeholders, XWPFDocument doc) { Dictionary <string, string> replacements = new Dictionary <string, string>(); foreach (var placeHolder in placeholders) { replacements.Add(placeHolder, string.Empty); } IEnumerator <XWPFParagraph> paragraphEnumerator = doc.GetParagraphsEnumerator(); XWPFParagraph paragraph; while (paragraphEnumerator.MoveNext()) { paragraph = paragraphEnumerator.Current; ReplaceInParagraph(replacements, null, paragraph); } IEnumerator <XWPFTable> tableEnumerator = doc.GetTablesEnumerator(); XWPFTable table; while (tableEnumerator.MoveNext()) { table = tableEnumerator.Current; ReplacePlaceholderInTable(table, replacements, null); } }
/// <summary> /// 替换Word中的占位符 /// </summary> /// <param name="doc">word</param> /// <param name="placeHolderAndValueDict">占位符:值 字典</param> /// <param name="hook">钩子委托</param> public static void ReplacePlaceholdersInWord(XWPFDocument doc, Dictionary <string, string> stringReplacements , Dictionary <string, IEnumerable <Picture> > pictureReplacements) { IEnumerator <XWPFParagraph> paragraphEnumerator = doc.GetParagraphsEnumerator(); XWPFParagraph paragraph; while (paragraphEnumerator.MoveNext()) { paragraph = paragraphEnumerator.Current; ReplaceInParagraph(stringReplacements, pictureReplacements, paragraph); } IEnumerator <XWPFTable> tableEnumerator = doc.GetTablesEnumerator(); XWPFTable table; while (tableEnumerator.MoveNext()) { table = tableEnumerator.Current; ReplacePlaceholderInTable(table, stringReplacements, pictureReplacements); } var placeholders = stringReplacements.Select(x => x.Key).Union(pictureReplacements.Select(x => x.Key)); ClearPlaceholders(placeholders, doc); }
/// <summary> /// 清空所有未使用的占位符用空字符串 /// </summary> /// <param name="doc"></param> /// <param name="listAllPlaceHolder"></param> private static void ClearNotUsedPlaceHolder(List <string> listAllPlaceHolder, XWPFDocument doc) { Dictionary <string, string> placeHolderAndValueDict = new Dictionary <string, string>(); foreach (var placeHolder in listAllPlaceHolder) { placeHolderAndValueDict.Add(placeHolder, string.Empty); } IEnumerator <XWPFParagraph> paragraphEnumerator = doc.GetParagraphsEnumerator(); XWPFParagraph paragraph; while (paragraphEnumerator.MoveNext()) { paragraph = paragraphEnumerator.Current; ReplacePlaceHolderInParagraph(paragraph, placeHolderAndValueDict); } IEnumerator <XWPFTable> tableEnumerator = doc.GetTablesEnumerator(); XWPFTable table; while (tableEnumerator.MoveNext()) { table = tableEnumerator.Current; ReplacePlaceHolderInTable(table, placeHolderAndValueDict); } }
/// <summary> /// 替换Word中的所有占位符 /// </summary> /// <param name="doc"></param> /// <param name="basicReplacements"></param> /// <param name="pictureReplacements"></param> public static void ReplaceInWord(XWPFDocument doc, List <Models.Word.ReplacementBasic> basicReplacements, List <Models.Word.ReplacementGrid> gridReplacements) { IEnumerator <XWPFParagraph> allParagraphs = doc.GetParagraphsEnumerator(); XWPFParagraph paragraph; while (allParagraphs.MoveNext()) { paragraph = allParagraphs.Current; ReplaceInParagraph(paragraph, basicReplacements); } IEnumerator <XWPFTable> allTables = doc.GetTablesEnumerator(); XWPFTable table; while (allTables.MoveNext()) { table = allTables.Current; ReplaceInTable(table, basicReplacements, gridReplacements); } }
/// <summary> /// 替换Word中的占位符 /// </summary> /// <param name="doc">word</param> /// <param name="placeHolderAndValueDict">占位符:值 字典</param> /// <param name="hook">钩子委托</param> public static void ReplacePlaceHolderInWord(XWPFDocument doc, Dictionary<string, string> placeHolderAndValueDict, List<AddPictureOptions> listAddPictureOptions, List<string> listAllPlaceHolder) { IEnumerator<XWPFParagraph> paragraphEnumerator = doc.GetParagraphsEnumerator(); XWPFParagraph paragraph; while (paragraphEnumerator.MoveNext()) { paragraph = paragraphEnumerator.Current; ReplacePlaceHolderInParagraph(paragraph, placeHolderAndValueDict); AddPicture(paragraph, listAddPictureOptions); } IEnumerator<XWPFTable> tableEnumerator = doc.GetTablesEnumerator(); XWPFTable table; while (tableEnumerator.MoveNext()) { table = tableEnumerator.Current; ReplacePlaceHolderInTable(table, placeHolderAndValueDict); AddPictureInTable(table, listAddPictureOptions); } ClearNotUsedPlaceHolder(listAllPlaceHolder, doc); }