public static WmlDocument ConvertHtmlToWml(
     string defaultCss,
     string authorCss,
     string userCss,
     XElement xhtml,
     HtmlToWmlConverterSettings settings)
 {
     return(HtmlToWmlConverterCore.ConvertHtmlToWml(defaultCss, authorCss, userCss, xhtml, settings, null, null));
 }
 public static WmlDocument ConvertHtmlToWml(
     string defaultCss,
     string authorCss,
     string userCss,
     XElement xhtml,
     HtmlToWmlConverterSettings settings,
     WmlDocument emptyDocument,
     string annotatedHtmlDumpFileName)
 {
     return(HtmlToWmlConverterCore.ConvertHtmlToWml(defaultCss, authorCss, userCss, xhtml, settings, emptyDocument, annotatedHtmlDumpFileName));
 }
        public static HtmlToWmlConverterSettings GetDefaultSettings(WmlDocument wmlDocument)
        {
            HtmlToWmlConverterSettings settings = new HtmlToWmlConverterSettings();

            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(wmlDocument.DocumentByteArray, 0, wmlDocument.DocumentByteArray.Length);
                using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, false))
                {
                    string majorLatinFont, minorLatinFont;
                    double defaultFontSize;
                    GetDefaultFontInfo(wDoc, out majorLatinFont, out minorLatinFont, out defaultFontSize);
                    settings.MajorLatinFont  = majorLatinFont;
                    settings.MinorLatinFont  = minorLatinFont;
                    settings.DefaultFontSize = defaultFontSize;

                    settings.MinorLatinFont            = "Times New Roman";
                    settings.DefaultFontSize           = 12d;
                    settings.DefaultBlockContentMargin = "auto";
                    settings.DefaultSpacingElement     = new XElement(W.spacing,
                                                                      new XAttribute(W.before, 100),
                                                                      new XAttribute(W.beforeAutospacing, 1),
                                                                      new XAttribute(W.after, 100),
                                                                      new XAttribute(W.afterAutospacing, 1),
                                                                      new XAttribute(W.line, 240),
                                                                      new XAttribute(W.lineRule, "auto"));
                    settings.DefaultSpacingElementForParagraphsInTables = new XElement(W.spacing,
                                                                                       new XAttribute(W.before, 100),
                                                                                       new XAttribute(W.beforeAutospacing, 1),
                                                                                       new XAttribute(W.after, 100),
                                                                                       new XAttribute(W.afterAutospacing, 1),
                                                                                       new XAttribute(W.line, 240),
                                                                                       new XAttribute(W.lineRule, "auto"));

                    XDocument mXDoc          = wDoc.MainDocumentPart.GetXDocument();
                    XElement  existingSectPr = mXDoc.Root.Descendants(W.sectPr).FirstOrDefault();
                    settings.SectPr = new XElement(W.sectPr,
                                                   existingSectPr.Elements(W.pgSz),
                                                   existingSectPr.Elements(W.pgMar));
                }
            }
            return(settings);
        }
Example #4
0
        private static void ConvertToDocx(string file, string destinationDir)
        {
            var sourceHtmlFi  = new FileInfo(file);
            var sourceImageDi = new DirectoryInfo(destinationDir);

            var destDocxFi = new FileInfo(Path.Combine(destinationDir, sourceHtmlFi.Name.Replace(".html", "-ConvertedByHtmlToWml.docx")));

            XElement html = HtmlToWmlReadAsXElement.ReadAsXElement(sourceHtmlFi);

            string usedAuthorCss = HtmlToWmlConverter.CleanUpCss((string)html.Descendants().FirstOrDefault(d => d.Name.LocalName.ToLower() == "style"));

            HtmlToWmlConverterSettings settings = HtmlToWmlConverter.GetDefaultSettings();

            // image references in HTML files contain the path to the subdir that contains the images, so base URI is the name of the directory
            // that contains the HTML files
            settings.BaseUriForImages = sourceHtmlFi.DirectoryName;

            WmlDocument doc = HtmlToWmlConverter.ConvertHtmlToWml(defaultCss, usedAuthorCss, userCss, html, settings);

            doc.SaveAs(destDocxFi.FullName);
        }