public CssStyle GetStyleFromRunProperties(RunProperties r, StyleConfig config = null) { CssStyle style = new CssStyle(null); var rPr = r; if (rPr != null) { if (rPr.RunFonts != null) { if (config != null) { if (config.Charset == "Ascii") { if (rPr.RunFonts.Ascii != null || rPr.RunFonts.HighAnsi != null) { var fs = (rPr.RunFonts.Ascii != null ? rPr.RunFonts.Ascii.Value : "nofont") + "," + (rPr.RunFonts.HighAnsi != null ? rPr.RunFonts.HighAnsi.Value : "nofont"); style.AddStyle("font-family", fs); } } if (config.Charset == "EastAsia") { if (rPr.RunFonts.EastAsia != null) { var fs = (rPr.RunFonts.EastAsia != null ? rPr.RunFonts.EastAsia.Value : "nofont"); style.AddStyle("font-family", fs); } } } else { var fs = (rPr.RunFonts.Ascii != null ? rPr.RunFonts.Ascii.Value : "nofont") + "," + (rPr.RunFonts.HighAnsi != null ? rPr.RunFonts.HighAnsi.Value : "nofont") + "," + (rPr.RunFonts.EastAsia != null ? rPr.RunFonts.EastAsia.Value : "nofont"); style.AddStyle("font-family", fs); } } if (rPr.FontSize != null && rPr.FontSize.Val != null) { style.AddStyle("font-size", rPr.FontSize.Val + "px"); } if (rPr.Bold != null) { if (rPr.Bold.Val != null && rPr.Bold.Val.Value) { style.AddStyle("font-weight", "bold"); } } if (rPr.Color != null) { style.AddStyle("color", "#" + rPr.Color.Val); } } return(style); }
public CssStyle GetStyleFromParagraphProperties(ParagraphProperties p, StyleConfig config = null) { CssStyle style = new CssStyle(null); var pPr = p; if (pPr != null) { if (pPr.Justification != null) { var jc = pPr.Justification; style.AddStyle("text-align", jc.Val); } if (pPr.ParagraphMarkRunProperties != null) { RunProperties rPr = new RunProperties(pPr.ParagraphMarkRunProperties.OuterXml); CssStyle p_s = this.GetStyleFromRunProperties(rPr, config); style.CombineStyles(p_s); } } return(style); }
static void Main(string[] args) { string file = "./data/word.docx"; file = "./data/DirectX_9_3D.docx"; const string ROOT = "./OUT/"; string fileMd5 = Utities.GetMd5(Path.GetFileName(file)); string docRoot = Path.Combine(new[] { ROOT, fileMd5 + "/" }); ConvertConfig config = new ConvertConfig { ResourcePath = "./" + fileMd5 + "/" }; if (!Directory.Exists(docRoot)) { Console.WriteLine("Unzip File"); UnZip un = new UnZip(); un.UnZipToDir(file, docRoot); } Console.WriteLine("Convert Word to Html"); OpenXmlHelper helper = new OpenXmlHelper(); Html.Html html = new Html.Html(); HtmlElement meta0 = new HtmlElement("meta", false); meta0.AddAttribute("http-equiv", "X-UA-Compatible"); meta0.AddAttribute("content", "IE=edge,chrome=1"); HtmlElement meta1 = new HtmlElement("meta", false); meta1.AddAttribute("name", "viewport"); meta1.AddAttribute("content", "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"); HtmlElement meta2 = new HtmlElement("meta", false); meta2.AddAttribute("name", "apple-mobile-web-app-capable"); meta2.AddAttribute("content", "yes"); HtmlElement meta3 = new HtmlElement("meta", false); meta3.AddAttribute("http-equiv", "content-type"); meta3.AddAttribute("content", "text/html; charset=UTF-8"); html.AddHeadElement(meta0); html.AddHeadElement(meta1); html.AddHeadElement(meta2); html.AddHeadElement(meta3); CSS css = new CSS(); CssStyle body = new CssStyle("body"); body.AddStyle("background-color", "gray"); CssStyle center = new CssStyle(".center"); center.AddStyle("text-align", "center"); css.AddStyle(body); //css.AddStyle(center); html.AddStyle(css); HtmlElement div = new HtmlElement("div"); HtmlAttribute divClass = new HtmlAttribute("class", "documentbody"); div.AddAttribute(divClass); CssStyle divStyle = new CssStyle(); divStyle.AddStyle("font-family", "'Times New Roman' 宋体"); divStyle.AddStyle("font-size", "10.5pt"); divStyle.AddStyle("margin", "0 auto"); divStyle.AddStyle("width", "600px"); divStyle.AddStyle("padding", "100px 120px"); divStyle.AddStyle("border", "2px solid gray"); divStyle.AddStyle("background-color", "white"); div.AddStyle(divStyle); #region docuemnt WordprocessingDocument doc = WordprocessingDocument.Open(file, false); //StyleParts StylesPart docstyles = doc.MainDocumentPart.StyleDefinitionsPart == null ? doc.MainDocumentPart.StylesWithEffectsPart : (StylesPart)doc.MainDocumentPart.StyleDefinitionsPart; var styles = docstyles.Styles; //styles var styleEl = styles.Elements <Style>(); //var i = __styles.Count(); //生产Style模版对应的CSS Style html.AddStyle(helper.GetStyles(styleEl)); var pps = doc.MainDocumentPart.Document.Body.ChildElements; ElementHandler handler = new ElementHandler(doc); //处理各个Word元素 foreach (var pp in pps) { //Console.WriteLine(pp.GetType().ToString()); div.AddChild(handler.Handle(pp, config)); } #endregion html.AddElement(div); string htmlfile = ROOT + fileMd5 + ".html"; FileStream fs = File.Exists(htmlfile) ? new FileStream(htmlfile, FileMode.Truncate, FileAccess.Write) : new FileStream(htmlfile, FileMode.CreateNew, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(html.ToString()); sw.Close(); fs.Close(); //// //XDocument _styles = null; //if (docstyles != null) //{ // using (var reader = XmlReader.Create(docstyles.GetStream(FileMode.Open, FileAccess.Read))) // { // _styles = XDocument.Load(reader); // } //} //if (_styles != null) //{ // //Console.WriteLine(_styles.ToString()); //} }