在特定页面反复打开PDF.(甚至在滚动之后)

Repeatedly open a PDF at a specific page. (even after a scroll)

本文关键字:滚动 之后 PDF      更新时间:2023-09-26

一周前我问了一个类似的问题,但可能我没有把这个问题说清楚。

从那以后,我一直在努力寻找有效的方法,但没有成功。

使用所有常见浏览器的最新版本,我想显示在特定页面上打开的PDF。


使用Chrome-做我想做的事:-)

按下按钮可打开第3页的PDF。如果用户将pdf滚动到另一页,然后再次按下按钮,pdf将在第3页再次打开;

使用FF、IE或Safari

按下按钮可打开第3页的PDF。如果用户将pdf滚动到另一个页面,然后重新按下按钮,则不会发生任何事情。


有人知道我如何用所有最新的浏览器在第3页打开页面吗?我非常乐意完全改变我的方法,包括在必要时使用免费插件。

如果有人知道我想做的事情只能在Chrome中完成,那么如果他们能告诉我,我将不胜感激。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Test for SO</title>
  <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
</head>
<body>
  <input id="test" value="Test" type="button" />
    <br />
  <iframe id="iFrame" width="500" height="600"></iframe>
</body>
</html>
<script type="text/javascript">
  $(function () {
    $("#test").click(function () {
      var fileName = "/PDFFiles/pages.pdf#page=3";
      $('#iFrame').attr("src", fileName);
    });
  });
</script>

这适用于Chrome

<A HREF="http://www.example.com/myfile.pdf#page=4">

若要在所有浏览器中运行此操作,请使用PDF has to be coded to open to a specific page.这可以使用Adobe Acrobat来完成。

尝试使用谷歌的文档查看器,如下所示:

<iframe src="http://docs.google.com/gview?url=http://infolab.stanford.edu/pub/papers/google.pdf&embedded=true" style="width:100%; height:500px;" onload="javascript:this.contentWindow.location.hash='#:0.page.4';" frameborder="0"></iframe>

关键部分是onload函数,它添加基于零的索引页码以显示:

onload="javascript:this.contentWindow.location.hash='#:0.page.4';"

如果你需要一个按钮来重新加载iframe,只需更新iframe-src(我认为这是最简单的方法,但我可能错了):

$('button').click(function(){
  $('iframe').attr("src", function() { 
    return this.src;
  });
});

我的建议是将iframe-src设置为其他内容,然后返回到正确的文档。仅在短时间内。

您需要销毁并重新创建iframe节点本身。例如:

<input id="test" value="Test" type="button" />
<br />
<div id="wrapper"> 
 <!-- iframe will be continually destroyed and re-created here --> 
</div>

<script type="text/javascript">
    // Create the initial iframe on page load
    $('<iframe id="iFrame" width="500" height="600" src="/PDFFiles/pages.pdf">').appendTo('#wrapper');
    // Destroy and recreate iframe on button click, starting on your desired page.
    $("#test").click(function () {
     if($('#iFrame').length) { 
       $('#iFrame').remove(); 
     }
     var page = 3;
     $('<iframe id="iFrame" width="500" height="600" src="/PDFFiles/pages.pdf#page='+page+'">').appendTo('#wrapper');
    });
</script>​​​​​​​

注意:PDF会在第一个页面上再次加载,但您可以将其隐藏在ajax加载程序球下1-2秒,然后当您将其显示出来时,它将跳到您想要的页面。