C# 中的复选框和文本之间的自动中断
Auto Break between Checkbox and Text in C#
希望你能帮我解决我的问题。
我想用 C# 程序自动生成 Word 清单。
但我无法将文本放在复选框后面。文本始终位于复选框下方的新行中。
我该如何解决这个问题?
你知道其他一些功能吗?
public void createChecklist()
{
Application app = new Application();
app.Visible = true;
Document doc = app.Documents.Add();
Paragraph para = doc.Paragraphs.Add();
ContentControl checkbox = para.Range.ContentControls.Add(WdContentControlType.wdContentControlCheckBox);
para.Range.InsertAfter("sdjsakd");
doc.SaveAs2("C:\tmp\checklist.docx");
app.Quit();
}
该行为的原因是因为文本的 "target" 是 整个 段落的范围。因此,当后面插入某些内容时,它会插入到该段落之后。
这样做的关键是将范围带回段落内 - 不包括段落标记。 (段落标记在屏幕截图中不可见,因为非打印字符的显示被抑制。单击功能区主页选项卡中的 "backwards P",段落标记应该可见。)
有多种方法可以解决这个问题; Range.MoveEnd
经常使用(下)。
注意:使用 COM "interop" 时 不 清理对象是很危险的。这可能会很快导致 "orphaned" 个应用程序实例留在内存中。我已将其添加到代码示例中,因为问题中的代码退出了 Word 应用程序。
public void createChecklist()
{
Application app = new Application();
app.Visible = true;
Document doc = app.Documents.Add();
Paragraph para = doc.Paragraphs.Add();
Range rng = para.Range;
ContentControl checkbox = rng.ContentControls.Add(WdContentControlType.wdContentControlCheckBox);
rng.MoveEnd(WdUnits.wdCharacter, -1);
rng.InsertAfter(" sdjsakd");
doc.SaveAs2("C:\tmp\checklist.docx");
//Release the COM objects and clean up
checkbox = null;
rng = null;
para = null;
doc = null;
app.Quit();
app = null;
GC.Collect(); GC.AwaitPendingFinalizers();
GC.Collect(); GC.AwaitPendingFinalizers();
}
添加注释以澄清为什么 运行 垃圾回收两次的问题:
此信息来自 Andrew Whitechapel 的“.Net Development for Microsoft Office”,第 2 章,其摘录在 MSDN 上不再可用,因此在此处引用:
Note that we're repeating the calls to Collect
and
WaitForPendingFinalizers
This is because the memory for the [Office
application reference] might have survived the first pass, although it
will then have been marked for collection on the next pass. So we will
make a second pass to clean up anything that survived the first pass
but was otherwise available for collection.
希望你能帮我解决我的问题。
我想用 C# 程序自动生成 Word 清单。
但我无法将文本放在复选框后面。文本始终位于复选框下方的新行中。
我该如何解决这个问题?
你知道其他一些功能吗?
public void createChecklist()
{
Application app = new Application();
app.Visible = true;
Document doc = app.Documents.Add();
Paragraph para = doc.Paragraphs.Add();
ContentControl checkbox = para.Range.ContentControls.Add(WdContentControlType.wdContentControlCheckBox);
para.Range.InsertAfter("sdjsakd");
doc.SaveAs2("C:\tmp\checklist.docx");
app.Quit();
}
该行为的原因是因为文本的 "target" 是 整个 段落的范围。因此,当后面插入某些内容时,它会插入到该段落之后。
这样做的关键是将范围带回段落内 - 不包括段落标记。 (段落标记在屏幕截图中不可见,因为非打印字符的显示被抑制。单击功能区主页选项卡中的 "backwards P",段落标记应该可见。)
有多种方法可以解决这个问题; Range.MoveEnd
经常使用(下)。
注意:使用 COM "interop" 时 不 清理对象是很危险的。这可能会很快导致 "orphaned" 个应用程序实例留在内存中。我已将其添加到代码示例中,因为问题中的代码退出了 Word 应用程序。
public void createChecklist()
{
Application app = new Application();
app.Visible = true;
Document doc = app.Documents.Add();
Paragraph para = doc.Paragraphs.Add();
Range rng = para.Range;
ContentControl checkbox = rng.ContentControls.Add(WdContentControlType.wdContentControlCheckBox);
rng.MoveEnd(WdUnits.wdCharacter, -1);
rng.InsertAfter(" sdjsakd");
doc.SaveAs2("C:\tmp\checklist.docx");
//Release the COM objects and clean up
checkbox = null;
rng = null;
para = null;
doc = null;
app.Quit();
app = null;
GC.Collect(); GC.AwaitPendingFinalizers();
GC.Collect(); GC.AwaitPendingFinalizers();
}
添加注释以澄清为什么 运行 垃圾回收两次的问题:
此信息来自 Andrew Whitechapel 的“.Net Development for Microsoft Office”,第 2 章,其摘录在 MSDN 上不再可用,因此在此处引用:
Note that we're repeating the calls to
Collect
andWaitForPendingFinalizers
This is because the memory for the [Office application reference] might have survived the first pass, although it will then have been marked for collection on the next pass. So we will make a second pass to clean up anything that survived the first pass but was otherwise available for collection.