tags:

views:

602

answers:

4
+1  Q: 

Preview PDF in C#

I'm looking for .NET GUI component (different than PDFsharp) allowing preview PDF 1-page document.
Basically I need something similar to PictureBox where I can load bitmaps and show it.

It would be great if that component allows zooming and moving picture inside.
Freeware solutions preferred :)

+1  A: 

you can use activex component that comes with Acrobat Reader.

http://stackoverflow.com/questions/518878/how-to-render-pdfs-using-c

Andrey
Yes, you can, but the end-user must have Adobe Reader installed...
Rowan
Reader is freeware so this is not big deal to me...
Maciej
A: 

ImageGear for .NET

Not free. Arguably has a larger scope than what you're concerned about. I hope it's somehow helpful.

lance
Looks interesting but pricing per runtime client is not acceptable in my case...
Maciej
A: 

Quick PDF Library, my companies PDF SDK, will help you to render PDF files. It's not freeware, but the license allows for royalty-free distribution of compiled apps that you build with it. Finding free / open source components for rendering PDF files is a little trickier than other basic PDF manipulation tasks because rendering PDF files can be quite difficult.

Here is some C# sample source code that shows you how to render the PDF in the picture box on your form.

private void Form1_Load(object sender, EventArgs e)
{
    QuickPDFAX0718.PDFLibrary pdf = new QuickPDFAX0718.PDFLibrary();

    qp.UnlockKey("......Licence Key......");

    // Open PDF File
    int Handle = qp.DAOpenFile("C:\\sample.pdf", null);

    // Get Total Number of Pages in a PDF File
    int PageCount = qp.DAGetPageCount(Handle);

    int PageNo = 1;

    // It will get Reference of page 1 from PDF file
    int PageRefNo = qp.DAFindPage(Handle, PageNo);

    // You can change this parameter for Zoom In/Zoom Out purpose
    int Zoom = 76;
    double pageWidth = qp.DAGetPageWidth(Handle, PageRefNo) / Zoom;
    double pageHeight = qp.DAGetPageHeight(Handle, PageRefNo) / Zoom;

    // DPI use for rendering the page. Increase DPI will increate quality of image
    int dpi = 92;

    // Calculate Dimension of final output image
    Bitmap b = new Bitmap(Convert.ToInt32(pageWidth * dpi), Convert.ToInt32(pageHeight * dpi));

    // This will Draw render image on GDI
    using (Graphics g = Graphics.FromImage(b))
    {
    IntPtr dc = g.GetHdc();
    qp.DARenderPageToDC(Handle, PageRefNo, dpi, (int)dc);
    g.ReleaseHdc(dc);
    }

    // Assigne rendered image to PictureBox Control which will display PDF on Windows Form.
    pictureBox1.Image = b;
    pictureBox1.BorderStyle = BorderStyle.Fixed3D;
}

The library doesn't include built-in functions to help you with zooming, but since you're rendering the PDF as a BMP image you can take care of the zooming fairly easily.

Rowan
Looks nice indeed. But easier and cheaper to me is use @bryanjonker's method :)
Maciej
Easier and cheaper for you, but not necessarily your end user. ;-)
Rowan
+1  A: 

Another option is to use the WebBrowser control in your GUI. It's going to use the browser to render the PDF, but I'd do that route rather than mess around with the Adobe Reader ActiveX component.

If you don't want any type of PDF reader available on the client, you could also convert it to a graphic file through GhostScript and display it as a bitmap.

bryanjonker
Good idea! Can you give me some tips how can I open it in webbrowser ?I'm setting webbrowser.URL to eg "file://c:/temp/test.pdf" but it doesnt work - it opens Acro Reader as separate process...(Same as IExplore...)
Maciej
OKAY I found solution here: http://kb2.adobe.com/cps/331/331025.html
Maciej