格式化并打印HTML页面 [英] Format and print a HTML page

查看:149
本文介绍了格式化并打印HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:)



我正在做一些HTML工作,我想打印(在纸上)这些HTML文件,实际上,文件确实不存在,一切都保存在一个字符串中,所有文本都是HTML格式,但我想打印,已经格式化了......



例如:



Hi :)

I''m doing some work with HTML and I want to print (on paper) these HTML files, in reality, the file does not exist, everything is saved in a string, all text in HTML, but I would like to print, already formatted...

for example:

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 [ ^ ]



但是,我想知道是否有另一种方法可以做到这一点,更好的方式......? :)



I found that: http://msdn.microsoft.com/en-us/library/w290k23d.aspx[^]

But, I whant to know if have another way to do this, a more better way.. ? :)

推荐答案

您可以使用 WebBrowser 控件并从中进行打印;它可能是 System.Windows.Forms.WebBrowser System.Windows.Controls.WebBrowser 。你知道的。



但是有一个非常不同且有趣的机会:来自CodeProject文章的控制:您将使用的专业HTML渲染器 [ ^ ]。



这是一个质量非常好的控件,允许使用可用的 System.Windows.Forms.RichTextBox 控件以轻量级方式呈现HTML。您可以呈现HTML并打印其内容。



-SA
You can use the WebBrowser control and print from it; it could be System.Windows.Forms.WebBrowser or System.Windows.Controls.WebBrowser. You know that.

But there is one very different and interesting opportunity: the control from this CodeProject article: A Professional HTML Renderer You Will Use[^].

This is a control of a very good quality which allow to render HTML in a lightweight way, using available System.Windows.Forms.RichTextBox control. You can render HTML and print its content.

—SA


试试这个。

您可以使用WebBrowser控件来执行此操作。它允许你在WinForms中显示HTML。



DocumentText proprety允许你设置一个表示你想要显示的HTML的字符串。



例如:

try this.
You can use the WebBrowser control to do so. It will allow you to show HTML inside your WinForms.

The DocumentText proprety will allow you to set a String that represent the HTML you want to show.

For example:
webBrowser.DocumentText = "<html><body><p>I like StackOverflow</p><body></html>";



之后如果要打印页面,则必须等到文档完成并调用WebBrowser的Print方法。 MSDN显示了一种简单的方法:


Afterward if you want to print the page, you''ll have to wait until the Document is completed and call the Print method of the WebBrowser. MSDN shows an easy way to do it:

 private void PrintHelpPage()
{
    // Create a WebBrowser instance. 
    WebBrowser webBrowserForPrinting = new WebBrowser();

    // Add an event handler that prints the document after it loads.
    webBrowserForPrinting.DocumentCompleted +=
        new WebBrowserDocumentCompletedEventHandler(PrintDocument);

    // Set the Url property to load the document.
    webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html");
}

private void PrintDocument(object sender,
    WebBrowserDocumentCompletedEventArgs e)
{
    // Print the document now that it is fully loaded.
    ((WebBrowser)sender).Print();

    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose();
}



您还应该考虑尝试使用PrintDialog方法来确保问题不是您的打印配置。



以下是MSDN的链接:在MSDN上使用WebBrowser控件进行打印 [ ^ ]



可能重复:打印WebBrowser控件内容 [ ^ ]


您可以在页面中的javascsript中调用print方法,但要将其打印为HTML页面,您需要在浏览器中打印它,否则它只是文本。
You can call the print method in javascsript within the page, but to get this to print as a HTML page, you need to print it in the browser, otherwise it''s just text.


这篇关于格式化并打印HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆