如何将 css 应用到 webbrowser.documentText 并使用应用的 css 打印 html?

How to Apply css to webbrowser.documentText and print the html with the applied css?

我有一个 asp.net mvc web 应用程序,它有一个控制器,它有一个 returns html 到 windows 表单客户端的操作。

打印返回的 html。 这非常有效,html 如下所示:

请注意 html 总是不同的。

我在 bootstrap 和其他一些自定义 css(包括内联样式)的帮助下设计了 html。

如上所示,有样式表被 link 编辑到 html 中。 我还没弄清楚如何将这个 css 应用到 webBrowser.DocumentText。

在 google 的帮助下,我发现我必须找到该文件,然后 link 从那里找到它,这是我的尝试:

    private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var document = ((WebBrowser) sender);
        string content = document.DocumentText;

        char separator = Path.DirectorySeparatorChar;
        string startupPath = AppDomain.CurrentDomain.BaseDirectory;
        string[] pathItems = startupPath.Split(separator);
        string projectPath = string.Join(separator.ToString(),
            pathItems.Take(pathItems.Length - 4));

       string file = Path.Combine(projectPath, "\IautmationWeb\Content\bootstrapSmall.css");

   content = content.Replace("<link href='/Content/bootstrapSmall.css' rel=stylesheet'/>", "<link href='" + projectPath + file + "'rel='stylesheet'/>");

       document.DocumentText = content;

        // add css ... how?
        // print...implemented

        for (int i = 0; i < copies; i++)
        {
            document.Print();
        }

        ((WebBrowser)sender).Dispose();
    }

这行不通。我做错了什么?

编辑:

在@Peter B 提供的一些信息后,我尝试了:

     content = content.Replace("href='/Content/bootstrapSmall.css'", "href='" + file + "'");

     document.DocumentText = content;

但是替换方法仍然没有完成我想要它做的事情:

编辑:

感谢@Peter B

我设法导航到文件。

        string file = Path.Combine(projectPath, "\IautmationWeb\Content\bootstrapSmall.css");
        string fileTwo = Path.Combine(projectPath, "\IautmationWeb\Content\Automation.css");
        string fileThree = Path.Combine(projectPath, "\IautmationWeb\Content\octicons.css");

        content = content.Replace("href=\"/Content/bootstrapSmall.css\"", "href='" + projectPath +  file + "'");

        content = content.Replace("href=\"/Content/Automation.css\"", "href='" + projectPath + fileTwo + "'");

        content = content.Replace("href=\"/Content/octicons.css\"", "href='" + projectPath + fileThree + "'");

        document.DocumentText = content;

html 现在的样子:

你好像把引号弄错了,'" 不一样。

试试这个:

content = content.Replace("href=\"/Content/bootstrapSmall.css\"", "href='" + file + "'");

对于第二个参数,使用哪个引号并不重要,但对于第一个参数却很重要。