2

I am trying to display a PDF in a winform using C# .net

I have included the iTextSharp Library, and successfully opened the PDF file, however I get a byte[] back from iTextView

 PdfReader reader = new PdfReader(GetURI("test.pdf")); 
 reader.getPageN(1); //returns byte array

I can't find much documentation on this library, and I was wondering how I would get the PDF on the actual form, be it in a picture box or a webview. How do I display the pages of the PDF?

EDIT:

I Don't want to open it in a third party reader

I don't want dependencies on adobe reader

I want to focus on a solution with iTextSharp or something similar, as I need to secure the PDF, encrypt it and eventually alter it.

ddoor
  • 5,819
  • 9
  • 34
  • 41
  • The iTextSharp library is for parsing and creating PDFs only; it does not have any functionality for rendering/displaying documents. – Bradley Smith Apr 23 '15 at 08:50
  • Sigh, this site used to be useful years ago. I will hit the forums, thanks anyway. – ddoor Apr 23 '15 at 08:57

5 Answers5

4

You can easily display PDF in WebBrowser control. Add a webBrowser control to your Winform. Add the following method to your form.

private void RenderPdf(string filePath)
{
    if (!string.IsNullOrWhiteSpace(filePath))
    {
        webBrowser1.Navigate(@filePath);
    }
}

Call this method by passing PDF file path,

RenderPdf(@"PDF path");
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
  • 1
    No this will prompt the browser to either download the PDF or to open it in some third party software, I don't want this, I need it in the form. – ddoor Apr 23 '15 at 08:45
  • Take a look at: http://stackoverflow.com/q/4504442/952310 – Yair Nevet Apr 23 '15 at 08:48
3

ITextSharp allows you to create and manipulate pdf's, but does not provide any rendering options like Bradley Smith said in a comment above

I did something similar a long time ago and what I ended up doing was using Ghostscript to generate a tiff image from my pdf and displaying that instead. Obviously that just displays an image so if you need to edit the pdf, this won't work for you.

Ghostscript is command line only so I think you have to run it something like this:

       Process.Start(@"c:\gs\gs.exe",
            String.Format("-o {0} -sDEVICE=tiffg4 -dFirstPage={1} -dLastPage={2} {3}", "tiffPages.tif", fromPage, toPage, "inputfile.pdf"));
BunkerMentality
  • 1,327
  • 16
  • 21
2

This question is a copy of this

The answer I found:

i think the easiest way is to use the Adobe PDF reader COM Component right click on your toolbox & select "Choose Items" Select the "COM Components" tab Select "Adobe PDF Reader" then click ok Drag & Drop the control on your form & modify the "src" Property to the PDF files you want to read i hope this helps

Community
  • 1
  • 1
Thealon
  • 1,935
  • 1
  • 13
  • 22
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – karlingen Apr 23 '15 at 09:11
  • @karlingen It did answer his question before he made an edit. – Thealon Apr 23 '15 at 09:12
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Nikolay Kostov Apr 23 '15 at 09:17
  • @NikolayKostov you are right sir, here is the answer. (Rather explain to me why downvote so I can fix my answer than downvote it without explanation – Thealon Apr 23 '15 at 09:28
1

what about using a viewer control from any vendor? Found it on the first page in google: viewer control for windows forms

Hugo Moreno
  • 150
  • 7
0

If a commercial library is an option, try Amyuni PDF Creator .Net. You will be able to display your PDF document in a form, modify the document or encrypt it. You will not need to rely on any other program to be installed, or any external processes.

As other answers and comments have mentioned, iText is not designed for displaying PDF documents.

Disclaimer: I work for Amyuni Technologies.

yms
  • 10,361
  • 3
  • 38
  • 68