键盘人生

After all,tomorrow is another day

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
每次看完pdf,下次打开还得从头翻,想着pdf为啥不支持书签呢。遂google。还真有。

代码如下。

编辑成bookmark_page.js 文件,放入到安装目录的“C:\Program Files (x86)\Adobe\Reader 9.0\Reader\Javascripts”下。

 

View Code 
  1 // bookmark_page.js, ver. 1.0
  2 // visit: www.pdfhacks.com/bookmark_page/
  3 // edit:cjs 2008-02-17 
  4 // 1 修改为中文标签
  5 // 2 设置书签时可以自动获取当前的书名
  6 // 3 自动获取当前所在的页码与总页数,方便查阅
  7 // 4 实现更多功能可以参看Adobe Reader的SDK
  8 // use this delimiter for serializing our array
  9 var bp_delim= '%#%#';
 10 function SaveData( data ) {
 11   // data is an array of arrays that needs
 12   // to be serialized and stored into a persistent
 13   // global string
 14   var ds= '';
 15   for( ii= 0; ii< data.length; ++ii ) {
 16     for( jj= 0; jj< 3++jj ) {
 17       if( ii!= 0 || jj!= 0 )
 18         ds+= bp_delim;
 19       ds+= data[ii][jj];
 20     }
 21   }
 22   global.pdf_hacks_js_bookmarks= ds;
 23   global.setPersistent( "pdf_hacks_js_bookmarks"true );
 24 }
 25 function GetData() {
 26   // reverse of SaveData; return an array of arrays
 27   if( global.pdf_hacks_js_bookmarks== null ) {
 28     return new Array(0);
 29   }
 30   var flat= global.pdf_hacks_js_bookmarks.split( bp_delim );
 31   var data= new Array();
 32   for( ii= 0; ii< flat.length; ) {
 33     var record= new Array();
 34     for( jj= 0; jj< 3 && ii< flat.length; ++ii, ++jj ) {
 35       record.push( flat[ii] );
 36     }
 37     if( record.length== 3 ) {
 38       data.push( record );
 39     }
 40   }
 41   return data;
 42 }
 43 //Get Current Date
 44 function DateNow(){
 45    var d, s ;
 46    d = new Date();
 47    s = d.getFullYear()+"/";
 48    s += (d.getMonth() + 1+ "/";
 49    s += d.getDate() ;
 50    /*
 51    s += d.getHours() + ":";
 52    s += d.getMinutes() + ":";
 53    s += d.getSeconds() ;
 54    */
 55    return(s);
 56 }
 57 function AddBookmark() {
 58   // query the user for a name, and then combine it with
 59   // the current PDF page to create a record; store this record
 60   var thisfilename=this.documentFileName;
 61   thisfilename=thisfilename.substr(0,thisfilename.lastIndexOf("."));
 62   var numPlugInss=this.pageNum+1;
 63   var currentdate=DateNow();
 64   var label= 
 65     app.response( "书签名称,可以修改以便于记忆:",
 66                   "书签名称",
 67                   ""+thisfilename+"》第 "+numPlugInss+" 页/共 "+this.numPages+" 页  "+currentdate,
 68                   false );
 69   if( label!= null ) {
 70     var record= new Array(3);
 71     record[0]= label;
 72     record[1]= this.path;
 73     record[2]= this.pageNum;
 74     data= GetData();
 75     data.push( record );
 76     SaveData( data );
 77   }
 78 }
 79 function ShowBookmarks() {
 80   // show a pop-up menu; this seems to only work when
 81   // a PDF is alreay in the viewer;
 82   var data= GetData();
 83   var items= '';
 84   for( ii= 0; ii< data.length; ++ii ) {
 85     if( ii!= 0 )
 86       items+= '';
 87     items+= '"'+ ii+ ''+ data[ii][0]+ '"';
 88   }
 89   // assemble the command and the execute it with eval()
 90   var command= 'app.popUpMenu( '+ items+ ' );';
 91   var selection= eval( command );
 92   if( selection== null ) {
 93     return// exit
 94   }
 95   // the user made a selection; parse out its index and use it
 96   // to access the bookmark record
 97   var index= 0;
 98   // toString() converts the String object to a string literal
 99   // eval() converts the string literal to a number
100   index= eval( selection.substring( 0, selection.indexOf(':') ).toString() );
101   if( index< data.length ) {
102     try {
103       // the document must be 'disclosed' for us to have any access
104       // to its properties, so we use these FirstPage NextPage calls
105       //
106       app.openDoc( data[index][1] );
107       //      app.execMenuItem( "FirstPage" );
108       //      for( ii= 0; ii< data[index][2]; ++ii ) {
109       //        app.execMenuItem( "NextPage" );
110       //      }
111       this.pageNum = data[index][2];
112     }
113     catch( ee ) {
114       var response= 
115         app.alert("打开书签错误. 是否删除本书签?"22,"删除书签");
116       if( response== 4 && index< data.length ) {
117         data.splice( index, 1 );
118         SaveData( data );
119       }
120     }
121   }
122 }
123 function DropBookmark() {
124   // modelled after ShowBookmarks()
125   var data= GetData();
126   var items= '';
127   for( ii= 0; ii< data.length; ++ii ) {
128     if( ii!= 0 )
129       items+= '';
130     items+= '"'+ ii+ ''+ data[ii][0]+ '"';
131   }
132   var command= 'app.popUpMenu( '+ items+ ' );';
133   var selection= eval( command );
134   if( selection== null ) {
135     return// exit
136   }
137   var index= 0;
138   index= eval( selection.substring( 0, selection.indexOf(':') ).toString() );
139   if( index< data.length ) {
140     data.splice( index, 1 );
141     SaveData( data );
142   }
143 }
144 function ClearBookmarks() {
145   if( app.alert("确认要清除所有的书签吗?"22,"删除书签" )== 4 ) {
146     SaveData( new Array(0) );
147   }
148 }
149 app.addMenuItem( {
150 cName: "-",              // menu divider
151 cParent: "View",         // append to the View menu
152 cExec: "void(0);" } );
153 app.addMenuItem( {
154 cName: "设置本页为书签(&B)",
155 cParent: "View",
156 cExec: "AddBookmark();",
157 cEnable: "event.rc= (event.target != null);" } );
158 app.addMenuItem( {
159 cName: "转到指定书签(&T)",
160 cParent: "View",
161 cExec: "ShowBookmarks();",
162 cEnable: "event.rc= (event.target != null);" } );
163 //cEnable: "event.rc= true;" } );
164 app.addMenuItem( {
165 cName: "删除一个书签(&D)",
166 cParent: "View",
167 cExec: "DropBookmark();",
168 cEnable: "event.rc= (event.target != null);" } );
169 app.addMenuItem( {
170 cName: "清除所有书签(&C)",
171 cParent: "View",
172 cExec: "ClearBookmarks();",
173 cEnable: "event.rc= true;"

posted on 2011-07-05 09:32  Dr.Wang  阅读(371)  评论(0编辑  收藏  举报