使用C#格式化并打印HTML页面

3

我正在处理一些与HTML相关的工作,我想打印(在纸上)这些HTML文件,实际上,文件并不存在,所有内容都保存在一个字符串中,所有文本都是HTML格式的,但我希望打印时已经格式化好了...

例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string HTML =
"<html>" +
"<head>" +
"    <style type=\"text/css\">" +
"    .title {" +
"        color: blue;" +
"        text-decoration: bold;" +
"        text-size: 1em;" +
"    }" +
"    .author {" +
"        color: gray;" +
"    }" +
"    </style>" +
"</head>" +
"<body>" +
"    <p>" +
"    <span class=\"title\">{0}</span>" +
"    <span class=\"author\">{1}</span>" +
"    </p>" +
"</body>" +
"</html>";

            // Just a sample of what I whant to do...
            // PseudoCode
            //Render the HTML code
            RenderHTML aa = new RenderHTML(string.Format(HTML, "Alexandre", "Bencz"));
            aa.PrintDocumentInPaper();
        }
    }
}

我发现: http://msdn.microsoft.com/en-us/library/w290k23d.aspx
但是我想知道是否有其他更好的方法来做到这一点?与此相关的是it技术。

1
请参见此帖子:https://dev59.com/kHE85IYBdhLWcg3w1HGF。建议使用HtmlTidy包装器进行所有格式设置,然后打印出来。 - Marcel N.
我知道这可能只是一些示例代码,但我建议在字符串开头使用@运算符表示它为字符串字面量,这将允许您避免所有的连接。或者,如果您正在连接数据,请确保使用StringBuilder。祝你好运! - Maurice Reeves
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接