[英]Adding a chunk of WordprocessingML to word document
是否可以在單詞文檔(而不是整個文檔)中添加“塊”的WordprocessingML? 我目前有一個docx用作模板,其中包含一個ContentControl。 我想通過AltChunk插入WordprocessingML的一部分,但是我插入的內容呈現為文本,而不是像我希望的那樣實現到文檔中。
我當前使用的方法是:
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生成):
<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呈現為文本(如上所示),而不是將其嵌入文件。
關於我可能在哪里出錯的任何建議?
看一下DocumentBuilder-開源示例和指南,該示例顯示了如何精確執行所需的操作。
http://openxmldeveloper.org/wiki/w/wiki/documentbuilder.aspx
首先觀看以下視頻:
-埃里克
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.