Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi,

I have a form that that grows in height as user controls are added to form depending on data retrieved and presented. The problem is that the form always grows past the screen height and when trying to print the form I'm only able to capture what's being displayed on the screen. Is there are way to capture the entire form and print it?

I've been using the following code:

Print Preview ...
private void buttonPrintPreview_Click(object sender, EventArgs e)
{
    PrintPreviewDialog dlg = new PrintPreviewDialog();
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(PrintImage);
    dlg.Document = pd;
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        pd.Print();
    }
}


Print ...
private void buttonPrint_Click(object sender, EventArgs e)
{
    PrintDialog dlg = new PrintDialog();
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(PrintImage);
    dlg.Document = pd;
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        pd.Print();
    }
}


Print Image ...
void PrintImage(object o, PrintPageEventArgs e)
{
    int width = this.Width;
    int height = this.Height;

    Rectangle bounds = new Rectangle(0, 0, width, height);
    Bitmap img = new Bitmap(width, height);

    this.DrawToBitmap(img, bounds);

    float WidthPer, HeightPer;
    int NewWidth, NewHeight;
    int pageWidth = e.PageBounds.Width-100;
    int pageHeight = e.PageBounds.Height-100;

    float thumbnailAspectRatio = (float)pageWidth / pageHeight;
    float origImageAspectRatio = (float)width / height;

    if (thumbnailAspectRatio < origImageAspectRatio)
    {
        NewWidth = pageWidth;
        WidthPer = (float)pageWidth / width;
        NewHeight = Convert.ToInt32(height * WidthPer);
    }
    else
    {
        NewHeight = pageHeight;
        HeightPer = (float)pageHeight / height;
        NewWidth = Convert.ToInt32(width * HeightPer);
    }

    e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
    e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    Rectangle oRectangle = new Rectangle(50, 50, NewWidth, NewHeight);
    e.Graphics.DrawImage(img, oRectangle);
}
Posted
Comments
Sergey Alexandrovich Kryukov 13-Apr-11 0:37am    
Just a note: I never understood why printing a form at all? I never saw any decent applications doing that. A printed stuff should be a document, floating...
--SA
CoderX71 13-Apr-11 0:49am    
The problem is the application lives in a secured network and is not allowed to access other documents / application to send the data too. As such I'm limited in what I can do, the only option I have is to print the form it self.
Oshtri Deka 13-Apr-11 7:38am    
If form has dataGridView, ListView or similar control for data presentation, why don't you print data only? If you have access to form, than you should have access to it's controls as well.
CoderX71 13-Apr-11 17:58pm    
Looks like the client is going to have to accept that a screen shot is not possible. The rich textbox option looks like a perfect solution.

1 solution

So you are basically printing a screen shot? not good.

You should be creating a print document based on the data.

so you go through each data field and draw that on the print document where you want it. This is not so simple as you will need to calculate positioning and also take page breaks into account.

I would suggest you create a couple of 'helper' classes that you can store a list of data values (and matching labels)

And then you print document event can loop through them and draw each value on a new line (for a very simple layout)

Before you draw each control you will want to check if the intended position is in the region of the printable area. If not you need to add a new page and continue drawing.

Maybe a link[^] will help you better with this...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900