利用pdf.js开发嵌入pdf显示,以及利用jquery-ui左右分栏显示

原来考虑用pdf.js的viewer.html页面,但怎么用都不方便。因此直接用pdf.js在左侧连续显示pdf所有内容,右侧显示其它相关内容,并且左右宽度可以任意拖动,最终实现效果如图:

代码:
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>  
    <link rel="prefetch" href="/pdfview/test.pdf">
    <style>
        #pdfView > canvas {
            width: 100%;
        }
        .ui-resizable-handle {
            background-color: #699083;
            width: 4px !important;
            right: 0px !important;
        }
        .ui-resizable-e { 
            width: 4px !important;
            right: 0px !important;
        }
    </style>
</head>
<body style="background-color: #ffffff">
           <div style="width: 100%; height: 100%;" id="mainDiv">
            <div style="width: 50%; float: left; height: 100%;" id="resizeBoxLeft">                
                <div id="pdfView" style="width: 100%; height: 100%; overflow: auto; background-color: #ffffff; -webkit-overflow-scrolling: touch;"></div>
            </div>
            <div id="resizeBoxRight" style="width: 50%; float: right; height: 100%; background-color: #ffffff; -webkit-overflow-scrolling: touch;">
               <!---其它内容--->
            </div>
        </div>
    </form>
    <script type="text/javascript" src="/js/jquery/jquery-1.10.2.js"></script>
    <script src="/js/jquery/jquery-ui.js"></script>
    <link rel="stylesheet" href="/js/jquery/jquery-ui.css">
    <script src="build/pdf.js"></script>
    <script type="text/javascript">

        $(function ()
        {
            var url = '../pdfview/test.pdf';
            PDFJS.workerSrc = 'build/pdf.worker.js';
            var scale = 1.5;       
            //逐页创建canvas显示pdf内容     
            PDFJS.getDocument(url).then(function (pdf)
            {                 
                var pdfPageCount = pdf.numPages;
                var getPageAndRender = function (pageNumber)
                {
                    pdf.getPage(pageNumber).then(function (page)
                    {
                        var viewport = page.getViewport(scale);
                        var canvas = document.createElement('canvas');
                        canvas.height = viewport.height;
                        canvas.width = viewport.width;
                        var renderContext = {
                            canvasContext: canvas.getContext('2d'),
                            viewport: viewport
                        };
                        page.render(renderContext);
                        $("#pdfView").append(canvas);                      
                    });
                    if (pageNumber < pdfPageCount )
                    {
                        pageNumber++;
                        getPageAndRender(pageNumber);
                    }
                }
                getPageAndRender(1);

            });


            $("#resizeBoxLeft").resizable({               
                handles: 'e',               
                start: function() {
                     $("#resizeBoxRight").hide();//拖动时会影响拖动条向右拖动,所以拖动时隐藏,停止拖动再显示。(暂时没想到其它办法)
                },
                stop: function (event, ui)
                {
                          //计算左右两栏宽度 百分比
                    var rightWidth = $(document).width() - ui.size.width;
                    $("#resizeBoxLeft").css({ "width": parseInt((ui.size.width / $(document).width()) * 100) + "%", "height": "100%" });
                    $("#resizeBoxRight").css({ "width": parseInt((rightWidth / $(document).width()) * 100) + "%", "height": "100%" });
                    $("#resizeBoxRight").show();
                }
            });
        });
    </script>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackieZhengChina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值