.net winfrom 用 MathML 显示数学公式并导出word与pdf
来源:互联网 发布:淘宝活动广场在哪 编辑:程序博客网 时间:2024/06/06 00:26
一、问题描述
本地应用程序开发,例如.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等有其优势,在本地应用的研发中也可以加以运用。
- .net winfrom 用 MathML 显示数学公式并导出word与pdf
- 导出Word 里面用替换符替换数据 并导出到PDF
- iText导出word、pdf
- 用vb.net 导出word
- MathML .NET Control
- FastReport.NET系列-------导出PDF格式,页面无法显示完全
- iText导出pdf、word、图片
- iText导出pdf、word、图片
- iText导出pdf、word、图片
- iText导出pdf、word、图片
- PHP导出word,CVS,PDF
- iText导出pdf、word、图片
- PHP导出word,CVS,PDF
- 关于asp.net 调用word模板,修改并导出
- 用java导出word并下载文件
- Aspose word导出word/pdf文件
- IE下用JavaScript将HTML导出为Word、Pdf
- IE下用JavaScript将HTML导出为Word、Pdf
- Android Studio编译报错“java.lang.OutOfMemoryError: GC overhead limit exceeded”
- 关于Java基类Object
- MC9S12XEP100的ATD模块(ADC12B16CV1)
- 僵死进程
- 负载均衡算法
- .net winfrom 用 MathML 显示数学公式并导出word与pdf
- 基于CentOS6.4环境编译Spark-2.1.0源码
- 二、Calcite官方文档翻译--进阶
- gunicorn常用命令
- 笨方法学习Python-习题12:提示别人
- codis集群部署实战
- 20171019
- Node开发环境在Ubuntu16.04上部署
- android lichee编译脚本解析