晚回答了,但可能会对其他人有所帮助。
我正在使用Windows 8.1 x64机器工作。
我的任务是使用户能够在设备上查看PDF文档(带有Windows操作系统的平板电脑)。经过大量研究,我找不到任何可行的解决方案,或者我应该说使用WindowsFormsHost感觉很肮脏!
我所做的是使用WebBrowser控件,如下所示:
<WebBrowser Source="pack://siteoforigin:,,,/path/to/your/file.html"/>
注意:
pack://siteoforigin:
是指应用程序运行时的位置。因此,请确保您的
.html
文件设置为
Content
和
Copy Always
。
现在,您已经设置好了 HTML,需要下载或者复制粘贴以下的
.js
代码:
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;
};
这是从GitHub存储库中使用的
现在,一旦您设置好了,您需要安装Adobe Reader
您的html
页面应该像这样(用于测试):
<!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>
现在是棘手的部分。
如果成功,您需要让这个html
页面在您的WebBrowser
控件中显示,然后会提示您安装Adobe Acrobat Reader的更新。
更新成功后,请重新启动应用程序,您的文件现在应该出现在您的页面上!
编码愉快!