Eclipsed4utoo's Blog
Not Your Ordinary Programmer

C# – View PDF In Windows Form

Here is a quick tutorial on how to view a PDF file in a Windows Form application.

I downloaded PDFSharp (free).  While it is a third-party application, it will still require the PC to have Adobe Reader (or better) installed.

And here is the small piece of code needed to display a PDF in a Windows Form:

string helpFile = "C:\HelpFile.pdf";

// the pdfAcroViewer is a control from PDFSharp
// You can add the control to the Toolbox in Visual Studio
//     then drag it onto the form.
pdfAcroViewer.LoadFile(helpFile);
pdfAcroViewer.ShowToolbar = false;
pdfAcroViewer.SetPageMode(PdfSharp.Pdf.PdfPageMode.FullScreen);

EDITED: After a number of questions in the comments, I decided to download the latest version and see if I could find the file.  The file itself is not in the latest version.  When I did this more than a year ago, the latest version was version 1.0.898.  That version is still for download on sourceforge.

(screenshot below)

PDFSharp

If you download that version, there will be directory located at “\PDFSharp\PdfViewer\PdfSharp.Viewing”.  In that directory, you will see a Visual Studio project with the name “PdfSharp.Viewing.csproj”.  Open the project in Visual Studio, and build the project in Release mode.  After building, you will have created a “bin\release” folder in that same directory.  You will need all of the .dlls in that directory(should be 6 of them).  One of them will be the “PdfSharp.Viewing.dll” which should contain the PdfAcroViewer control from the code above.

Post in the comments if this works for you.

Tags: , , ,

13 Responses to “C# – View PDF In Windows Form” »

  1. Stefan Says:

    Thanks for the tutorial, but which file in the PDFSharp download contains the pdfAcroViewer control (the one to be added to the VS Toolbox)?
    Greatful for help!

  2. Ryan Alford Says:

    It should be the PDFSharp.Viewing.dll.

  3. Sally Says:

    I downloaded all latest Pdfsharp zip files. But I could not find PDFSharp.Viewing.dll anywhere. WHere it could be?
    Thanks a lot.

  4. Ryan Alford Says:

    Ok, so I went looking for the file, and it’s no longer there. I did this a while back(probably more than a year ago). I used version 1.0.898. I just downloaded that version, and it wasn’t there either. However, there was a project for PDFSharp.Viewing. So I opened the project and built it, and it built the PDFSharp.Viewing.dll.

    So this is probably what you will need to do. In the bin directory where the .dll is created, there will be a number of other .dll files. You will also need those. I have updated the blog post.

  5. Arnel Says:

    Hello,
    I was able to download PDFSharp.Viewing.dll and get the pdfAcroViewer control working. My problem is with the deployment. I am using the clickonce with VS2008.

    When starting the application with the referce link that clickonce created the pdfAcroViewer control is not displayed and a null object reference error comes up. But when I start the appication by double clicking the application exe directly the control is displayed and no error.

    Thanks!
    Arnel

  6. Ryan Alford Says:

    @Arnel

    Are you including the .dll in the ClickOnce deployment?

  7. Arnel Says:

    Yes, all the dll’s are there. It works fine when I start the application by going directly to the install folder and double clicking on the exe.

  8. Arnel Says:

    Could it be something with the manifest of the dll’s?

  9. Ryan Alford Says:

    I don’t deal with ClickOnce that much. Is Adobe installed on the PC?

  10. Arnel Says:

    Yes, Adobe is installed.

  11. Neperz Says:

    I have the same problem! when I publish it’s not works. only work in dev enviroment.

  12. Ryan Alford Says:

    @Neperz

    Adobe Acrobat needs to be installed on the client.

  13. Rye Says:

    Hi,

    is it possible to hide the navigation toolbar in the pdf while in the runtime environment mode?

Leave a Comment