private static string[] GetLackedStyleIdsWithinBaseFile(string baseFilePath, UserSettingStyleMap userSettingStyleMap)
        {
            var tempFilePath = Path.GetTempFileName();

            try
            {
                using (var manipulator = new WordDocumentManipulator(baseFilePath, tempFilePath, true))
                {
                    // Gathering the lacked style IDs.
                    var lackedStyleIds = new List <string>();
                    foreach (var styleId in userSettingStyleMap.StyleMap.Values)
                    {
                        if (!manipulator.StyleManager.ContainsStyleId(styleId))
                        {
                            lackedStyleIds.Add(styleId);
                        }
                    }

                    return(lackedStyleIds.ToArray());
                }
            }
            finally
            {
                if (File.Exists(tempFilePath))
                {
                    File.Delete(tempFilePath);
                }
            }
        }
        public void ConvertDocument(string sourceMarkdownFilePath, string outputDocxFilePath)
        {
            // Check the style existence within the docx document.
            var lackedStyleIds = GetLackedStyleIdsWithinBaseFile(BaseFilePath, UserSettingStyleMap);

            if (lackedStyleIds.Length != 0)
            {
                var ex = new LackOfStyleWithinBaseFileException(string.Format("The base file does not contain some style IDs: {0}", string.Join(", ", lackedStyleIds)));
                ex.Data.Add("LackedStyleIds", lackedStyleIds);
                throw ex;
            }

            var mdDoc = ParseMarkdownDocument(sourceMarkdownFilePath);

            // Build the Word document based-on the base docx file.
            using (var manipulator = new WordDocumentManipulator(BaseFilePath, outputDocxFilePath, true))
            {
                var baseFolderPathForRelativePath = Path.GetDirectoryName(sourceMarkdownFilePath);
                var blockConverter = new BlockConverter(manipulator, UserSettingStyleMap, baseFolderPathForRelativePath);

                foreach (var block in mdDoc)
                {
                    var elements = blockConverter.Convert(block);
                    manipulator.AppendElementsToDocumentBody(elements);
                }

                // Save the Word document.
                manipulator.Save();
            }
        }
예제 #3
0
        private static void PrintStyleInformation(string docxFilePath)
        {
            var tempFilePath = Path.GetTempFileName();

            try
            {
                using (var manipulator = new WordDocumentManipulator(docxFilePath, tempFilePath, true))
                {
                    var styleSummaries = manipulator.StyleManager.GetStyleSummaries();

                    Console.WriteLine(@"The {0} styles contained within ""{1}"" file.", styleSummaries.Length, docxFilePath);

                    foreach (var styleSammary in styleSummaries)
                    {
                        Console.WriteLine(@"  ID:{0}, Type:{1}, Name:""{2}""", styleSammary.StyleId, styleSammary.StyleType.ToString(), styleSammary.StyleName);
                    }
                }
            }
            finally
            {
                if (File.Exists(tempFilePath))
                {
                    File.Delete(tempFilePath);
                }
            }
        }
예제 #4
0
 public InlineConverter(WordDocumentManipulator manipulator, UserSettingStyleMap userSettingStyleMap, string baseFolderPathForRelativePath)
 {
     Manipulator                   = manipulator;
     UserSettingStyleMap           = userSettingStyleMap;
     BaseFolderPathForRelativePath = baseFolderPathForRelativePath;
 }