1

我正在嘗試自動化Intranet網站的打印。由於這是一個將放在特定用戶計算機上的應用程序,它將根據需要運行,我希望它儘可能不中斷(換句話說,不要爲每個頁面啓動IE )。問題是,我需要打印網站的第一頁,然後再打印整個網站,這將產生兩次第一頁。做這個的最好方式是什麼?打印WebBrowser的一頁

我沒有問題讓它遍歷它需要打印的頁面,也沒有問題用webbrowser打開頁面。但是,我確實遇到了指定打印範圍的問題。

我也嘗試了PrintDocument,但無法弄清楚如何在窗體中打開它。

感謝您提供任何幫助。

回答

0

要下載的pdf文件,使用iTextSharp的嘗試此解決方案:如果您想直接保存到一個文件

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設置 ITextSharp HTML to PDF?

除非一個替換,嘗試ghostscript打印一頁: Print existing PDF (or other files) in C#

如果你啓動一個shell執行該進程,你可以使用命令行參數:

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

另外,web瀏覽器可以直接打印完整的網頁:http://msdn.microsoft.com/en-us/library/b0wes9a3.aspx

我找不到任何引用該web瀏覽器本身就可以打印出「從第一頁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再熟悉不過了,但它應該是一個簡單轉換)