C# 用c在word中插入一个等式#
我想用C#为Microsoft Word添加一个等式。例如,我想添加以下内容: 方程式如下:C# 用c在word中插入一个等式#,c#,winforms,ms-word,C#,Winforms,Ms Word,我想用C#为Microsoft Word添加一个等式。例如,我想添加以下内容: 方程式如下: private void writep(_Document oDoc, string text, int font, bool bold) { Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(); oPara1.Range.Font.Size = font
private void writep(_Document oDoc, string text, int font, bool bold)
{
Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add();
oPara1.Range.Font.Size = font;
oPara1.Range.Text = text;
oPara1.Range.Font.Name = "Arial";
oPara1.ReadingOrder = WdReadingOrder.wdReadingOrderRtl;
int q = 0;
if (bold)
q = 1;
oPara1.Range.Font.Bold = q;
oPara1.Range.InsertParagraphAfter();
}
$\sqrt{a^2+b^3}$
为此,我使用以下代码:
private void button2_Click(object sender, EventArgs e)
{
object oMissing = System.Reflection.Missing.Value;
_Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
writep(oDoc, "The equation is as follows:", 16, true);
oWord.Selection.OMaths.Add(oWord.Selection.Range);
OMathFunction E = oWord.Selection.OMaths[1].Functions.Add(oWord.Selection.Range,
WdOMathFunctionType.wdOMathFunctionBox);
OMathBox B = E.Box;
B.E.Range.Text = \\ \sqrt{a^2+b^3};
}
其中[writep]是在word中插入段落的函数。现在我有这些问题:
1-我无法在文本后插入公式。当我运行这段代码时,等式被插入到页面顶部。我想把它插入到另一段文字的右后,左对齐。我曾经
2-如何在方程式中写入方程式$\sqrt{a^2+b^3}$?是否有任何说明方程式编写方式的参考资料
提前谢谢
编辑:写操作如下:
private void writep(_Document oDoc, string text, int font, bool bold)
{
Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add();
oPara1.Range.Font.Size = font;
oPara1.Range.Text = text;
oPara1.Range.Font.Name = "Arial";
oPara1.ReadingOrder = WdReadingOrder.wdReadingOrderRtl;
int q = 0;
if (bold)
q = 1;
oPara1.Range.Font.Bold = q;
oPara1.Range.InsertParagraphAfter();
}
您看到的问题来自未指定除选择之外的任何其他目标位置。将内容添加到单词
范围时
不会更改选择
。因此,Selection
不会自动移动到您添加的内容的末尾。因此,方程式始终位于文档的开头
如果你想让方程式紧跟在你插入的段落之后,那么你必须得到该段落或其范围
我更改了writep
以返回一个Word.Range对象,我在调用过程中使用该对象将范围折叠到其端点,然后将等式添加到该范围
请注意,OMATH.Add需要范围内的文本内容。在创建OMath对象之前,我在这个范围内放置了一个等式
另外,请注意,我对您的代码(Word对象的声明方式)做了一些更改,以便它们在我的测试环境中工作,因此您不能简单地复制/粘贴
private Word.Range writep(Word.Document oDoc, string text, int font, bool bold)
{
Word.Paragraph oPara1 = oDoc.Content.Paragraphs.Add();
Word.Range rng = oPara1.Range;
rng.Font.Size = font;
rng.Text = text;
rng.Font.Name = "Arial";
oPara1.ReadingOrder = Word.WdReadingOrder.wdReadingOrderRtl;
int q = 0;
if (bold)
q = 1;
rng.Font.Bold = q;
rng.InsertParagraphAfter();
return rng;
}
private void button1_Click(object sender, EventArgs e)
{
object oMissing = System.Reflection.Missing.Value;
Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oDoc;
Word.Range rng = null;
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
rng = writep(oDoc, "The equation is as follows:", 16, true);
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
rng.Collapse(ref oCollapseEnd);
rng.Text = "\\sqrt{a^2+b^3}";
rng.OMaths.Add(rng);
Word.OMathFunction E = rng.OMaths[1].Functions.Add(rng,
Word.WdOMathFunctionType.wdOMathFunctionBox);
}
尝试使用Office Open XML库而不是旧的com库,这样即使没有运行word,也可以直接插入到文件中,