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);
                }
            }
        }
Ejemplo n.º 2
0
        private Table ConvertTableBlock(MDT.Table tableBlock, int numberingId, int nestLevel)
        {
            var styleId = UserSettingStyleMap.GetStyleId(UserSettingStyleMap.StyleMapKeyType.Table, null);

            var oxmlTable = Manipulator.ElementCreator.CreateTableElement(styleId, numberingId, nestLevel);

            foreach (MDT.TableRow tableRow in tableBlock)
            {
                var oxmlTableRow = Manipulator.ElementCreator.CreateTableRowElement();
                foreach (MDT.TableCell tableCell in tableRow)
                {
                    var oxmlTableCell = Manipulator.ElementCreator.CreateTableCellElement();
                    foreach (ParagraphBlock paragraphBlock in tableCell)
                    {
                        var oxmlParagraph = ConvertParagraphBlock(paragraphBlock, WordDocumentNumberingManager.OutsideOfListNumberingId, 0);
                        oxmlTableCell.Append(oxmlParagraph);
                    }

                    oxmlTableRow.Append(oxmlTableCell);
                }

                oxmlTable.Append(oxmlTableRow);
            }

            return(oxmlTable);
        }
Ejemplo n.º 3
0
        private OpenXmlElement ConvertLinkInline(LinkInline linkInline, bool isBoldInherited = false, bool isItalicInherited = false)
        {
            if (linkInline.IsImage)
            {
                var explicitAbsoluteImagePath = GetExplicitAbsoluteImagePath(linkInline.Url, BaseFolderPathForRelativePath);
                var relationshipId            = AddImagePartFromFile(explicitAbsoluteImagePath);

                // At this time, temporary uses the original image dimension.
                // In later, adjust the image dimension using the page settings and inherited indentation.
                (var originalImageWidthInch, var originalImageHeightInch) = Manipulator.GetImageDimensionInInch(relationshipId);
                var originalImageWidthEmu  = WordDocumentManipulator.UnitConverter.InchToEmu(originalImageWidthInch);
                var originalImageHeightEmu = WordDocumentManipulator.UnitConverter.InchToEmu(originalImageHeightInch);

                var fileName = Path.GetFileName(explicitAbsoluteImagePath);
                var altText  = GetLinkText(linkInline);
                return(Manipulator.ElementCreator.CreateImageElement(relationshipId, (long)originalImageWidthEmu, (long)originalImageHeightEmu, fileName, altText));
            }
            else
            {
                var linkText = GetLinkText(linkInline);

                var hyperlinkUri            = GetLinkTargetUri(linkInline.Url);
                var hyperlinkRelationshipId = Manipulator.AddHyperlinkRelationship(hyperlinkUri);

                var styleId = UserSettingStyleMap.GetStyleId(UserSettingStyleMap.StyleMapKeyType.Hyperlink, null);
                return(Manipulator.ElementCreator.CreateHyperlinkElement(linkText, hyperlinkRelationshipId, isBoldInherited, isItalicInherited, styleId));
            }
        }
Ejemplo n.º 4
0
        private OpenXmlElement[] ConvertCodeBlock(CodeBlock codeBlock, int numberingId, int nestLevel)
        {
            string styleId;

            if (codeBlock.GetType() == typeof(FencedCodeBlock))
            {
                // For the code block starting with ``` line and ending with ``` line.
                styleId = GetFencedCodeBlockStyleId(((FencedCodeBlock)codeBlock).Info);
            }
            else
            {
                // For the code block by indent.
                styleId = UserSettingStyleMap.GetStyleId(UserSettingStyleMap.StyleMapKeyType.Code, null);
            }

            var oxmlParagraphs = new List <OpenXmlElement>();

            for (int i = 0; i < codeBlock.Lines.Count; i++)
            {
                var lineText      = codeBlock.Lines.Lines[i].Slice.ToString();
                var oxmlParagraph = Manipulator.ElementCreator.CreateCodeElement(lineText, styleId, numberingId, nestLevel);
                oxmlParagraphs.Add(oxmlParagraph);
            }

            return(oxmlParagraphs.ToArray());
        }
Ejemplo n.º 5
0
        private Paragraph ConvertParagraphBlock(ParagraphBlock paragraphBlock, int numberingId, int nestLevel)
        {
            var inlineConverter    = new InlineConverter(Manipulator, UserSettingStyleMap, BaseFolderPathForRelativePath);
            var oxmlInlineElements = inlineConverter.Convert(paragraphBlock.Inline);

            var styleId       = UserSettingStyleMap.GetStyleId(UserSettingStyleMap.StyleMapKeyType.Paragraph, null);
            var oxmlParagraph = Manipulator.ElementCreator.CreateParagraphElement(oxmlInlineElements, styleId, numberingId, nestLevel);

            Manipulator.AdjustImageDimension(oxmlParagraph);

            return(oxmlParagraph);
        }
Ejemplo n.º 6
0
        private Paragraph ConvertHeadingBlock(HeadingBlock headingBlock)
        {
            var inlineConverter    = new InlineConverter(Manipulator, UserSettingStyleMap, BaseFolderPathForRelativePath);
            var oxmlInlineElements = inlineConverter.Convert(headingBlock.Inline);

            var styleId = UserSettingStyleMap.GetStyleId(UserSettingStyleMap.StyleMapKeyType.Heading, new UserSettingStyleMap.HeadingStyleMapArgs()
            {
                Level = headingBlock.Level
            });
            var oxmlHeading = Manipulator.ElementCreator.CreateHeadingElement(oxmlInlineElements, styleId);

            Manipulator.AdjustImageDimension(oxmlHeading);

            return(oxmlHeading);
        }
Ejemplo n.º 7
0
        private string GetFencedCodeBlockStyleId(string adhocStyleId)
        {
            if (string.IsNullOrWhiteSpace(adhocStyleId))
            {
                return(UserSettingStyleMap.GetStyleId(UserSettingStyleMap.StyleMapKeyType.Code, null));
            }

            if (Manipulator.StyleManager.ContainsStyleId(adhocStyleId))
            {
                return(adhocStyleId);
            }
            else
            {
                var ex = new LackOfStyleWithinBaseFileException(string.Format("The base file does not contain the style ID: {0}", adhocStyleId));
                ex.Data.Add("LackedStyleIds", new string[] { adhocStyleId });
                throw ex;
            }
        }
Ejemplo n.º 8
0
        private OpenXmlElement[] ConvertQuoteBlock(QuoteBlock quoteBlock, int numberingId, int nestLevel)
        {
            var styleId = UserSettingStyleMap.GetStyleId(UserSettingStyleMap.StyleMapKeyType.Quote, null);

            var oxmlElements = new List <OpenXmlElement>();

            foreach (var block in quoteBlock)
            {
                switch (block)
                {
                case ParagraphBlock paragraphBlock:
                    var oxmlParagraph      = ConvertParagraphBlock(paragraphBlock, WordDocumentNumberingManager.OutsideOfListNumberingId, 0);
                    var oxmlQuoteParagraph = Manipulator.ElementCreator.CreateQuoteElement(oxmlParagraph, styleId, numberingId, nestLevel);

                    Manipulator.AdjustImageDimension(oxmlQuoteParagraph);

                    oxmlElements.Add(oxmlQuoteParagraph);
                    break;

                case ListBlock listBlock:
                    var oxmlListItems = ConvertListBlock(listBlock, WordDocumentNumberingManager.OutsideOfListNumberingId, 0);
                    foreach (var oxmlListItem in oxmlListItems)
                    {
                        if (oxmlListItem.GetType() == typeof(Paragraph))
                        {
                            var oxmlListItemParagraph      = (Paragraph)oxmlListItem;
                            var oxmlListItemQuoteParagraph = Manipulator.ElementCreator.CreateQuoteElement(oxmlListItemParagraph, styleId, numberingId, nestLevel);

                            Manipulator.AdjustImageDimension(oxmlListItemQuoteParagraph);

                            oxmlElements.Add(oxmlListItemQuoteParagraph);
                        }
                    }
                    break;

                default:
                    throw new NotImplementedException(string.Format("Unknown block type within the quote block: {0}", block.GetType().FullName));
                }
            }

            return(oxmlElements.ToArray());
        }
 public MarkdownToDocxConverter(string baseFilePath, IReadOnlyDictionary <string, string> userSettingStyleMap)
 {
     BaseFilePath        = baseFilePath ?? throw new ArgumentNullException(nameof(baseFilePath), "Cannot be set null for this parameter.");
     UserSettingStyleMap = new UserSettingStyleMap(userSettingStyleMap);
 }
Ejemplo n.º 10
0
 public InlineConverter(WordDocumentManipulator manipulator, UserSettingStyleMap userSettingStyleMap, string baseFolderPathForRelativePath)
 {
     Manipulator                   = manipulator;
     UserSettingStyleMap           = userSettingStyleMap;
     BaseFolderPathForRelativePath = baseFolderPathForRelativePath;
 }