none
PrintDocument.Print (Print a website)

    Question

  • Hi I am using the following code to print a webpage:

    PrintDocument pd = new PrintDocument();
    pd.Print();
    

    But as a result, I only get a blank page.

    I want to print an entire webpage (ex: http://www.google.com).

    Can someone help?

    Wednesday, May 18, 2011 8:44 AM

Answers

All replies

  • Use the namespaces first...  
    using System.Net;
    using System.IO;
    using System.Drawing.Printing;

     

     

    Declare

      private Font printFont;
      private StreamReader reader;
      string url = "http://www.google.com";
      WebResponse response = null;

     

     

    Create an event...

      private void pd_PrintPage(object sender, PrintPageEventArgs ev)
      {
       float linesPerPage = 0;
       float yPos = 0;
       int count = 0;
       float leftMargin = ev.MarginBounds.Left;
       float topMargin = ev.MarginBounds.Top;
       string line = null;
    
       // Calculate the number of lines per page.
       linesPerPage = ev.MarginBounds.Height /
        printFont.GetHeight(ev.Graphics);
    
       // Print each line of the file.
       while (count < linesPerPage &&
        ((line = reader.ReadLine()) != null))
       {
        yPos = topMargin + (count *
         printFont.GetHeight(ev.Graphics));
        ev.Graphics.DrawString(line, printFont, Brushes.Black,
         leftMargin, yPos, new StringFormat());
        count++;
       }
    
       // If more lines exist, print another page.
       if (line != null)
        ev.HasMorePages = true;
       else
        ev.HasMorePages = false;
      }

     

     

    On the Print button click call the event...

    private void printButton_Click(object sender, EventArgs e) 
     {
      try 
      {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        response = request.GetResponse();
        reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    
       try 
       {
        printFont = new Font("Arial", 10);
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler
         (this.pd_PrintPage);
        pd.Print();
       } 
       finally 
       {
        reader.Close();
       }
      } 
      catch(Exception ex) 
      {
       MessageBox.Show(ex.Message);
      }
     }
    

     

     

    Here is the Microsoft Example.. http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument(v=VS.90).aspx

    Thanks

     


    Anirban Bhattacharya (Lead Consultant - ESSPL)
    Wednesday, May 18, 2011 9:01 AM
  • Thank you for your help.

    But the code above prints the source of the website, and not the website (preview) itself.

    I need to print the look of the site, how it is shown in the browser.

     

     

    Wednesday, May 18, 2011 9:18 AM
  • Here is the ready solution for you... http://www.codeproject.com/KB/printing/printhml.aspx

    Thanks


    Anirban Bhattacharya (Lead Consultant - ESSPL)
    Wednesday, May 18, 2011 9:51 AM
  • Thank you, but this solution is not working for me.
    Wednesday, May 18, 2011 11:32 AM
  • Hi Rusul Ibrahimi,

    Welcome!

    From your description, it looks you want to print a web page. Based on my understanding, we can use WebBrowswer.ShowPrintDialog method to print web page directly.

    In addition, if you want to use PrintDocument class to achieve your project, please check if you pass a PrintDocument.DocumentName property in your project.

    Hope this helps.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 19, 2011 4:01 AM
  • Any update? Would you mind letting us know how it goes? Was your question solved?

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 02, 2011 11:07 AM