Late answer but it might help other people.
I am working on a Windows 8.1 x64 machine.
My task was to enable users to view PDF documents on the device (Tablet with Windows OS). After much of a research I couldn't find any feasible solution, or what I should say using WindowsFormsHost just feels dirty!
What I have done is to use a WebBrowser control like so:
<WebBrowser Source="pack://siteoforigin:,,,/path/to/your/file.html"/>
Note:
The pack://siteoforigin:
refers to a location when your application is running. So make sure your .html
files are set to Content
and Copy Always
.
Now that your html has been set up you need to now either download or just copy and paste the following .js
code:
var PDFObject = function (obj) {
if (!obj || !obj.url) { return false; }
var pdfobjectversion = "1.2",
id = obj.id || false,
width = obj.width || "100%",
height = obj.height || "100%",
pdfOpenParams = obj.pdfOpenParams,
url,
pluginTypeFound,
createAXO,
hasReaderActiveX,
hasReader,
hasGeneric,
pluginFound,
setCssForFullWindowPdf,
buildQueryString,
get,
embed;
createAXO = function (type) {
var ax;
try {
ax = new ActiveXObject(type);
} catch (e) {
ax = null;
}
return ax;
};
hasReaderActiveX = function () {
var axObj = null;
if (window.ActiveXObject) {
axObj = createAXO("AcroPDF.PDF");
if (!axObj) { axObj = createAXO("PDF.PdfCtrl"); }
if (axObj !== null) { return true; }
}
return false;
};
hasReader = function () {
var i,
n = navigator.plugins,
count = n.length,
regx = /Adobe Reader|Adobe PDF|Acrobat/gi;
for (i = 0; i < count; i++) {
if (regx.test(n[i].name)) {
return true;
}
}
return false;
};
hasGeneric = function () {
var plugin = navigator.mimeTypes["application/pdf"];
return (plugin && plugin.enabledPlugin);
};
pluginFound = function () {
var type = null;
if (hasReader() || hasReaderActiveX()) {
type = "Adobe";
} else if (hasGeneric()) {
type = "generic";
}
return type;
};
setCssForFullWindowPdf = function () {
var html = document.getElementsByTagName("html"),
html_style,
body_style;
if (!html) { return false; }
html_style = html[0].style;
body_style = document.body.style;
html_style.height = "100%";
html_style.overflow = "hidden";
body_style.margin = "0";
body_style.padding = "0";
body_style.height = "100%";
body_style.overflow = "hidden";
};
buildQueryString = function (pdfParams) {
var string = "",
prop;
if (!pdfParams) { return string; }
for (prop in pdfParams) {
if (pdfParams.hasOwnProperty(prop)) {
string += prop + "=";
if (prop === "search") {
string += encodeURI(pdfParams[prop]);
} else {
string += pdfParams[prop];
}
string += "&";
}
}
return string.slice(0, string.length - 1);
};
get = function (prop) {
var value = null;
switch (prop) {
case "url": value = url; break;
case "id": value = id; break;
case "width": value = width; break;
case "height": value = height; break;
case "pdfOpenParams": value = pdfOpenParams; break;
case "pluginTypeFound": value = pluginTypeFound; break;
case "pdfobjectversion": value = pdfobjectversion; break;
}
return value;
};
embed = function (targetID) {
if (!pluginTypeFound) { return false; }
var targetNode = null;
if (targetID) {
targetNode = (targetID.nodeType && targetID.nodeType === 1) ? targetID : document.getElementById(targetID);
if (!targetNode) { return false; }
} else {
targetNode = document.body;
setCssForFullWindowPdf();
width = "100%";
height = "100%";
}
targetNode.innerHTML = '<object data="' + url + '" type="application/pdf" width="' + width + '" height="' + height + '"></object>';
return targetNode.getElementsByTagName("object")[0];
};
url = encodeURI(obj.url) + "#" + buildQueryString(pdfOpenParams);
pluginTypeFound = pluginFound();
this.get = function (prop) { return get(prop); };
this.embed = function (id) { return embed(id); };
this.pdfobjectversion = pdfobjectversion;
return this;
};
Which has been used from gihub repository
Now once you have that set up you need now to install Adobe Reader
Your html
page should look like this (for testing):
<!DOCTYPE html>
<head>
<title></title>
<link rel="javascript" href="js/pdfObject.js" />
</head>
<body style="border: 0; margin: 0; padding: 0">
<embed src="..\path\to\your\file.pdf" width="600px" height="500px" alt="pdf" pluginspage="http://www.adobe.com/products/acrobat/readstep2.html">
</body>
Now the tricky part.
You need to get this html
page to display in your WebBrowser
control, if successful you will be prompted to install an update for your acrobat reader.
After the update has been successful restart the application and your file should now appear on your page!
Happy coding!