TL;DR
- https://github.com/pvginkel/PdfiumViewer is recommended if you’re okay with just viewing PDFs.
- If you need more complicated features, then convert the PDF into base64 and pass it to a javascript function which decodes and call the PDFViewerApplication.open which is implemented at viewer.js
- However, it’s really, really slow.
- The example source is at https://github.com/heejune/WinForm-PDFjs
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(https://github.com/pvginkel/PdfiumViewer).
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:
- Include the web browser control into the target project
- Download PDFjs and make the C# code locate it.
- Navigate the web browser control to the viewer.html which is located at ‘pdfjs-1.4.20-dist/web/viewer.html’
- [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.
- [From viewere.js] Add a helper function which decodes the base64 into an ArrayBuffer and call PDFViewerApplication.open with it.
Code#1. PDF file into base64 and pass it to webbrowser control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("PDFViewerApplication.open", 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/heejune/WinForm-PDFjs/blob/master/WinFormPDFViewer/pdfjs-1.4.20-dist/web/viewer.js | |
// PDFViewerApplication.open 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); | |
} | |
PDFViewerApplication.open(bytes) | |
}; |
The complete example is https://github.com/heejune/WinForm-PDFjs
Got hints following:
http://stackoverflow.com/questions/21797299/convert-base64-string-to-arraybuffer
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…