public void CopyToFile_Test_CopiesCorrectHyperlinksToBookmarks()
        {
            Range        range        = _application.Documents.Add().Range();
            const string bookmarkName = "bookmark1";

            Interop.Paragraph paragraph1 = range.Paragraphs.Add();
            paragraph1.Range.Text = "bookmark";
            Interop.Bookmark bookmark = paragraph1.Range.Bookmarks.Add(bookmarkName);

            range.Paragraphs.Add();
            Interop.Paragraph paragraph2 = range.Paragraphs.Add();
            paragraph2.Range.Text = "link";
            range.Hyperlinks.Add(paragraph2.Range, SubAddress: bookmark);

            _tmpFile = new OpenXmlHelper().CopyToFile(range);

            using (WordprocessingDocument doc = WordprocessingDocument.Open(_tmpFile, true)) {
                OpenXml.Body body = doc.MainDocumentPart
                                    .Document
                                    .Body;

                OpenXml.BookmarkStart bookmarkStart = body.Descendants <OpenXml.BookmarkStart>().First();
                OpenXml.Hyperlink     hyperlink     = body.Descendants <OpenXml.Hyperlink>().First();

                Assert.That(bookmarkStart.Name.Value, Is.EqualTo(bookmarkName));
                Assert.That(hyperlink.Anchor.Value, Is.EqualTo(bookmarkName));
            }
        }
        private bool CheckPicture(OpenXmlBookmarkStart bookmarkstart, ref ImagePart imagePart)
        {
            var document = bookmarkstart.Parent;

            while (document != null)
            {
                if (document is DocumentFormat.OpenXml.Wordprocessing.Document)
                {
                    break;
                }

                document = document.Parent;
            }

            if (document == null)
            {
                return(false);
            }

            var doc      = document as DocumentFormat.OpenXml.Wordprocessing.Document;
            var mainpart = doc.MainDocumentPart;
            var run      = bookmarkstart.NextSibling <Run>();
            var pic      = run?.GetFirstChild <Drawing>();

            if (pic != null)
            {
                var blipElement = pic.Descendants <DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault();
                if (blipElement != null)
                {
                    var imageId = blipElement.Embed.Value;
                    imagePart = (ImagePart)mainpart.GetPartById(imageId);
                }

                return(true);
            }

            return(false);
        }
        private bool CheckParentTable(OpenXmlBookmarkStart bookmarkstart, out DocumentFormat.OpenXml.Wordprocessing.Table table)
        {
            if (bookmarkstart.NextSibling() is DocumentFormat.OpenXml.Wordprocessing.Table)
            {
                table = bookmarkstart.NextSibling() as DocumentFormat.OpenXml.Wordprocessing.Table;
                return(true);
            }

            var parent = bookmarkstart.Parent;

            while (parent != null)
            {
                if (parent is DocumentFormat.OpenXml.Wordprocessing.Table)
                {
                    table = parent as DocumentFormat.OpenXml.Wordprocessing.Table;
                    return(true);
                }

                parent = parent.Parent;
            }

            table = null;
            return(false);
        }
        /// <summary>
        /// xml配置文件中添加的书签替换
        /// </summary>
        /// <param name="bookmarkstart"></param>
        /// <param name="bookMarkConfs"></param>
        /// <param name="ctx"></param>
        /// <param name="logger"></param>
        /// <param name="missMatch"></param>
        private bool UpdateBookMarksByCfg(OpenXmlBookmarkStart bookmarkstart, BookMarkConfs bookMarkConfs, ReplacerContext <T> ctx, Action <string> logger, Action <string> missMatch)
        {
            if (null == bookMarkConfs || bookMarkConfs.Confs.Count < 1)
            {
                logger?.Invoke("配置文件:宏替换器内容为空");
                return(false);
            }

            if (!bookMarkConfs.Confs.ContainsKey(ctx.DestFileNameWithoutExt))
            {
                logger?.Invoke($"配置文件:没有找到{ctx.DestFileNameWithoutExt}宏替换器");
                return(false);
            }

            var bookMarks = bookMarkConfs.Confs[ctx.DestFileNameWithoutExt].Marks;

            if (bookMarks.ContainsKey(bookmarkstart.Name.Value.ToUpper()))
            {
                var bookMark = bookMarks[bookmarkstart.Name.Value.ToUpper()];
                switch (bookMark.Type)
                {
                case "TEXT":
                {
                    ctx.BookmarkText = ctx.PreprocessText(bookMark.Text);
                    ctx.BookmarkSql  = ctx.PreprocessSql(bookMark.Sql);

                    var bookmarkText = bookmarkstart.NextSibling <Run>();
                    var text         = bookmarkText?.GetFirstChild <Text>();
                    if (text != null)
                    {
                        _handler.CustomText(text, ctx, logger);
                        return(true);
                    }
                    else
                    {
                        logger?.Invoke($"书签[{bookmarkstart.Name.Value}]:不是文本书签");
                    }
                }
                break;

                case "PICTURE":
                {
                    ctx.BookmarkPicUri = ctx.PreprocessPicUri(bookMark.PicUri);
                    ImagePart imagepart = null;
                    if (CheckPicture(bookmarkstart, ref imagepart))
                    {
                        _handler.CustomPicture(imagepart, ctx, logger);
                        return(true);
                    }
                    else
                    {
                        logger?.Invoke($"书签[{bookmarkstart.Name.Value}]:不是图片书签");
                    }
                }
                break;

                case "TABLE":
                {
                    ctx.BookmarkSql = ctx.PreprocessSql(bookMark.Sql);
                    DocumentFormat.OpenXml.Wordprocessing.Table table;
                    if (CheckParentTable(bookmarkstart, out table))
                    {
                        _handler.CustomTable(table, ctx, logger);
                        return(true);
                    }
                    else
                    {
                        logger?.Invoke($"书签[{bookmarkstart.Name.Value}]:不是表格书签");
                    }
                }
                break;

                default:
                {
                    logger?.Invoke($"书签[{bookmarkstart.Name.Value}]:类型定义{bookMark.Type}不正确(TEXT/PICTURE/TABLE之一)");
                }
                    return(false);
                }

                logger?.Invoke($"书签[{bookmarkstart.Name.Value}]:替换完成");
            }
            else
            {
                logger?.Invoke($"书签[{bookmarkstart.Name.Value}]:没有找到宏替换器");
            }
            return(false);
        }
        public void AddTextToWord(WordprocessingDocument doc, string text, bool bold, bool italic, bool underline, int heading, int slideNumber)
        {
            MainDocumentPart mainPart = doc.MainDocumentPart;
            WordDoc.Body body = mainPart.Document.Body;
            WordDoc.Paragraph para = new WordDoc.Paragraph();
            foreach (WordDoc.Paragraph p in body.Elements<WordDoc.Paragraph>())
            {
                para = p;
            }
            if (heading != 0)
            {
                para = body.AppendChild(new WordDoc.Paragraph());
                para.ParagraphProperties = new WordDoc.ParagraphProperties(new WordDoc.ParagraphStyleId() { Val = "Heading" + heading.ToString() });
                bold = false;
                italic = false;
                underline = false;
                WordDoc.BookmarkStart bs = new WordDoc.BookmarkStart();
                bs.Id = slideNumber.ToString();
                bs.Name = text;
                bs = para.AppendChild(bs);
            }
            else
            {
                if (para.ParagraphProperties != null)
                {
                    if (para.ParagraphProperties.ParagraphStyleId != null)
                    {
                        if (para.ParagraphProperties.ParagraphStyleId.Val == "Heading1" ||
                          para.ParagraphProperties.ParagraphStyleId.Val == "Heading2" ||
                          para.ParagraphProperties.ParagraphStyleId.Val == "Heading3")
                        {
                            para = body.AppendChild(new WordDoc.Paragraph());
                        }
                    }
                }
            }

            string[] t1 = text.Split('\n');
            for (int j = 0; j < t1.Length; j++)
            {

                WordDoc.Run run = para.AppendChild(new WordDoc.Run());
                WordDoc.RunProperties rp = run.AppendChild(new WordDoc.RunProperties());
                if (bold)
                {
                    WordDoc.Bold b = rp.AppendChild(new WordDoc.Bold());
                }
                if (italic)
                {
                    WordDoc.Italic i = rp.AppendChild(new WordDoc.Italic());
                }
                if (underline)
                {
                    WordDoc.Underline u = new WordDoc.Underline()
                    {
                        Val = WordDoc.UnderlineValues.Single
                    };
                    rp.AppendChild(u);
                }
                WordDoc.Text t = new WordDoc.Text()
                {
                    Text = t1[j],
                    Space = SpaceProcessingModeValues.Preserve
                };
                run.AppendChild(t);
                if (j != t1.Length - 1)
                    run.AppendChild(new WordDoc.Break());

            }
            WordDoc.BookmarkEnd be = new WordDoc.BookmarkEnd();
            be.Id = slideNumber.ToString();
            be = para.AppendChild(be);
        }