static void Main(string[] args) { const string filename = "test.docx"; string html = Demo.Properties.Resources.CompleteRunTest; if (File.Exists(filename)) File.Delete(filename); using (MemoryStream generatedDocument = new MemoryStream()) { // Uncomment and comment the second using() to open an existing template document // instead of creating it from scratch. byte[] data = Demo.Properties.Resources.template; generatedDocument.Write(data, 0, data.Length); generatedDocument.Position = 0L; using (WordprocessingDocument package = WordprocessingDocument.Open(generatedDocument, true)) //using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = package.MainDocumentPart; if (mainPart == null) { mainPart = package.AddMainDocumentPart(); new Document(new Body()).Save(mainPart); } HtmlConverter converter = new HtmlConverter(mainPart); //converter.WebProxy.Credentials = new System.Net.NetworkCredential("nizeto", "****", "domain"); //converter.WebProxy.Proxy = new System.Net.WebProxy("proxy01:8080"); converter.ImageProcessing = ImageProcessing.ManualProvisioning; converter.ProvisionImage += OnProvisionImage; Body body = mainPart.Document.Body; converter.ParseHtml(html); mainPart.Document.Save(); //AssertThatOpenXmlDocumentIsValid(package); } File.WriteAllBytes(filename, generatedDocument.ToArray()); } System.Diagnostics.Process.Start(filename); }
static void Main(string[] args) { CssParser cp = new CssParser(); cp.AddStyleSheet(@"1_files/quemain.css"); var html = File.ReadAllText("2.html"); var htmlText = CssHtmlMerger.MergeHtmlAndCss(cp, html); var bodyhtml = htmlText; //string body = File.ReadAllText("E:\\N1.txt"); //mathjax2word.OpenXmlWord word = new mathjax2word.OpenXmlWord("E:\\a4.docx"); //word.WriteTextToWord(bodyhtml); //word.Close(); //Console.ReadLine(); #region html 2 openxml const string filename = "test.docx"; if (File.Exists(filename)) File.Delete(filename); using (MemoryStream generatedDocument = new MemoryStream()) { // Uncomment and comment the second using() to open an existing template document // instead of creating it from scratch. byte[] data = Resource1.template; generatedDocument.Write(data, 0, data.Length); generatedDocument.Position = 0L; using (WordprocessingDocument package = WordprocessingDocument.Open(generatedDocument, true)) //using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = package.MainDocumentPart; if (mainPart == null) { mainPart = package.AddMainDocumentPart(); new Document(new Body()).Save(mainPart); } HtmlConverter converter = new HtmlConverter(mainPart); //converter.WebProxy.Credentials = new System.Net.NetworkCredential("nizeto", "****", "domain"); //converter.WebProxy.Proxy = new System.Net.WebProxy("proxy01:8080"); converter.ImageProcessing = ImageProcessing.AutomaticDownload; converter.ProvisionImage += OnProvisionImage; Body body = mainPart.Document.Body; converter.ParseHtml(bodyhtml); mainPart.Document.Save(); //AssertThatOpenXmlDocumentIsValid(package); } File.WriteAllBytes(filename, generatedDocument.ToArray()); } System.Diagnostics.Process.Start(filename); #endregion #region 1 // string br = "<w:p><w:r><w:t></w:t></w:r></w:p>"; // string oxmlText = "<w:p><w:r><w:t>{0}</w:t></w:r></w:p>"; // var brArray = body.Split(new string[] { "<br>" }, body.Length, StringSplitOptions.None); // var strList = new List<string>(); // for (int k = 0; k < brArray.Length; k++) // { // strList.Add(HandleStr(brArray[k])); // strList.Add(br); // } // var xml = string.Join("", strList); // Console.WriteLine(xml); // OpenXmlWord word = new OpenXmlWord("E:\\a.docx"); //// var omml = OpenXmlWord.ConvertMathMl2OMML(xml); // word.WriteTextToWord(xml); // word.Close(); // Console.ReadLine(); #endregion #region 2 //var lmm = new LatexToMathMLConverter( // "E:\\source.txt", // Encoding.UTF8, // "E:\\source.xml"); ////lmm.ValidateResult = true; ////lmm.Convert(); //var str = lmm.ConvertToText(); //Console.WriteLine(str); //string strText = System.Text.RegularExpressions.Regex.Replace(body, "<[^>]+>", ""); //strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", ""); ////Regex.Match(body,) //body.Replace(@"\(", "$"); //OpenXmlWord word = new OpenXmlWord("E:\\a.docx"); //var omml = OpenXmlWord.ConvertMathMl2OMML(str); //word.WritOfficeMathMLToWord(omml); //word.Close(); //Console.ReadLine(); #endregion }