将html转换为word文档时未反映字体

时间:2014-09-10 13:30:15

标签: c# html c#-4.0 ms-word

我在c#ASP.NET 4.0中有网站。这里使用HTML打开xml代码和dll。

为了做到这一点,我将采取以下步骤: -

1)首先打开现有的单词doc复制一些带有字体“Arial”的段落。  2)然后使用htmltoopen xml dll将html解析为我的html到word转换代码。

我的c#代码在这里: -

protected void btnExportToWord_Click(object sender, EventArgs e)
    {
        string readText = string.Empty;
        const string filename = "C:\\Temp\\ExportToDocNew1.docx";

        try
        {               
            HTMLFile = "<html><head><style type=\"text/css\" media=\"screen,print\">";
            HTMLFile += " .divPageBreak { page-break-after: always; display: block;  clear: both;}";
            HTMLFile += " table,td{border:1px solid black;border-collapse:collapse;}";
            HTMLFile += "</style></head><body><div class=\"divPageBreak\">" + "<p>&nbsp;</p>
自报告职责之日起,您将有资格获得随附附件中规定的工资,津贴和其他福利。附件中所示的报酬是全面的,包罗万象的,因此应被视为包括公司的所有责任。您和公司之间的薪酬待遇严格保密,不得与任何人讨论,也不得以任何方式泄露给任何人。

“+”“;

            using (MemoryStream generatedDocument = new MemoryStream())
            {
                using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
                {
                    MainDocumentPart mainPart = package.MainDocumentPart;

                    if (mainPart == null)
                    {
                        mainPart = package.AddMainDocumentPart();
                        new DocumentFormat.OpenXml.Wordprocessing.Document(new Body()).Save(mainPart);
                    }

                    HtmlConverter converter = new HtmlConverter(mainPart);
                    Body body = mainPart.Document.Body;

                    SectionProperties sectionProps = new SectionProperties();
                    PageMargin pageMargin = new PageMargin() { Top = 1000, Right = (UInt32Value)1008U, Bottom = 2000, Left = (UInt32Value)1008U, Header = (UInt32Value)1600U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U };
                    sectionProps.Append(pageMargin);
                    body.Append(sectionProps);

                    var paragraphs = converter.Parse(HTMLFile);
                    word.Application wordApp = new word.Application();
                    for (int i = 0; i < paragraphs.Count; i++)
                    {
                        body.Append(paragraphs[i]);
                    }

                    converter.ExcludeLinkAnchor = false;                     

                    mainPart.Document.Save();
                }

                File.WriteAllBytes(filename, generatedDocument.ToArray());
            }
           }
        catch
        {
        }
    }

1 个答案:

答案 0 :(得分:0)

您还没有明确提出我可以看到的问题,因此我会认为您对以C#/ .NET在html到docx之间的字体感兴趣。

我不知道&#34; htmltoopen xml dll&#34;,但作为替代方案,docx4j-ImportXHTML.NET肯定支持字体:

 /**
 * Map a font family, for example "Century Gothic" in:
 * 
 *    font-family:"Century Gothic", Helvetica, Arial, sans-serif;
 * 
 * to a w:rFonts object, for example:
 * 
 *    <w:rFonts w:ascii="Arial Black" w:hAnsi="Arial Black"/>
 * 
 * Assuming style font-family:"Century Gothic", Helvetica, Arial, sans-serif;
 * the first font family for which there is a mapping is the one
 * which will be used. 
 * 
 * xhtml-renderer's CSSName defaults font-family: serif
 * 
 * It is your responsibility to ensure a suitable font is available 
 * on the target system (or embedded in the docx package).  If we 
 * (eventually) support CSS @font-face, docx4j could do that
 * for you (at least for font formats we can convert to something
 * embeddable).
 * 
 * You should set these up once, for all your subsequent 
 * imports, since some stuff is cached and currently won't get updated
 * if you add fonts later.
 * 
 * @since 3.0
 */
public static void addFontMapping(String cssFontFamily, RFonts rFonts) {
    FontHandler.addFontMapping(cssFontFamily, rFonts);
}

public static void addFontMapping(String cssFontFamily, String font) {

    FontHandler.addFontMapping(cssFontFamily, font);
}

您还可以配置默认值:

# docx4j-ImportXHTML.properties

docx4j-ImportXHTML.fonts.default.serif=Times New Roman

docx4j-ImportXHTML.fonts.default.sans-serif=Arial

docx4j-ImportXHTML.fonts.default.monospace=Courier New