How to embed the PDFjs into your C# Project


If you needed to add PDF support feature into your C# based project, then using the PDFium & PDFViewer will be the easiest approach. Just include the PDFium via nugget package. You can also find the PdfiumViewer is on the github(

However, the wrapper control only provides basic features so that you might need to consider different solutions in case you required another features such as highlighting text, searching keyword(PdfiumViewer supports the search, but it only returns position information rather actually selecting searched keywords). That’s why I tried to embed the PDFjs into a C# project.

The overall process will be like this:

  1. Include the web browser control into the target project
  2. Download PDFjs and make the C# code locate it.
  3. Navigate the web browser control to the viewer.html which is located at ‘pdfjs-1.4.20-dist/web/viewer.html’
  4. [From C#] Convert the pdf file into base64 string. I did on purpose because the C# application currently I’m writing only handles the PDF as memory buffer. If you could refer to the target PDF as local file, then go with it.
  5. [From viewere.js] Add a helper function which decodes the base64 into an ArrayBuffer and call with it.

Code#1. PDF file into base64 and pass it to webbrowser control

if (locateText.Text.EndsWith("pdf", true, null) == true &&
File.Exists(locateText.Text) == true)
var buffer = File.ReadAllBytes(locateText.Text);
// Document.InvokeScript seems not understanding the Blob type
//object[] args = { fileBytes };
//webBrowser1.Document.InvokeScript("", args);
// so convert it to base64 and pass it
var asBase64 = Convert.ToBase64String(buffer);
webBrowser1.Document.InvokeScript("openPdfAsBase64", new[] { asBase64 });

Code#2. Open PDFjs with viewer.js and base64 string

// wrapper for base64 param
window.openPdfAsBase64 = function (base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);

The complete example is

Got hints following:

2 thoughts on “How to embed the PDFjs into your C# Project

  1. HI Heejune…I went through your github code and tried to run the c# applicatoin. However, navigation button (previous/next), zoomin, zoom out and all other buttons are not responding on click. Could you please let me know what can be the issue ? Further, how can I refer to the target PDF as local file as mentioned by you above.
    Thanks a lot in advance…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s