您的位置:首页 > 其它

.net winfrom 用 MathML 显示数学公式并导出word与pdf

2017-10-19 16:44 585 查看

一、问题描述

本地应用程序开发,例如.net平台下的winform编程,显示数学公式是一件麻烦的事情,如果要将显示的数学公式导出为word与pdf,将更让人头疼。本文将介绍利用js与html脚本技术,解决上述问题的方法。

二、方法摘要

以.net平台winform开发为例,分为以下几个步骤:

1. 用WebBrowser控件作为数学公式的显示平台,数学公式用html页面来显示;

2. 用MathJax.js作为数学公式解释脚本,支持MathML、LaTex等数学描述语言;

3. 用Aspose.Words(建议用16.x及以上版本)将html页面转换为word(docx版本)文件;

4. 用wkhtmltopdf将html页面转换为pdf文件;

三、过程详述

下载MathJax.js、Aspose.Words.dll和wkhtmltopdf

github上下载MathJax.js极其依赖库:https://github.com/mathjax/MathJax.git

注意:html5支持MathML语法,但是chrome和IE(至少我电脑上装的版本,IE11)目前不支持,而WebBorwser默认选用你电脑上的IE作为解释引擎,所以需要引入MathJax.js库。

创建工程

vs创建工程,添加html与js脚本目录。图中创建了math文件,添加MathJax.js极其依赖库(依赖库没显示全),并创建pages文件夹,pageshow.html用于显示需要展示的数学公式。



绑定WebBorwser控件

在From1的设计面板中,添加WebBorwser控件,并关联pageshow.html:

//C#代码
private string page_path = System.Environment.CurrentDirectory + "/math/pages/pageshow.html";//获取页面路径
this.webBrowser1.Url = new Uri("file:///" + page_path);//在WebBorwser上加载改页面


//html脚本
<!DOCTYPE html>
<html>
<head>
<title>MathJax MathML Test Page</title>
<!-- Copyright (c) 2010-2017 The MathJax Consortium -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1">

<script type="text/javascript" src="../MathJax.js?config=MML_HTMLorMML-full"></script>

</head>
<body>

<p>
When
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mi>a</mi><mo>≠</mo><mn>0</mn>
</math>,
there are two solutions to
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mi>a</mi><msup><mi>x</mi><mn>2</mn></msup>
<mo>+</mo> <mi>b</mi><mi>x</mi>
<mo>+</mo> <mi>c</mi> <mo>=</mo> <mn>0</mn>
</math>
and they are
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mi>x</mi> <mo>=</mo>
<mrow>
<mfrac>
<mrow>
<mo>−</mo>
<mi>b</mi>
<mo>±</mo>
<msqrt>
<msup><mi>b</mi><mn>2</mn></msup>
<mo>−</mo>
<mn>4</mn><mi>a</mi><mi>c</mi>
</msqrt>
</mrow>
<mrow> <mn>2</mn><mi>a</mi> </mrow>
</mfrac>
</mrow>
<mtext>.</mtext>
</math>
</p>

</body>
</html>


显示结果



Aspose.Words将显示结果导出为word docx文件

string doc_path = "output.docx";
Stream stream = this.webBrowser1.DocumentStream;
StreamReader reader = new StreamReader(stream, Encoding.Default);
string responseFromServer = reader.ReadToEnd();//读取数据
reader.Close();

Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.InsertHtml(@responseFromServer);

try
{
doc.Save(doc_path);
}
catch (Exception)
{
MessageBox.Show("导出失败!");
}


wkhtmltopdf将显示结果导出为pdf文件

string param = "file:///" + page_path + " " + pdf_path;//page_path是pageshow.html的绝对路径,pdf_path是pdf的导出路径
try
{
System.Diagnostics.Process p = System.Diagnostics.Process.Start(@"extends\wkhtmltopdf.exe", @param);//在工程目录下创建了extends文件夹,将wkhtmltopdf.exe放入extends文件夹下进行调用
p.WaitForExit();
}
catch (Exception)
{
MessageBox.Show("导出失败!");
}


注意

项目中创建的pages、extends等所有的依赖项,最后运行的时候都要放入debug目录下,及与生成的exe在一个目录下。

四、结束语

html脚本显示复杂图像和文字,如数学公式和svg等有其优势,在本地应用的研发中也可以加以运用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: