var PdfReader = function(options) {
this.version = "4.1.0.0";
this.domain = window.location.host;
this.pdf = null;
this.plugin = true;
this._pageNum = options.pageNum;
this._totalPageNum = options.totalPageNum;
this._readLink = options.readLink;
this._pageWidth = typeof (options.pageWidth) != "undefined" ? options.pageWidth
: null;
this._pageHeight = typeof (options.pageHeight) != "undefined" ? options.pageHeight
: null;
this._sessionId = typeof (options.sessionId) != "undefined" ? options.sessionId
: null;
this._rcUrl = "/pages/screen/web/readers/js/pdf/SC_TC_JP_KR.CAB";
this._patchPath = "/pages/screen/web/readers/js/pdf/PDFView.zip";
this.setOptions(typeof (options.toolbar) != "undefined" ? options.toolbar
: {});
};
PdfReader.prototype = {
setOptions : function(toolbar) {
this.toolbar = {
viewType : typeof (toolbar.viewType) != "undefined" ? toolbar.viewType
: 2, // 2:单页,5:双页
fitType : typeof (toolbar.fitType) != "undefined" ? toolbar.fitType
: "width", // width:适宽,height:适高
pageTurning : typeof (toolbar.pageTurning) != "undefined" ? toolbar.pageTurning
: 2, // 2:上下滚动,3:左右滑动
readPattern : typeof (toolbar.readPattern) != "undefined" ? toolbar.readPattern
: 0, // 0:阅读,1:选择,2:截图
openZoom : typeof (toolbar.openZoom) != "undefined" ? toolbar.openZoom
: true, // true:打开,false:关闭
openRotate : typeof (toolbar.openRotate) != "undefined" ? toolbar.openRotate
: true, // true:打开,false:关闭
openNote : typeof (toolbar.openNote) != "undefined" ? toolbar.openNote
: true, // true:打开,false:关闭
uploadLink : typeof (toolbar.uploadLink) != "undefined" ? toolbar.uploadLink
: null
// 注释上传附件地址
};
},
initPdfReader : function() {
this.plugin = checkPlugin();
if (!this.plugin) {
this.buildInPdf();
} else {
this.ycanPdf();
this.ycanConfig();
}
},
checkVersion : function(curVersion, newVersion) {
var newVer = newVersion.split('.');
var curVer = curVersion.split('.');
if (parseInt(newVer[0]) > parseInt(curVer[0])) {
return true;
}
if (parseInt(newVer[0]) == parseInt(curVer[0])
&& parseInt(newVer[1]) > parseInt(curVer[1])) {
return true;
}
if (parseInt(newVer[0]) == parseInt(curVer[0])
&& parseInt(newVer[1]) == parseInt(curVer[1])
&& parseInt(newVer[2]) > parseInt(curVer[2])) {
return true;
}
if (parseInt(newVer[0]) == parseInt(curVer[0])
&& parseInt(newVer[1]) == parseInt(curVer[1])
&& parseInt(newVer[2]) == parseInt(curVer[2])
&& parseInt(newVer[3]) > parseInt(curVer[3])) {
return true;
}
return false;
},
buildInPdf : function() {
var str = "
";
str += "
";
str += "
";
str += "";
str += "
";
str += "
";
$("#pdfContentLoader").html(str);
PDFJS.workerSrc = '/pages/screen/web/readers/js/pdf/pdf.worker.min.js';
//var h = $(window).height() - 155;
//$("#pdfContentLoader").height(h);
var link = "http://" + this.domain + this._readLink + "&pageNum=" + this._pageNum;
PDFJS.getDocument(link).then(function getPdfPage(pdf) {
pdf.getPage(1).then(function getPdfPage(page) {
var scale = 1;
var viewport = page.getViewport(scale);
scale = $("#pdfContentLoader").width()/viewport.width;
viewport = page.getViewport(scale);
var canvas = document.getElementById('the-canvas');
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
$("#controlContainer").css("overflow","scroll");
$("#controlContainer").scrollTop(0);
});
});
/*
var agent = navigator.userAgent.toLowerCase();
var html = "';
$("#pdfContentLoader").html(html);
*/
},
ycanPdf : function() {
var tips = null;
if (window.ActiveXObject || 'ActiveXObject' in window) {
try {
var obj = new ActiveXObject('PDFVIEWYC.PDFViewCtrl.1');
this.pdf = document.getElementById('YCanPDFIE');
} catch (e) {
tips = '您尚未安装PDF控件,点这里进行安装...';
}
} else { // 非IE
var obj = navigator.plugins['npPDFViewyc dll'];
if (obj == undefined) {
tips = '您尚未安装PDF控件,点这里进行安装...';
} else {
this.pdf = document.getElementById('YCanPDFNOTIE');
}
}
if (tips == null) {
var curVersion = this.pdf.GetVersion();
//if (!this.checkVersion(curVersion, this.version))
//return;
// if (this.checkVersion(curVersion, this.version)) {
// tips = '发现新版本阅读控件,点这里进行更新安装...';
// } else { // 用户当前使用版本大于等于新版最低版本,按照新版本方式更新
// var curVer = this.pdf.GetVersionUpdate(this.version, "http://"
// + this.domain + this._patchPath, 0);
// if (curVer == "程序默认升级失败") {
// tips = '程序默认升级失败,请关闭正在使用的页面,手动升级程序!';
// }
// }
}
if (tips != null) {
$("#controlContainer")
.html(
''
+ tips + '');
if (this.pdf != null) {
this.pdf.CloseFile();
this.pdf = null;
}
}
},
ycanConfig : function() {
this.pdf.HttpSetVualue("JSESSIONID", this._sessionId, 0);
this.pdf.SetRCURL("http://" + this.domain + this._rcUrl, 0);
this.pdf.BeginBook();
for (var i = 1; i < this._totalPageNum; i++) {
this.pdf.AddPage("http://" + this.domain + this._readLink
+ "&pageNum=" + i);
}
this.pdf.EndBook();
this.pdf.PageIndex = this._pageNum;
if (this.toolbar.openNote && this.toolbar.uploadLink != null) {
this.pdf.SetUpLoadPath("http://" + this.domain + this.toolbar.uploadLink, 5*1024*1024, 0);
}
this.pdf.SetViewType(this.toolbar.viewType);
if (!"width" == this.toolbar.fitType) {
this.pdf.FitHeight();
} else {
this.pdf.FitWidth();
}
},
getNoteRegionsSingle : function (commentIndex) {
return this.pdf.getNoteRegions(commentIndex,"");
}
};
var checkPlugin = function() {
var agent = navigator.userAgent.toLowerCase();
if (agent.indexOf("edge") > 0 || agent.indexOf("mobile") > 0
|| agent.indexOf("pad") > 0 || agent.indexOf("mac") > 0) {
return false;
} else if (agent.indexOf("chrome") > 0) {
if (agent.indexOf("linux") > 0) {
return false;
} else {
var regStr_chrome = /chrome\/[\d.]+/gi;
var v = (agent.match(regStr_chrome) + "").replace(/[^0-9.]/ig,
"");
if (Number(v.substring(0, v.indexOf("."))) > 42) {
return false;
}
}
}
return true;
};