This being the case, all PDF viewers must be built on top of rendering functionality, although PDFs can also be rendered for printing or saving in the desired image format.
PDF rendering is the term used to describe the translation and transport of (usually) web-based pages into PDF format directly on-screen and (usually) for onward use as a saved file, for despatch to a mobile device or for printing.
Google has open sourced its excellent PDF rendering engine - PDFium - that it wrote with Foxit Software.
There is a C# nuget package called PdfiumViewer which gives a C# wrapper around PDFium and allows PDFs to be displayed and printed.
I have used it and was very impressed with the quality of the rendering.
PDFium works directly with streams so it doesn't require any data to be written to disk.
This is my example from a WinForms app
public void LoadPdf(byte[] pdfBytes)
{
var stream = new MemoryStream(pdfBytes);
LoadPdf(stream)
}
public void LoadPdf(Stream stream)
{
// Create PDF Document
var pdfDocument = PdfDocument.Load(stream);
// Load PDF Document into WinForms Control
pdfRenderer.Load(pdfDocument);
}
Edit: To get the pdfRenderer control in WinForm: Add the PdfiumViewer
NuGet package to the project; open the projects packages folder in Windows Explorer and drag the PdfiumViewer.dll
file onto the Toolbox
window; A control called PdfRenderer
will be available to add:
There are a few other choices in case the Adobe ActiveX isn't what you're looking for (since Acrobat must be present on the user machine and you can't ship it yourself).
For creating the PDF preview, first have a look at some other discussions on the subject on StackOverflow:
In the last two I talk about a few things you can try:
You can get a commercial renderer (PDFViewForNet, PDFRasterizer.NET, ABCPDF, ActivePDF, XpdfRasterizer and others in the other answers...).
Most are fairly expensive though, especially if all you care about is making a simple preview/thumbnails.
In addition to Omar Shahine's code snippet, there is a CodeProject article that shows how to use the Adobe ActiveX, but it may be out of date, easily broken by new releases and its legality is murky (basically it's ok for internal use but you can't ship it and you can't use it on a server to produce images of PDF).
You could have a look at the source code for SumatraPDF, an OpenSource PDF viewer for windows.
There is also Poppler, a rendering engine that uses Xpdf as a rendering engine. All of these are great but they will require a fair amount of commitment to make make them work and interface with .Net and they tend to be be distributed under the GPL.
You may want to consider using GhostScript as an interpreter because rendering pages is a fairly simple process.
The drawback is that you will need to either re-package it to install it with your app, or make it a pre-requisite (or at least a part of your install process).
It's not a big challenge, and it's certainly easier than having to massage the other rendering engines into cooperating with .Net.
I did a small project that you will find on the Developer Express forums as an attachment.
Be careful of the license requirements for GhostScript through.
If you can't leave with that then commercial software is probably your only choice.
Here is my answer from a different question.
First you need to reference the Adobe Reader ActiveX Control
Adobe Acrobat Browser Control Type Library 1.0
%programfiles&\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll
Then you just drag it into your Windows Form from the Toolbox.
And use some code like this to initialize the ActiveX Control.
private void InitializeAdobe(string filePath)
{
try
{
this.axAcroPDF1.LoadFile(filePath);
this.axAcroPDF1.src = filePath;
this.axAcroPDF1.setShowToolbar(false);
this.axAcroPDF1.setView("FitH");
this.axAcroPDF1.setLayoutMode("SinglePage");
this.axAcroPDF1.Show();
}
catch (Exception ex)
{
throw;
}
}
Make sure when your Form closes that you dispose of the ActiveX Control
this.axAcroPDF1.Dispose();
this.axAcroPDF1 = null;
otherwise Acrobat might be left lying around.
PdfiumViewer is great, but relatively tightly coupled to System.Drawing
and WinForms. For this reason I created my own wrapper around PDFium: PDFiumSharp
Pages can be rendered to a PDFiumBitmap which in turn can be saved to disk or exposed as a stream. This way any framework capable of loading an image in BMP format from a stream can use this library to display pdf pages.
For example in a WPF application you could use the following method to render a pdf page:
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using PDFiumSharp;
static class PdfRenderer
{
public static ImageSource RenderPage(string filename, int pageIndex, string password = null, bool withTransparency = true)
{
using (var doc = new PdfDocument(filename, password))
{
var page = doc.Pages[pageIndex];
using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, withTransparency))
{
page.Render(bitmap);
return new BmpBitmapDecoder(bitmap.AsBmpStream(), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First();
}
}
}
}
ABCpdf will do this and many other things for you.
Not ony will it render your PDF to a variety of formats (eg JPEG, GIF, PNG, TIFF, JPEG 2000; vector EPS, SVG, Flash and PostScript) but it can also do so in a variety of color spaces (eg Gray, RGB, CMYK) and bit depths (eg 1, 8, 16 bits per component).
And that's just some of what it will do!
For more details see:
http://www.websupergoo.com/abcpdf-8.htm
Oh and you can get free licenses via the free license scheme.
There are EULA issues with using Acrobat to do PDF rendering. If you want to go down this route check the legalities very carefully first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With