首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向word文档添加WordprocessingML块

向word文档添加WordprocessingML块
EN

Stack Overflow用户
提问于 2012-08-08 23:47:05
回答 1查看 872关注 0票数 0

是否可以在word文档中添加一块WordprocessingML (而不是整个文档)?我目前有一个docx作为我的模板,它包含一个ContentControl。我想通过AltChunk插入WordprocessingML的一部分,但是我插入的内容呈现为文本,而不是像我希望的那样被实现到文档中。

我目前使用的方法是:

代码语言:javascript
复制
var main = doc.MainDocumentPart;

// Create new AltChunk
string altChunkId = "altChunkId";
AlternativeFormatImportPart chunk = main.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);

// Populate altChunk. stream = MemoryStream containing WordprocessingML
chunk.Feed(stream);

// Replace content control with altChunk info
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;

// Get SdtBlock to replace
SdtBlock block = ContentControlHelpers.GetSdtBlock(doc, "ContentControlId");
OpenXmlElement parent = block.Parent;
parent.InsertAfter(altChunk, block);
block.Remove();

我尝试插入的WordproccessingML示例如下(通过XML +XSLT生成):

代码语言:javascript
复制
<w:p>
  <w:pPr>
    <w:pStyle w:val="heading2" />
  </w:pPr>
  <w:r>
    <w:t>Product 1</w:t>
  </w:r>
</w:p>
<w:p>
  <w:pPr>
    <w:pStyle w:val="heading3" />
  </w:pPr>
  <w:r>
    <w:t>Europe</w:t>
  </w:r>
</w:p>
<w:p>
  <w:pPr>
    <w:pStyle w:val="heading4" />
  </w:pPr>
  <w:r>
    <w:t>France</w:t>
  </w:r>
</w:p>

我已经尝试在它周围添加<w:document><w:body>元素来包装它,但无论我尝试什么,文档只是将WordprocessingML呈现为文本,而不是将其嵌入到文档中。

对于我可能会出错的地方有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2012-08-09 12:53:29

看看DocumentBuilder -开源示例和指南,它展示了如何准确地做你想要做的事情。

http://openxmldeveloper.org/wiki/w/wiki/documentbuilder.aspx

先看下面的视频:

http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2011/08/17/new-screen-cast-short-and-sweet-intro-to-documentbuilder-2-0.aspx

-Eric

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11868287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档