How to render pdfs using C# - c#

I want to load and draw pdf files graphically using C#. I don't need to edit them or anything, just render them at a given zoom level.
The pdf libraries I have found seem to be focussed on generation. How do I do this?
Thanks.

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:
How can I take preview of documents?
Get a preview jpeg of a pdf on windows?
.NET open PDF in winform without external dependencies
PDF Previewing and viewing
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.Drawingand 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.

Use the web browser control. This requires Adobe reader to be installed but most likely you have it anyway. Set the UrL of the control to the file location.

You can add a NuGet package CefSharp.WinForms to your application and then add a ChromiumWebBroweser control to your form.
In the code you can write:
chromiumWebBrowser1.Load(filePath);
This is the easiest solution I have found, it is completely free and independent of the user's computer settings like it would be when using default WebBrowser control.

I would like to mention Docotic.Pdf library here. It can convert PDF to image in C# and VB.NET. And it can also render and print PDF documents.
The library is 100% managed without external dependencies. It does not depend on System.Drawing.dll or GDI+. You will get consistent output on Windows, Linux, macOS, iOS, and Android.
I am one of Docotic.Pdf developers, so I really like it. Please try it and you will probably like it, too.

You can also just return a byte[] of a pdf as a file. Here is an example of returning a rendered pdf in a C# web application from a local pdf file. Ideally the pdf would be stored as a byte[] in the database.
public async Task<ActionResult> ViewDocument(GetPdf pdfdata)
{
MemoryStream ms = new MemoryStream();
FileStream stream = new FileStream("Graph.pdf", FileMode.Open, FileAccess.Read);
stream.CopyTo(ms);
return File(ms.ToArray(), "application/pdf");
}

Dynamic PDF Viewer from ceTe software might do what you're looking for. I've used their generator software and was pretty happy with it.
http://www.dynamicpdf.com/

The easiest lib I have used is Paolo Gios's library. It's basically
Create GiosPDFDocument object
Create TextArea object
Add text, images, etc to TextArea object
Add TextArea object to PDFDocument object
Write to stream
This is a great tutorial to get you started.

This looks like the right thing: http://code.google.com/p/lib-pdf/

You could google for PDF viewer component, and come up with more than a few hits.
If you don't really need to embed them in your app, though - you can just require Acrobat Reader or FoxIt (or bundle it, if it meets their respective licensing terms) and shell out to it. It's not as cool, but it gets the job done for free.

Related

Open PDF in C# as view only without adobe

im needing to create a form in my C# project that just allows the user to view the pdf.
i have a way to open the pdf and read it but i need to disable features like printing, saving, highlighting, copy/pasting while maintaining the ability to search in the document
they should really just be able to open the document, read it,search for words in the document, close it
any help would be great
thanks in advanced
You could use Ghostscript to convert PDF to images and then show the images on your form or you could rasterize your PDF directly to the screen.
To use Ghostscript from .NET you can take a look at the Ghostscript.NET library (managed wrapper around the Ghostscript library).
Ghostscript Viewer C# sample that rasterizes PDF directly to the screen can be found here: https://github.com/jhabjan/Ghostscript.NET/tree/master/Ghostscript.NET.Viewer
To search for the text inside the pdf you can use iTextSharp
(Disclaimer I worked on this component at Software Siglo XXI)
If you don't want to mess with Ghostscript API and need a quick working solution to visualise the documents, you could use ImageZoom Viewer .NET. It's available for both 32 and 64 bit and is very cheap and effective. I'd recommend you to try it since it's a very fancy and fast. You can browse, scroll and print the pages from the viewer.
You can take a look here: http://softwaresigloxxi.com/ImageZoom.html
This is for quick browsing and reading. Then, when you want to use text operations, you could let the user to use Adobe Reader, launching the PDF from there.

Displaying PDF on WebBrowser Control not working

I Have a test.pdf and I want to display it inside in my form.
My code is very simple:
public Form1()
{
InitializeComponent();
this.wbPdf.Navigate(#"file:///<fullpath>\test.pdf#toolbar=0");
}
and it's not working. It's showing me a white page with an "X".
But if I do instead this:
this.wbPdf.Navigate(#"file:///<fullpath>\test.pdf#toolbar=0", true);
the IE opened and it showed my pdf. Of course, I tried to use false in the second parameter and it's not working.
Also, if I tried to do something like
this.wbPdf.Navigate(#"http://www.google.com");
it showed me google, so I think there isn't any problem of configuration of Web Browser Control
Any ideas? I'm not will be able to have Acrobat Reader installed, so using ActiveX components is not an option (also, this project is in x64 and I've read that this component is not working very well in x64).
I want to display the pdf only for viewing inside the form, not in another window.
AFAIK, the web browser control in WinForms relies on the default PDF reader (usually Acrobat Reader) for displaying PDF files. If you need to display PDF files without requiring any other piece of software to be installed, then you will probably need to use a PDF rendering library in your application. Some examples of PDF rendering libraries:
MuPDF A GPL/Commercial viewer with a .net wrapper, you need a commercial license for use on commercial closed-source applications.
Amyuni PDF Creator .Net Commercial library for editing or displaying PDF files. Disclaimer: I currently work as a developer of the library.
Another option would be to create a local HTML5 page that renders the PDF file using the project pdf.js, then embeed that page on your web browser control. But this will only work on Windows systems with IE 9.0 or above.

Video Thumbnail generation

i am currently exploring on how to create thumbnail from video file. so far, i am able to generate thumbnail using ffmpeg but can somebody suggest me if there any other ways to generate thumbnail without involving executable(.exe) file.
Thanks,
New Learner, Please guide me...
ffmpeg.exe is the best solution for Video Thumbnails generation as it is free software licensed under the LGPL or GPL.
There are few other places where you will find the solution:
C# Wrapper for the AviFile Library
DirectShow .NET
SlimDX is a free open source framework that enables developers to easily build DirectX applications using .NET technologies such as C#
If you just need a generic thumbnail an easy way to get one is through the WindowsAPICodePack.Shell package. A small, medium, large, or extra large thumbnail can be generated. There's not as much control over the output as ffmpeg but it's hard to beat this method when a generic thumbnail will do or when avoiding an outside exe.
using Microsoft.WindowsAPICodePack.Shell;
using System.Drawing;
namespace GetVideoThumbnail
{
class VideoThumbnail
{
static void Main(string[] args)
{
string videoPath = #"C:\Users\NotSure\Desktop\SampleVideo.MP4";
// Create shell object
ShellObject videoShellObject = ShellObject.FromParsingName(videoPath);
// Create bitmap and/or save locally
Bitmap thumbnailBmp = videoShellObject.Thumbnail.LargeBitmap;
thumbnailBmp.Save(#"C:\Users\NotSure\Desktop\SampleVideoThumbnail.bmp");
}
}
}
Yea!,good this interestingly Q.
I worked it issue,my diploma tasking-at Uni.
And more yet Articles.
1.OpenCV - This Open source project Best worked C/C++,C# and Java
2.Emg CV - This good worked C#
, and more...
Try it's open sources.
You may want to try the VideoFileReader from AForge, specifically AForge.Video.FFMPEG.VideoFileReader. It makes use of ffmpeg DLLs.
Alternatively, on Windows 7, you could use ShellFile.Thumbnail from WindowsAPICodePack or use DirectShow to save a frame with VideoRenderer.
I recently used the ShellFile method, with VideoFileReader as backup.

How to highlight text in Pdf Winforms C#

I have a pdf file which I want to open in a Windows Forms Application and perform following tasks-
View the pdf document
Zoom +/- document
Search Text
Highlight a specific text
Show it in a listbox/dropdown
select those words and highlight in pdf
Remove selection/Highlight.
I have tried using certain libraries like pdfSharp/iTextSharp even Acrobat Reader OCX control.
Its really bugging me..is there any help??
I'd suggest looking at some means of converting the PDF if you don't have a direct need to edit it. Even then, it may be easier to convert to a different form, make changes, and then convert back. PDF is a form of PostScript, which makes it powerful, but also makes it a mess to deal with and my personal preference is to skip that headache. Not always avoidable (had a lot of fun creating Thai support in PDF print#home ticket creation once without bloating the document beyond unusable), but highly recommended where possible.
Anyways, there are a variety of PDF conversion libraries out there, some of which may be available for .NET. Worst case, you may need to create a managed C++ layer to allow your C# code to access them.
Doesn't acrobat reader OCX already have all those features ? What exactly doesnt the OCX do that you need to do in your code ?
You might try contacting Adobe and getting their full SDK for PDF. It might have controls which you can use to solve your problem.
Come to think of it , is there even an SDK for PDF from Adobe ?
You have not mentioned your preference of using Free or Commercial PDF Viewer option. If you are open to use Commercial PSF viewer, you may evaluate SyncFusion PDF Viewer control, Telerik PDF Viewer, Dynamic PDF Viewer or TallComponents. I have checked feature set and all seem to have features you are looking for. I do not represent or promote any of these SDKs, I have used TallComponents and Dynamic PDF for PDF manipulation and both have excellent support, I would say PDF Veterans in .NET space.

How do I display office and/or pdf content on a windows form?

We have an application in which admin members can add content for their subordinates to view. Their requirement is that it should be able to display Word, Excel, PowerPoint and PDF documents in a non-editable manner.
The one option that I found for doing this is to have the content loaded into a web browser component. The downside to that is that it prompts the user to open/save/cancel. We are concerned that the subordinates, being mostly computer illiterate, will have trouble opening the documents in this manner.
Using the above method also means that Microsoft Office and Adobe Acrobat (or another IE enabled PDF viewer) need to be installed on all the machines that will be running the application, which implies expensive licensing fees.
Is there a better way to get this content to display on my forms in C#?
Possibly interesting as well:
Save the documents to XPS using Microsoft Office 2007 (or print them to an XPS printer).
You can display the read-only XPS document either using the XPS viewer component or render page by page into a PNG or JPEG image. This rendering can be achieved quite easily using .NET 3.5 / WPF.
XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
const double scaleFactor = 0.8;
for (int pageNum = 0; pageNum < docSeq.DocumentPaginator.PageCount; pageNum++)
{
DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);
// FIX: calling GetPage without calling UpdateLayout causes a memory leak
((FixedPage)docPage.Visual).UpdateLayout();
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)Math.Round(scaleFactor * docPage.Size.Width),
(int)Math.Round(scaleFactor * docPage.Size.Height), (int)Math.Round(scaleFactor * 96), (int)Math.Round(scaleFactor * 96), PixelFormats.Default);
renderTarget.Render(docPage.Visual);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 75;
// Choose type here ie: JpegBitmapEncoder, etc
//BitmapEncoder encoder = new PngBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
string pageImageFileName = string.Format("{0}-{1}.jpg", Path.Combine(Path.GetDirectoryName(xpsFileName), Path.GetFileNameWithoutExtension(xpsFileName)), pageNum);
using (FileStream pageOutStream = new FileStream(pageImageFileName, FileMode.Create, FileAccess.Write))
{
encoder.Save(pageOutStream);
}
}
This code needs references to the PresentationCore, PresentationFramework and ReachFramework assemblies.
EDIT: The code above contained a memory leak (see Opening XPS document in .Net causes a memory leak). The workaround has been been inserted in the example.
SpreadsheetGear for .NET has an Excel Compatible Windows Forms control which will display your Excel workbooks (it will do lot more than that if you want it to). You can see what people say and download the free trial if you want to give it a try.
SpreasheetGear can also create images from charts and ranges of cells if you need to generate images to display in a web page.
Have you looked at Microsoft Word 9.0 object library ? It might not be possible to just display the data as it was originally written, however, you COULD do something evul here, how about, printing as a temporary pdf in memory and displaying that?
This is how you display a PDF with C#
All this is windows specific.
If you wish to display something on a client machine without relying on any local install then you must take total responsibility for the rendering either by:
Supplying some sort of non invasive libraries that run at the client and know how to render it
Use the 'proper' tools/libraries to render it on the server to an in memory image and send that image to the client. Slow, very computationally expensive on your server and will not provide a 'document like' interface to your clients.
Sumatra is completely free and open source. It would not require any form of install, thus including it in your application install as a binary in a sub folder and then shelling out directly to that to display pdf's will work fine (either the pdf ids network accessible so it's as simple as executing
SumatraPDF.exe {path-to-file}
If it is not network accessible download it in the background to a temporary location and then execute as above.
Office documents are a bit more tricky since they all require a local install. Here's a (out of date) list Note that many of the links to downloads will then point you to the very latest version which is recommended.
An alternate approach for this is to use OpenOffice.org in it's 'portable' incarnation which will allow it to run without requiring an install (so you can drop it in place just like the Sumatra approach) this however has a great many flaws in your case because it would still require java to be installed, the resulting fiels would be editable (unless you made changes to the OpenOffice version which may well be complex) and you may not get terribly good display.
If you have any sort of ability to run arbitrary programs on install of your application installing the viewers is probably for the best, they are entirely free and redistributable.
If you have access to SharePoint you can try an entirely different approach which is to do it all via a web application. The sharepoint plugins will allow hosting views on the documents directly in the browser. Note that this pretty much requires Internet Explorer to be fully usable though.
Disclaimer, I'm from Atalasoft
If you want to display PDF in any kind of .NET GUI (Winforms, ASP.NET, Silverlight, WPF), our DotImage with PDF Reader add-on supports it. It doesn't use Adobe and doesn't require anything to be installed on the client-machine or server (just our assemblies).

Categories