这是我关于StackOverflow的第一个问题,所以请耐心等待:D
我实际上是想通过Visual Studio环境中的Microsoft.Office.Interop.Word lib从.txt文件生成.docx文件(我用C#编码)。
这些Word文件适用于有特殊帮助(颜色,大小等)的儿童。
某些txt文件包含我想在Word上使用特定工具“OMath”重新创建的等式(如果我理解的话,将用于创建那些“等式”框)。
当时,我没有成功生成超过1个盒子。例如,如果txt有2个或更多个等式,则所有这些等式都在同一个等式框中创建,但不是单独创建的。
我尝试过这样的代码:
using Word = Microsoft.Office.Interop.Word;
...
Word.Application wapp = new Word.Application();
Word.Document wdoc = wapp.Documents.Add();
...
wpara = wdoc.Paragraphs.Add();
e.ElementAspect.setFormat(ref wpara); // set wpara format (color, font ...)
Word.Range test1 = wapp.Selection.Range;
test1.Text = e.Content;
test1 = wpara.Range.OMaths.Add(test1);
Word.OMath equation = test1.OMaths[1];
equation.BuildUp();
答案 0 :(得分:1)
首先,您不需要使用wapp.Selection
对象。 Selection
用于文档中的当前光标/用户位置(简称) - Selection interface
其次,使用我测试过的wpara = wdoc.Paragraphs.Add()
,为您提供当前选择的段落。
此代码(如您所遇到的)将在每个迭代prevoius方程中覆盖。
for (int i = 1; i <= 10; i++)
{
var para = oDoc.Paragraphs.Add();
var paraRange = para.Range;
paraRange.Text = "x^2 + " + i + " = y";
}
最后,您可以向OMaths
对象添加多个范围,并使用BuildUp
一次构建所有范围。
尝试使用此代码并将其与您的解决方案结合使用:
var oWord = new Word.Application();
oWord.Visible = true;
var oDoc = oWord.Documents.Add();
var math = oDoc.OMaths;
for (int i = 1; i <= 10; i++)
{
oDoc.Paragraphs.Add();
var para = oDoc.Paragraphs[i];
var paraRange = para.Range;
paraRange.Text = "x^2 + " + i + " = y";
math.Add(paraRange);
}
math.BuildUp();
oWord.Quit();
/编辑
@JLA发现,更复杂的场景,如\mapsto
或⟼
不起作用。
首次尝试:字宏
幕后的Word设法转换它,所以在开发人员的功能区选项卡中我记录了很多,只是注意到录制启用时它不再工作了......
第二次尝试:将LaTeX转换为MathML
问题Tools for converting LaTeX equations to Content MathML or OpenMath?我SnuggleTeX获得了live demo converting to MathML,但它仅适用于非常简单的场景:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mn>1</mn><mn>2</mn></mfrac></math>
添加到Word的代码:
Clipboard.SetText("<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mrow><mn>1</mn><mn>x</mn></mrow></math>", TextDataFormat.Text);
第三次尝试:自行转换
将所有与OMath
无效的特殊名称更改为UTF-16字符。例如:
\mapsto = \u27FC
Word代码:
paraRange.Text = "x^2 + "+ i + '\u27FC' +" y";
答案 1 :(得分:1)
对于那些在lib上搜索答案的人,因为msdn文档不是那么友好,我就我所理解的内容做了一个快速文档(PDF)(它是法语的,但我相信你可能会设法理解代码部分;))
https://drive.google.com/file/d/0BybogIUkJwCfU2kzU3E4cE1HX2c/view?usp=sharing