2010-06-08 837 views
2

總的來說,我試圖寫出一個網頁PDF。有一個Web服務,我可以用它來將文件轉換爲pdf。所以我想要做的是從WebBrowser winforms控件中保存一個網頁。如何使用c#中的內置webbrowser保存完整的網頁

我已經嘗試寫出它的文檔流,但只是給了我的網頁的HTML,而不是與它一起使用的圖像。

另一種我研究但未取得成功的方法是嘗試創建WebBrowser文檔的圖像。我在網上找到了一些利用DrawToBitmap函數的例子,但是它們都沒有爲我工作。

任何援助將不勝感激。

+0

可能的重複:http://stackoverflow.com/questions/564650/convert-html-to-pdf-in-net http://stackoverflow.com/questions/570179/generate-pdf-from-asp-net-from -raw-html-css-content http:// stackov erflow.com/questions/598980/generation-pdf-from-html-component-for-net http://stackoverflow.com/questions/973861/convert-html-document-into-pdf-using-c http: //stackoverflow.com/questions/589852/export-from-html-to-pdf-c http://stackoverflow.com/questions/936508/how-to-render-html-chunk – 2010-06-08 20:30:11

回答

0

要創建PDF,您使用的程序將需要該網站的源代碼。無論你使用WebBrowser winforms控件還是其他東西來獲取該信息,都沒有實際的區別。

此代碼將得到任何網站的源代碼,對你來說,假設你不需要先上傳東西:

string url = "some site"; 
string source = string.Empty; 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
using(StreamReader sr = new StreamReader(response.GetResponseStream()){ 
    source = sr.ReadToEnd(); 
} 
0

,直到你有使用Graphics.CopyFromScreen功能整個頁面你可以採取截圖。

// Get screen location of web browser 
Rectangle rec = webBrowser1.RectangleToScreen(webBrowser1.ClientRectangle); 
// create image to hold whats in view 
Bitmap image = new Bitmap(rec.Width, rec.Height); 
// get graphics to draw on image 
Graphics g = Graphics.FromImage(image); 
// Save into image 
// From MSDN: 
//public void CopyFromScreen(
// int sourceX, 
// int sourceY, 
// int destinationX, 
// int destinationY, 
// Size blockRegionSize 
//) 
g.CopyFromScreen(rec.X,rec.Y,0,0,rec.Size) 

你也可以因此他們不是在你的圖像以去除滾動條:

webBrowser.ScrollBarsEnabled = false; 
webBrowser.Document.Body.Style = "overflow:hidden;"; 

,然後向下滾動採取下一個頁面的截圖:

webBrowser.Document.Window.ScrollTo(x,y); 
相關問題