Displaying a pdf file from Winform

I would put it on within my program folder, add a link within my Start Menu folder to allow a direct access (without starting my tool) and just at on some click event System.Diagnostics.Process.Start(@".\Manual.pdf");

Update

Ok, now we come to a completely new question: How to embed a file in my application and start it?

For this question you'll find already several answers here, but here is the short version:

  1. Right click your project and select Add - Existing Item
  2. Select your file (don't double click it)
    • Click the little arrow next to the Add button and select Add As Link
  3. Double click on Properties - Resources.resx
  4. Click the little arrow next to Add Resource and select Add Existing File
  5. Select the same file again in the open dialog
  6. Now you can access the file within your code as byte[] from Properties.Resources.NameOfResource

With these steps you reference your file where ever it exists within your structure. If you like that a copy of your pdf file will be put into a subfolder Resources within your project, just skip the points one and two in the above list.

To get your pdf now opened, you'll have to write the byte[] down to disk (maybe with Path.GetTempFileName()) and start it with Adobe Reader. (Don't forget to delete the file after usage)


You can reference the Adobe Reader ActiveX control and bundle it with your application.

Simply add AcroPDF.PDF.1 to your Toolbox from the COM Components tab (right click toolbox and click Choose Items...) then drag an instance onto your Winform to have the designer create the code for you. Alternately, after adding the necessary reference you can use the following code:

AxAcroPDFLib.AxAcroPDF pdf = new AxAcroPDFLib.AxAcroPDF();
pdf.Dock = System.Windows.Forms.DockStyle.Fill;
pdf.Enabled = true;
pdf.Location = new System.Drawing.Point(0, 0);
pdf.Name = "pdfReader";
pdf.OcxState = ((System.Windows.Forms.AxHost.State)(new System.ComponentModel.ComponentResourceManager(typeof(ViewerWindow)).GetObject("pdfReader.OcxState")));
pdf.TabIndex = 1;

// Add pdf viewer to current form        
this.Controls.Add(pdf);

pdf.LoadFile(@"C:\MyPDF.pdf");
pdf.setView("Fit");
pdf.Visible = true;

You could use the WebBrowser control and let IE load a PDF reader for you if there is one installed on the machine.

However the last time I tried this, I had to write the PDF file to disk first, so I could point the WebBrowser control at it.

Tags:

C#

Winforms

Pdf