简体   繁体   中英

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 :)

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.

you can use activex component that comes with Acrobat Reader.

How to render pdfs using C#

Question is rather old, but proposed solutions have significant drawbacks:

  • WebBrowser control relies on the IE and that it can display PDF documents (this is true only if Adobe Reader is installed)
  • GhostScript is licensed under AGPL and it requires rather expensive license for usage in closed source/commercial projects.

Fortunately free alternative exists: poppler tools (based on xpdf codebase) which are licensed under GPL and may be used as console utility. From .NET code it may be executed with System.Diagnostics.Process.

To simplify poppler tools usage we've developed NReco.PdfRenderer .NET wrapper that embeds poppler windows binaries (they're extracted on first use) and provides simple API for rendering PDF pages to image:

var pdfToImg = new NReco.PdfRenderer.PdfToImageConverter();
Image firstPageImg = pdfToImg.GenerateImage( "test.pdf", 1);

Component is not free, but its pricing is very reasonable.

ImageGear for .NET

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM