将内容控件添加到现有段落

时间:2014-10-04 04:36:27

标签: vsto openxml wordml

我有一个方法,除了一个单词段落,段落内容和任何评论内容(如果有的话)。作为跟踪文档中哪些段落已被处理的方法,我试图在openxml注入段落周围添加内容控件。它爆炸了,我不确定从哪里开始,因为我对openxml相当新。我的代码示例如下:

    public void Handle(Paragraph paragraph, string docPartXml, string commentsPartXml)
    {

        var docXml = docPartXml;
        var commentsXml = commentsPartXml;

        using (var res = paragraph.Range.WithComCleanup())
        {
            var range = res.Resource;
            using (var packageStream = range.GetPackageStreamFromRange())
            using (var document = WordprocessingDocument.Open(packageStream, true))
            {
                var pbody = document.MainDocumentPart.Document.Body;
                pbody.InnerXml = docXml + "<w:p></w:p>";  // paragraphs are merged/combined w/o this
                document.MainDocumentPart.Document.Save();

                WordprocessingCommentsPart commentsPart = null;
                if (document.MainDocumentPart.WordprocessingCommentsPart == null)
                {
                    commentsPart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>();
                    document.MainDocumentPart.AddPart(commentsPart);
                    document.MainDocumentPart.Document.Save();
                    commentsPart = document.MainDocumentPart.WordprocessingCommentsPart;
                    commentsPart.Comments = new Comments();
                }
                if (commentsPart == null)
                    commentsPart = document.MainDocumentPart.WordprocessingCommentsPart;

                commentsPart.Comments.InnerXml += commentsXml;
                commentsPart.Comments.Save();

                document.MainDocumentPart.Document.Save();

                document.Package.Flush();
                XDocument xdoc = OpenXmlExtensions.OpcToFlatOpc(document.Package);
                var openxml = xdoc.ToString();

                var validator = new OpenXmlValidator();
                var errors = validator.Validate(document);

                // todo - provide more detailed info for the log
                foreach (var err in errors)
                    _logger.Warn(err.Description, "OpenXml Injection");

                try
                {
                    range.InsertXML(openxml);
                }
                catch (COMException e)
                {
                    _logger.Error("Unable to insert xml into paragraph");
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

使用以下内容替换pbody.InnerXml = ...代码行,它可以正常工作。

pbody.RemoveAllChildren<Paragraph>();
pbody.RemoveAllChildren<SdtBlock>();
pbody.RemoveAllChildren<SdtProperties>();
var sdtBlock1 = new SdtBlock();
var sdtPr = new SdtProperties(
    new SdtAlias {Val = string.Format("{0}-{1}", "CC", hash)},
    new Tag {Val = string.Format("{0}-{1}", hash, "CC")});
                        //new Lock(){ Val = LockingValues.ContentLocked});
sdtBlock1.AppendChild(sdtPr);
var sdtContentBlock1 = new SdtContentBlock {

InnerXml = docXml};
sdtBlock1.AppendChild(sdtContentBlock1);

pbody.AppendChild( sdtBlock1);