Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a PDF in winforms [duplicate]

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.

like image 310
ddoor Avatar asked Dec 29 '25 13:12

ddoor


2 Answers

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");
like image 149
Kurubaran Avatar answered Jan 01 '26 02:01

Kurubaran


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"));
like image 21
BunkerMentality Avatar answered Jan 01 '26 01:01

BunkerMentality