Windows Forms: printing a pdf file in C# 2.0

The problem: I have a Windows application developed in C# 2.0 and I want to print a pdf document.
 
The solution: Acrobat Reader 6.5 and higher are suposed to have an activex control that you can use inside C# to manipulate a pdf document.
This was tested against Acrobat Reader 8, using Visual Studio 2005.
 
Steps:
1 – You need to have Acrobat Reader 8 installed in your machine.
2 – Make a reference to AcroPDF.dll, you should find it at C:\Program Files\Common Files\Adobe\Acrobat\ActiveX.
3 – Add the Adobe PDF Reader to your control’s toolbox: go to the Tools menu / Choose Toolbox Items / Tab COM Components and select the checkbox for the Adobe PDF Reader control.
4 – At this stage, you should have the control in your toolbox. It should be under the General group. Drag it to your form.
5 – Add the following code:
 
private void button1_Click(object sender, EventArgs e)
        {
            this.axAcroPDF1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.axAcroPDF1.Enabled = true;
            this.axAcroPDF1.Location = new System.Drawing.Point(0, 0);
            this.axAcroPDF1.Name = "axAcroPDF1";
            this.axAcroPDF1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axAcroPDF1.OcxState")));
            //if you want to visualize the document before print
            this.axAcroPDF1.Size = new System.Drawing.Size(292, 266);
            this.axAcroPDF1.TabIndex = 0;
            this.axAcroPDF1.TabStop = false;
            //If you do not want to visualize the document
            this.axAcroPDF1.Visible = false;
            //wherever you load the document you put this:
            this.axAcroPDF1.LoadFile("C:\\MyTmp\\foo\\tmp\\2delete\\Report3.pdf");

            //wherever you want to print the document:
            this.axAcroPDF1.printAll();//N.B. if you do not set in advance the
            // print options of the document it will 
            //be printed by default to the Windows
            //MDI document printer, which is not very
            // convenient, so take care to set any
            //necessary printing parameter
        }
This entry was posted in Development. Bookmark the permalink.

Leave a comment