【问题标题】:Print one page of WebBrowser打印一页 WebBrowser
【发布时间】:2013-01-23 17:12:43
【问题描述】:

我正在尝试自动打印 Intranet 网站。由于这是一个将放置在特定用户计算机上的应用程序,它将根据需要运行,我希望它尽可能不受干扰(换句话说,不要为每个页面启动 IE )。问题是我需要打印网站的第一页,然后再次打印整个网站,这将产生两次第一页。最好的方法是什么?

让它循环浏览需要打印的页面没有问题,使用 webbrowser 打开页面也没有问题。但是,我确实在指定打印范围时遇到了问题。

我也尝试过 PrintDocument,但不知道如何在表单中打开它。

感谢您提供的任何帮助。

【问题讨论】:

    标签: vb.net winforms automation webbrowser-control printdocument


    【解决方案1】:

    要下载 pdf 文件,请使用 iTextSharp 尝试此解决方案: ITextSharp HTML to PDF?

    如果你想直接保存到文件中,除了一个替换

    private MemoryStream createPDF(string html)
    {
        MemoryStream msOutput = new MemoryStream();
        TextReader reader = new StringReader(html);
    
        // step 1: creation of a document-object
        Document document = new Document(PageSize.A4, 30, 30, 30, 30);            
    
        // step 2:
        // we create a writer that listens to the document
        // and directs a XML-stream to a file
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c:\\my.pdf", FileMode.Create));
    
        // step 3: we create a worker parse the document
        HTMLWorker worker = new HTMLWorker(document);
    
        // step 4: we open document and start the worker on the document
        document.Open();
        worker.StartDocument();
    
        // step 5: parse the html into the document
        worker.Parse(reader);
    
        // step 6: close the document and the worker
        worker.EndDocument();
        worker.Close();
        document.Close();
    
        return msOutput;
    }
    

    PDF 设置完成后,尝试使用 ghostscript 打印一页: Print existing PDF (or other files) in C#

    如果您启动进程的 shell 执行,您可以使用命令行参数:

    gsprint "filename.pdf" -from 1 - to 1
    

    或者,WebBrowser 可以只打印整页:http://msdn.microsoft.com/en-us/library/b0wes9a3.aspx

    我找不到任何引用 WebBrowser 本身可以在没有打印对话框的情况下打印“从页面 X 到 Y”的内容。

    由于我面临类似的问题,这里有一个替代解决方案:

    这个开源项目将 HTML 文档转换为类似于 iTextSharp (http://code.google.com/p/wkhtmltopdf/) 的 PDF 文档。我们最终没有使用 iTextSharp,因为我们想要打印的网站的布局方式存在一些格式问题。我们发送命令行参数以将使用 webclient 下载的 html 转换为 pdf 文件。

    WebClient wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultNetworkCredentials;
    string htmlText = wc.DownloadString("http://websitehere.com);
    

    然后,转成pdf后,就可以简单的打印文件了:

    Process p = new Process();
    p.StartInfo.FileName = string.Format("{0}.pdf", fileLocation);
    p.StartInfo.Verb = "Print";
    p.Start();
    p.WaitForExit();
    

    (对于C#的道歉,我比VB.NET更熟悉它,虽然它应该是一个简单的转换)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-25
      • 2015-09-12
      • 2013-03-14
      • 2010-10-11
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      相关资源
      最近更新 更多