我需要一些东西作为占位符.我起初将内容控制视为一种解决方案,但我遇到了一些问题.
然后我考虑将.docx中的CustomXML添加到其中,但由于i4i的诉讼而拒绝了.
然后我决定通过OpenXML SDK 2.0简单地改变内容控制的文本,但即使它如此标记,内容控制也不会消失.我想它不知道文本发生了变化,除非它发生在单词内部.
我或许可以删除CC并放置文本,但我担心它可能带来的格式和样式问题,而且它会违反内容控制的目的.
然后我开始想知道我是否可以定义Word可以识别的自己的占位符.也许通过积木.它不需要做任何事情,除了易于使用OpenXML找到并以某种方式可标记,所以我知道用什么来替换它.我不确定可以用Building Blocks做什么,但我希望它能够做到.
不确定哪种解决方案最适合我,但我需要的是:
a)一些容易放在模板中的东西,也许是预定义的内容控件占位符,你可以放置在你想要的地方并按照你喜欢的样式.
b)添加数据后,删除所有占位符,不再修改.它保持在占位符中定义的样式/格式.
要收回,我需要回答
如何在OpenXML SDK中编辑内容控件,以便在添加文本后删除它们.
-要么-
我可以为Word文档定义自己的自定义OpenXML标记,然后我可以替换它吗?
我正在使用互操作,我想得到word文档中包含的所有内容控件的列表(在正文,形状,页眉,页脚..).这是正确的,也是最好的方法:
public static List<ContentControl> GetAllContentControls(Document wordDocument)
{
if (null == wordDocument)
throw new ArgumentNullException("wordDocument");
List<ContentControl> ccList = new List<ContentControl>(); ;
// Body cc
var inBodyCc = (from r in wordDocument.ContentControls.Cast<ContentControl>()
select r);
ccList.AddRange(inBodyCc);
// cc within shapes
foreach (Shape shape in wordDocument.Shapes)
{
if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
{
ccList.AddRange(WordDocumentHelper.GetContentControlsInRange(shape.TextFrame.TextRange));
}
}
// Get the list of cc in the story ranges : wdFirstPageHeaderStory, wdFirstPageFooterStory, wdTextFrameStory (textbox)...
foreach (Range range in wordDocument.StoryRanges)
{
ccList.AddRange(WordDocumentHelper.GetContentControlsInRange(range));
}
return ccList;
}
public static List<ContentControl> …
Run Code Online (Sandbox Code Playgroud) 在我们的VSTO Word 2010 Addin中,我们尝试在给定的其他ContentControl之后插入RichTextControl.我们试过这个:
public ContentControl AddContentControl(WdContentControlType type, int position)
{
Paragraph paragraphBefore = null;
if (position == 0)
{
if (WordDocument.Paragraphs.Count == 0)
{
WordDocument.Paragraphs.Add();
}
paragraphBefore = WordDocument.Paragraphs.First;
}
else
{
paragraphBefore = Controls.ElementAt(position - 1).Range.Paragraphs.Last;
}
object start = paragraphBefore.Range.End;
object end = paragraphBefore.Range.End + 1;
paragraphBefore.Range.InsertParagraphAfter();
Range rangeToUse = WordDocument.Range(ref start, ref end);
ContentControl newControl = _ContentControl = _WordDocument.ContentControls.Add(type, rangeToInsert);
Controls.Insert(position, newControl);
OnNewContentControl(newControl, position);
return newControl.ContentControl;
}
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,除非我们想要插入的控件在结尾处有一个空段落.如果是这种情况,则新的ContentControl将插入到最后一个控件中.
我们怎能避免这种情况?