using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using OpenXmlPowerTools;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Xml.Xsl;
using TEAMModelOS.SDK.Helper.Common.StringHelper;
using TEAMModelOS.SDK.Helper.Network.HttpHelper;
namespace TEAMModelOS.Test.OfficeDoc
{
class Program
{
///
/// 对数组进行组合操作,选取selectCount个元素进行组合
///
/// 即将进行组合操作的数组
/// 选取的元素的个数
static void C(List lsArray, int selectCount)
{
int totolcount = lsArray.Count;
int[] currectselect = new int[selectCount];
int last = selectCount - 1;
for (int i = 0; i < selectCount; i++)
{
currectselect[i] = i;
}
while (true)
{
for (int i = 0; i < selectCount; i++)
{
Console.Write(" {0} ", lsArray[currectselect[i]]);
}
Console.WriteLine();
if (currectselect[last] < totolcount - 1)
{
currectselect[last]++;
}
else
{
int pos = last;
while (pos > 0 && currectselect[pos - 1] == currectselect[pos] - 1)
{
pos--;
}
if (pos == 0) return;
currectselect[pos - 1]++;
for (int i = pos; i < selectCount; i++)
{
currectselect[i] = currectselect[i - 1] + 1;
}
}
}
}
///
/// 对数组进行全排列
///
/// 要进行全排列的数组
/// 进行全排列的开始下标
/// 进行全排列的结束下标
static void A(List lsArray, int begin, int end)
{
if (begin == end)
{
for (int i = 0; i <= end; i++)
Console.Write(" {0} ", lsArray[i]);
Console.WriteLine();
}
for (int i = begin; i <= end; i++)
{
Swap(lsArray, begin, i);
A(lsArray, begin + 1, end);
Swap(lsArray, begin, i);
}
}
///
/// 交换数组中的下标为x,y的值
///
/// 该数组
///
///
static void Swap(List lsArray, int x, int y)
{
string t = lsArray[x];
lsArray[x] = lsArray[y];
lsArray[y] = t;
}
static void Main(string[] args)
{
List list = new List();
for (int i = 0; i < 6; i++)
{
list.Add(i.ToString());
}
//C(list, 4);
Console.WriteLine("---------------------");
A(list, 0, 4);
// wordPathStr = @"E:\document\题目模板.docx";
//string s = HttpHelper.HttpGet("https://teammodelstorage.blob.core.chinacloudapi.cn/teammodelos/common/20190620/index_143520-2550.html");
//for (int i = 0; i < 1000; i++) {
// Console.WriteLine(DateTime.Now.ToString("yyyyMMddHHmmssfffffff"));
//}
//ConvertDocxToHtml(wordPathStr);
//byte[] byteArray = File.ReadAllBytes(wordPathStr);
//using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
//{
// Document document = new Document();
// document.Load(doc.MainDocumentPart);
// var stream = new MemoryStream();
// stream.Write(byteArray, 0, (int)byteArray.Length);
//doc.SaveAs(@"E:\document\1026-Weekend Acitivities-刘海荣.xml");
// File.WriteAllBytes(@"E:\document\1026-Weekend Acitivities-刘海荣.xml", stream.ToArray());
//}
// string a = GetWordDocumentAsMathML(wordPathStr);
}
public static string ConvertXML(XmlDocument InputXMLDocument, string XSLTFilePath, XsltArgumentList XSLTArgs)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
XslCompiledTransform xslTrans = new XslCompiledTransform();
xslTrans.Load(XSLTFilePath);
xslTrans.Transform(InputXMLDocument.CreateNavigator(), XSLTArgs, sw);
return sw.ToString();
}
public static string GetWordDocumentAsMathML(string fileName) {
string officeML = string.Empty;
using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, false))
{
string wordDocXml = doc.MainDocumentPart.Document.OuterXml;
XslCompiledTransform xslTransform = new XslCompiledTransform();
// The OMML2MML.xsl file is located under
// %ProgramFiles%\Microsoft Office\Office15\
xslTransform.Load(@"E:\document\OMML2MML.XSL");
using (TextReader tr = new StringReader(wordDocXml))
{
// Load the xml of your main document part.
using (XmlReader reader = XmlReader.Create(tr))
{
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings settings = xslTransform.OutputSettings.Clone();
// Configure xml writer to omit xml declaration.
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.OmitXmlDeclaration = true;
XmlWriter xw = XmlWriter.Create(ms, settings);
// Transform our OfficeMathML to MathML.
xslTransform.Transform(reader, xw);
ms.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
{
officeML = sr.ReadToEnd();
// Console.Out.WriteLine(officeML);
}
}
}
}
}
return officeML;
}
public static void ConvertDocxToHtml(string fileName)
{
byte[] byteArray = File.ReadAllBytes(fileName);
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
{
//XslCompiledTransform xslTransform = new XslCompiledTransform();
//xslTransform.Load(@"E:\document\OMML2MML.XSL");
//XmlWriterSettings xmlsettings = xslTransform.OutputSettings.Clone();
//xmlsettings.ConformanceLevel = ConformanceLevel.Fragment;
//xmlsettings.OmitXmlDeclaration = true;
//XmlWriter xw = XmlWriter.Create(memoryStream, xmlsettings);
// XmlDocument xdoc = doc.ToFlatOpcDocument().GetXmlDocument();
// xdoc.Save(@"E:\document\" + Guid.NewGuid().ToString() + ".xml");
// string path = @"E:\document\" + Guid.NewGuid().ToString() + ".xml";
// xdoc.Save(path);
// xdoc.LoadXml(path);
// string returnhtml = ConvertXML(xdoc, @"E:\document\1111" + ".xml", new XsltArgumentList());
int imageCounter = 0;
WmlToHtmlConverterSettings settings = new WmlToHtmlConverterSettings()
{
PageTitle = "My Page Title",
AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }",
TableInnerCss = true,
FabricateCssClasses = true,
CssClassPrefix = "pt-",
RestrictToSupportedLanguages = false,
RestrictToSupportedNumberingFormats = false,
ImageHandler = imageInfo =>
{
++imageCounter;
string extension = imageInfo.ContentType.Split('/')[1].ToLower();
ImageFormat imageFormat = null;
if (extension == "png") imageFormat = ImageFormat.Png;
else if (extension == "gif") imageFormat = ImageFormat.Gif;
else if (extension == "bmp") imageFormat = ImageFormat.Bmp;
else if (extension == "jpeg") imageFormat = ImageFormat.Jpeg;
else if (extension == "tiff")
{
extension = "gif";
imageFormat = ImageFormat.Gif;
}
else if (extension == "x-wmf")
{
extension = "wmf";
imageFormat = ImageFormat.Wmf;
}
if (imageFormat == null) return null;
string base64 = null;
try
{
using (MemoryStream ms = new MemoryStream())
{
imageInfo.Bitmap.Save(ms, imageFormat);
var ba = ms.ToArray();
base64 = System.Convert.ToBase64String(ba);
}
}
catch (System.Runtime.InteropServices.ExternalException)
{ return null; }
ImageFormat format = imageInfo.Bitmap.RawFormat;
ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders()
.First(c => c.FormatID == format.Guid);
string mimeType = codec.MimeType;
string imageSource =
string.Format("data:{0};base64,{1}", mimeType, base64);
XElement img = new XElement(Xhtml.img,
new XAttribute(NoNamespace.src, imageSource),
imageInfo.ImgStyleAttribute,
imageInfo.AltText != null ?
new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
return img;
}
};
// XElement html = HtmlConverter.ConvertToHtml(doc, settings);
// File.WriteAllText(@"E:\document\kk.html", html.ToStringNewLineOnAttributes());
XElement htmlElement = WmlToHtmlConverter.ConvertToHtml(doc, settings);
var htmls = new XDocument(new XDocumentType("html", null, null, null), htmlElement);
var htmlString = htmls.ToString(SaveOptions.DisableFormatting);
//引入MathJax插件
htmlString = htmlString + "";
File.WriteAllText(@"E:\document\kk.html", htmlString);
};
}
}
}
}