Windows Develop Bookmark and Share   
 index > Windows Forms General > How to generating PDF documents from Windows Application?
 

How to generating PDF documents from Windows Application?

Hi,

I want to save/Convert the windows forms to a PDF File when the user clicks "Create PDF" Button.

The look and feel of the PDF should be same as in the Forms.

Is there any tool to do this process or is there any way to acheive this?

Any help is greatly appreciated.

Thanks in Advance,
Edwin Aloysius.
Edwin Aloysius  Thursday, September 10, 2009 5:08 AM

Hi Edwin,

We can use the open source library iTextSharp to convert a form to a pdf. These are the steps:

1. Get the image of the form using BitBlt API or DrawToBitmap method of the form.

2. Add the image to the pdf document and save the pdf.

This is a tutorial of iTextSharp:
http://69.10.233.10/KB/graphics/iTextSharpTutorial.aspx?display=PrintAll&fid=397222&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=2182599

This is a code snippet about how to save a form to a pdf:

    //Save the form to a pdf.
    public bool FormToPdf(string sFilePDF, System.Windows.Forms.Form form)
    {
        bRet = false;

        // step 1: creation of a document-object
        Document document = new Document();

        try
        {
            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(sFilePDF, FileMode.Create));

            // step 3: we open the document
            document.Open();
            
            // step 4: 
            //Get the image of the form.
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(form.Width, form.Height);
            this.DrawToBitmap(form, bitmap);      
            //Add image to the document.
            Image img = Image.GetInstance(bitmap, Color.WHITE);
            document.Add(img);

            bRet = true;
        }
        catch (DocumentException de)
        {
            this.Message = de.Message;
        }
        catch (IOException ioe)
        {
            this.Message = ioe.Message;
        }

        // step 5: we close the document
        document.Close();

        if (bRet)
            this.Message = sFilePDF + " has been created";

        return bRet;
    }

    [DllImport("gdi32.dll")]
    static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int
    nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

    //A method to draw a Form to an image.
    private void DrawToBitmap(Form f, System.Drawing.Bitmap image)
    {

        System.Drawing.Graphics g = f.CreateGraphics();
        System.Drawing.Graphics g2 = System.Drawing.Graphics.FromImage(image);
        IntPtr gi = g.GetHdc();
        IntPtr gi2 = g2.GetHdc();
        BitBlt(gi2, 0, 0, f.Width, f.Height, gi, 0, 0, 0x00CC0020);
        g.ReleaseHdc();
        g2.ReleaseHdc();
        g.Dispose();
        g2.Dispose();
    }



Let me know if this does not help.

Aland Li

This response contains a reference to a third party World Wide Web site or a third party control. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites or controls and has not tested any software or information found on these sites or controls; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 14, 2009 6:54 AM
You will need a PDF library for that. There are plenty of commercial ones out there, try www.componentsource.com or try searching for a FREE one.
VB.NET to C# http://www.developerfusion.com/tools/convert/vb-to-csharp/
Se3ker385  Thursday, September 10, 2009 5:55 AM

Hi Edwin,

We can use the open source library iTextSharp to convert a form to a pdf. These are the steps:

1. Get the image of the form using BitBlt API or DrawToBitmap method of the form.

2. Add the image to the pdf document and save the pdf.

This is a tutorial of iTextSharp:
http://69.10.233.10/KB/graphics/iTextSharpTutorial.aspx?display=PrintAll&fid=397222&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=2182599

This is a code snippet about how to save a form to a pdf:

    //Save the form to a pdf.
    public bool FormToPdf(string sFilePDF, System.Windows.Forms.Form form)
    {
        bRet = false;

        // step 1: creation of a document-object
        Document document = new Document();

        try
        {
            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(sFilePDF, FileMode.Create));

            // step 3: we open the document
            document.Open();
            
            // step 4: 
            //Get the image of the form.
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(form.Width, form.Height);
            this.DrawToBitmap(form, bitmap);      
            //Add image to the document.
            Image img = Image.GetInstance(bitmap, Color.WHITE);
            document.Add(img);

            bRet = true;
        }
        catch (DocumentException de)
        {
            this.Message = de.Message;
        }
        catch (IOException ioe)
        {
            this.Message = ioe.Message;
        }

        // step 5: we close the document
        document.Close();

        if (bRet)
            this.Message = sFilePDF + " has been created";

        return bRet;
    }

    [DllImport("gdi32.dll")]
    static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int
    nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

    //A method to draw a Form to an image.
    private void DrawToBitmap(Form f, System.Drawing.Bitmap image)
    {

        System.Drawing.Graphics g = f.CreateGraphics();
        System.Drawing.Graphics g2 = System.Drawing.Graphics.FromImage(image);
        IntPtr gi = g.GetHdc();
        IntPtr gi2 = g2.GetHdc();
        BitBlt(gi2, 0, 0, f.Width, f.Height, gi, 0, 0, 0x00CC0020);
        g.ReleaseHdc();
        g2.ReleaseHdc();
        g.Dispose();
        g2.Dispose();
    }



Let me know if this does not help.

Aland Li

This response contains a reference to a third party World Wide Web site or a third party control. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites or controls and has not tested any software or information found on these sites or controls; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 14, 2009 6:54 AM
Thanks Aland Li for the information you have given.

Thanks,
Edwin
Edwin Aloysius  Friday, September 18, 2009 10:08 AM

You can use google to search for other answers

Custom Search

More Threads

• Quick DataGridView Question
• FolderBrowserDialog slow to open
• Create list of object...
• pop up internet browser from windows application
• Context menu for Toolstipmenuitems
• BufferedGraphicsContext Allocate question.
• Protocol Handler - Bring window to foreground
• DataGridViewCell - ParseFormattedValue Method Question
• Store RichTextBox's content in Access
• MainMenu ???????????????????????????????????????????????????????????????????