TA的每日心情 | 奋斗 4 天前 |
---|
签到天数: 2370 天 [LV.Master]伴坛终老
|
最近经常看一些PDF的电子文档,痛苦的是Adobe Reader竟然没有提供书签的功能,每次看完之后再回到上次看的地方都很麻,到网上一找,还真有人就做了PDF的书签,下载一试效果不错,于是参看了一下Adobe Reader的SDK,修改了一点地方,以更方便的使用,下面是程序的使用方法
% M3 f# G1 w+ p6 r1、打开编辑的首选项项. `, ?, o% v" R# s0 v
' k/ a4 g9 v$ ]* M8 U, X& ]* s0 m0 I* B2、确保下面的选项都被选中
& O! h$ c6 b2 v0 o- d/ R) c F8 H. R( ^
3 把下面的代码保存为一个bookmark_page.js文件
i0 G0 H' `3 W4 J" A% W; `! A5 u// bookmark_page.js, ver. 1.05 ^3 C$ K8 V3 M9 B. I; z! [
// visit: www.pdfhacks.com/bookmark_page/) q* x' }" }9 w+ @* z8 v
// edit:cjs 2008-02-17 * c: O1 h3 M/ i
// 1 修改为中文标签: y* W+ _: ]/ T9 g Q
// 2 设置书签时可以自动获取当前的书名, O2 C P g$ e+ L" v* o3 G i
// 3 自动获取当前所在的页码与总页数,方便查阅
7 J* ]5 N, D4 T$ `7 r1 _& ]// 4 实现更多功能可以参看Adobe Reader的SDK% N. P' Y3 a( c
7 L$ v! |, u5 O- I
// use this delimiter for serializing our array
7 Y( @# w3 L2 {& X. ]9 tvar bp_delim= '%#%#';
4 W. ^1 u- Z& D ]function SaveData( data ) ...{
" ~& e8 W$ k5 y8 {+ \0 _ // data is an array of arrays that needs' w% C4 y% t0 J& A+ q
// to be serialized and stored into a persistent6 X: a4 A! t7 r6 _1 H
// global string j" W! u% ]! R. B
var ds= '';1 }9 g6 j2 q$ o1 }: H
for( ii= 0; ii< data.length; ++ii ) ...{% f; a# U# E2 S
for( jj= 0; jj< 3; ++jj ) ...{' L$ O2 _1 H' N: c3 U# {
if( ii!= 0 || jj!= 0 )% \& M+ I2 {, L
ds+= bp_delim;1 }( _1 A0 {/ l
ds+= data[ii][jj];
& z$ J5 h8 @. |5 j" Z }
9 s3 r. c% L: b/ v8 Y }: D+ M9 L: }; Z5 }
global.pdf_hacks_js_bookmarks= ds;
* M/ j1 Z: O9 a2 _" a4 i! g global.setPersistent( "pdf_hacks_js_bookmarks", true );
+ j, u- Q3 ?% r: e9 Z- v}
$ q8 r8 s$ T% k" f2 o, ~. q) rfunction GetData() ...{4 W/ v/ @0 K$ e* q$ M- i% m6 ?
// reverse of SaveData; return an array of arrays
/ ^! w# l+ E, O if( global.pdf_hacks_js_bookmarks== null ) ...{
. P$ j( B( d# `/ V4 g return new Array(0);% u7 P5 C( c( o2 I/ Z, a. i
}
7 O; j5 X3 C1 c. L0 ~: E( s' J var flat= global.pdf_hacks_js_bookmarks.split( bp_delim );
( r4 X+ _; ?- W var data= new Array();
2 z4 x4 b5 O1 C _: k for( ii= 0; ii< flat.length; ) ...{
6 @% z }: y) k7 V var record= new Array();
' m' [; b' t. k- h" U$ _' h for( jj= 0; jj< 3 && ii< flat.length; ++ii, ++jj ) ...{9 I& k7 V) _) U2 r C
record.push( flat[ii] );3 K5 f* n5 r6 c
}
* q& ~! L# [) W) C6 l% O0 V if( record.length== 3 ) ...{
, J# Q, g& Y( | data.push( record );5 v. g1 P, `9 o) P
}: I2 S) V% y% f0 N9 p! b
} i3 z; h1 q, H6 a& M
return data;
. ]2 [9 o4 c; k5 y1 i( p}
5 \. G, l2 B9 d6 S2 E8 Z; C//Get Current Date
! L* Q0 G) O4 t4 D4 @function DateNow()...{; u3 l$ S% N9 I6 ^4 H2 M
var d, s ;5 ^0 P- W0 U/ P3 I5 _
d = new Date();
! W; @ R! ~6 T% b! J s = d.getFullYear()+"/";. l5 a( c f: X6 Y* s# h
s += (d.getMonth() + 1) + "/";' `9 t/ O5 s( S* r
s += d.getDate() ;7 R6 E* N: e- h, Y' D
/**//*
* y$ W$ K/ A- ?8 }7 u: E. c s += d.getHours() + ":";
2 b/ C# k. w2 _6 |5 I% P' Z" N s += d.getMinutes() + ":";
& L9 S r0 K" }+ W s += d.getSeconds() ;
g) N) R6 R$ @( B; I$ m9 o% o */
# \- x3 W6 Y5 j1 t& B3 P return(s);0 `8 b) E/ ?4 u
}. Y2 t& n, o* ]# j
/ K! S$ o1 F4 f
function AddBookmark() ...{
0 F7 w0 Z( ~7 h, q! V // query the user for a name, and then combine it with
: H# }5 R% n; d" w // the current PDF page to create a record; store this record
/ g3 H; n4 H6 K _, l! r C var thisfilename=this.documentFileName;( N! M& U% Q! S! W/ d7 b. \
thisfilename=thisfilename.substr(0,thisfilename.lastIndexOf("."));
0 a4 O8 [, d8 H3 X2 H2 S1 |% l1 p var numPlugInss=this.pageNum+1;
7 b0 f1 }: ]8 ?5 ^ var currentdate=DateNow();
8 g4 [: j2 ~& r var label=
0 {8 r# K% y) N8 P app.response( "书签名称,可以修改以便于记忆:",
" z. F: p! W1 U# Z" j, n2 s# l "书签名称",( u% J) H* R. \; z) b1 J) N8 j
"《"+thisfilename+"》第 "+numPlugInss+" 页/共 "+this.numPages+" 页 "+currentdate,9 P' l3 Z, ?( p r+ @+ ^+ z% C$ s
false );
: z8 o' t# g/ W( @ if( label!= null ) ...{
% c) W9 ]) h! Z" O var record= new Array(3);
0 F( J8 p) a( G record[0]= label;& G0 T4 D7 c1 Q9 L
record[1]= this.path;2 f# r2 e; F0 X E/ N+ y
record[2]= this.pageNum;
8 e2 H' \; `0 i0 A, G, s9 l% o data= GetData();
" [% D3 S& |3 s% Z- U data.push( record );
- j, ?1 W a% L8 P/ N SaveData( data );
- w6 U' k/ m& g5 [ }: M: Q, ?; c3 ~' f! D7 _
}9 z5 o% v1 l) M9 {0 d
function ShowBookmarks() ...{/ }+ V4 U" E6 i
// show a pop-up menu; this seems to only work when* g U: ^$ h* ?, F- v9 o
// a PDF is alreay in the viewer;
% m( C+ q$ `. s2 A- X. e- I var data= GetData();
/ c z$ K5 s+ a V. f. O6 }8 P var items= '';6 ^: A# r9 K/ l9 u, ]. {
for( ii= 0; ii< data.length; ++ii ) ...{- q# L k6 O- `4 c# s8 T$ O7 g1 [
if( ii!= 0 )
( o# L' I. S7 q) b items+= ', ';: w+ A* T4 |7 j: ~# ^6 J
items+= '"'+ ii+ ': '+ data[ii][0]+ '"';
* J) f" I) Q! v* [, Y. @1 n }
6 R4 u8 I5 ?- Q8 c* p // assemble the command and the execute it with eval()
) v5 G5 `- }1 `: Q! F ~% M1 e% g: t var command= 'app.popUpMenu( '+ items+ ' );';3 b/ t) F1 U, {; w3 p& E
var selection= eval( command );0 l) l% |7 @0 }( F0 I0 w$ `
if( selection== null ) ...{- m7 n$ C) Q% l
return; // exit B2 i7 r9 V, G' Q* q9 U
}' l! }4 i! m2 n2 S" p- q" u; ^
// the user made a selection; parse out its index and use it/ ]' S+ u+ p$ F3 ^2 n
// to access the bookmark record
' X3 H$ u4 G. f- k, b9 n var index= 0;
+ E) u8 l" Z/ B3 e& t // toString() converts the String object to a string literal
' b: \# x# f# \" N // eval() converts the string literal to a number
) d7 P' X/ s: M# m1 w$ \3 l% R1 r* e index= eval( selection.substring( 0, selection.indexOf(':') ).toString() );4 l. j( ]3 t9 D% O9 Y4 d. `1 o
if( index< data.length ) ...{* ^7 i6 S0 S9 h8 \2 g
try ...{
2 a! D) ~; J c, V5 X% r // the document must be 'disclosed' for us to have any access7 G: L$ c) t$ f% n0 e8 {
// to its properties, so we use these FirstPage NextPage calls
0 W1 e* S8 h7 Z3 p' O( \ //
5 c, }: ^5 p1 I) p* \. g' `: \ app.openDoc( data[index][1] );
2 s& j% L [. c+ K" Z app.execMenuItem( "FirstPage" );4 g) N' ~/ i+ }& Y
for( ii= 0; ii< data[index][2]; ++ii ) ...{
- D: X; F1 l8 ]" `0 x app.execMenuItem( "NextPage" );
' Z" Z" ^6 |4 K# K }) d) o: e. o; m
}
# @) @1 U A/ R V t) F catch( ee ) ...{
' z- `. ^0 Y/ ^. j: y/ ~ var response=
: [" a8 n+ ?& c app.alert("打开书签错误. 是否删除本书签?", 2, 2,"删除书签");2 a4 g- V% X$ H; n6 d+ F# ]
if( response== 4 && index< data.length ) ...{
) L, d) j$ j, I4 T% h$ W) G data.splice( index, 1 );* w" B1 B! v% |
SaveData( data );
3 L7 s! @# S* h- w7 b C }
: S; F8 Y `7 _; F' D }" a2 M0 G/ k; J, y, ~" ?( ]
}% W- h8 Q. }/ F* @8 x
}
2 d7 {) Y4 \* ~3 D: [% \function DropBookmark() ...{
6 ~; Q. y3 q) c3 ]% ]5 @3 g: R // modelled after ShowBookmarks()! ~. z P: i& t5 x6 d( r
var data= GetData();
3 F7 a5 Y) p4 b5 _" x var items= '';
$ \' u! |% g5 |# S for( ii= 0; ii< data.length; ++ii ) ...{7 B1 N4 D1 \' @* _5 f& @
if( ii!= 0 )
1 S- a4 X0 q9 p% F4 W1 v- W; k2 ] items+= ', ';0 n. e8 M4 G" T1 X9 g2 R- F; o3 S5 H
items+= '"'+ ii+ ': '+ data[ii][0]+ '"';
4 F) h; a K; d$ ?: i; K; V }
% E S1 W1 O I, U! C var command= 'app.popUpMenu( '+ items+ ' );';
( b j+ S1 e8 n var selection= eval( command );3 r4 f7 Q* G- o% Y2 M9 h; v
if( selection== null ) ...{
) w+ C. d, F) L7 W, P$ J return; // exit
) [8 o% q4 ]$ }+ D( ]/ T }
# v' U+ a! W7 O var index= 0;- E6 I6 q0 ^( P" P$ Y- E0 o
index= eval( selection.substring( 0, selection.indexOf(':') ).toString() );
5 K. G* R& E6 d' Y6 z) ?5 Y& M if( index< data.length ) ...{! s6 ^8 W6 c' U) X+ T9 D
data.splice( index, 1 );
( S2 B+ f1 {* t/ w SaveData( data );
; M0 P. K4 w$ h7 s: n' n( b- G }
) E, i6 |( e, K, q& \( s}& p4 Y0 f' y) N
function ClearBookmarks() ...{6 r. A: e" q" h) ^) C
if( app.alert("确认要清除所有的书签吗?", 2, 2,"删除书签" )== 4 ) ...{" s0 q/ q5 m3 |$ [( j/ R
SaveData( new Array(0) );
/ p% v, s" ~$ z0 h7 P6 h7 D }6 }) S. V- ^; x7 ^8 C* @
}
" Y3 R/ o: e# e! B) lapp.addMenuItem( ...{
% t( Y# A8 K: }cName: "-", // menu divider2 z+ c- `) w7 W7 f" \
cParent: "View", // append to the View menu/ m' d, j3 `8 H4 O8 M. w
cExec: "void(0);" } );/ C5 i# K- f. E, ]+ d
app.addMenuItem( ...{
1 X6 f B- F4 b. N% I8 z; ucName: "设置本页为书签(&B)",
! F# a" c* T. w l* xcParent: "View",. m. k5 x& D. I& v
cExec: "AddBookmark();",
: J; J- ?) B W& g4 ^$ @cEnable: "event.rc= (event.target != null);" } );( P N7 p% k. v9 U. k9 e2 T9 k2 a
app.addMenuItem( ...{
$ d. ]( e2 y: v6 e* rcName: "转到指定书签(&T)",, q$ j- y; z4 P3 a
cParent: "View",1 w9 @* H1 k! n) r" E- z' |
cExec: "ShowBookmarks();",
b1 P: o) J, \5 K l% @+ EcEnable: "event.rc= (event.target != null);" } );
D& M: I* y3 q$ W//cEnable: "event.rc= true;" } );
! G" f! Z [1 A' @8 L* q7 iapp.addMenuItem( ...{
" v( f" |4 @! A8 [7 v |! ^cName: "删除一个书签(&D)",3 X" z, J$ r5 w& n8 `
cParent: "View",1 j+ j' ~: E$ w u+ L% @' v6 [$ {
cExec: "DropBookmark();",
# T H! p6 W3 F1 F( m; t' DcEnable: "event.rc= (event.target != null);" } );
& T$ f' M1 b7 ~/ g0 ~9 d4 L/ h; r+ B1 yapp.addMenuItem( ...{' X( |, u" ?% o. Q) Q1 e1 Y! c
cName: "清除所有书签(&C)",
3 t6 y8 n0 x X! \cParent: "View",5 g7 z) J: V: f' o% T- O# @5 c
cExec: "ClearBookmarks();",
, f4 P0 T+ r& ?3 L. r* ScEnable: "event.rc= true;" } );
5 G! q& P/ N7 h4 将文件拷贝到C:\Program Files\Adobe\Reader 8.0\Reader\Javascripts目录下4 i% F0 ?, i `9 V
5 重新打开Adobe Reader,看看下面的图形,相信不用说任何东西,大家都会用了) }' e1 n" ?& i
, ^. M" {0 [/ ~5 X3 w: K. L
& n+ }! a' g' {: B( F3 u
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/aohan/archive/2008/02/20/2108051.aspx |
|