TA的每日心情 | 擦汗 1 小时前 |
---|
签到天数: 2367 天 [LV.Master]伴坛终老
|
最近经常看一些PDF的电子文档,痛苦的是Adobe Reader竟然没有提供书签的功能,每次看完之后再回到上次看的地方都很麻,到网上一找,还真有人就做了PDF的书签,下载一试效果不错,于是参看了一下Adobe Reader的SDK,修改了一点地方,以更方便的使用,下面是程序的使用方法
4 l% a; H' I" H- J; ~, X2 D1、打开编辑的首选项项
, F3 C) v0 S" S/ T; |3 ] p( R5 D$ R" ~
2、确保下面的选项都被选中
: c* I- a0 N7 N- y G
$ t+ d3 V4 u+ O# d2 L4 c3 把下面的代码保存为一个bookmark_page.js文件2 j8 ?9 R6 n( V8 r
// bookmark_page.js, ver. 1.0
" k' ^5 }( n1 o1 n. g+ X8 p& L }// visit: www.pdfhacks.com/bookmark_page/
+ g4 a; M8 Q3 |- l9 |2 ^// edit:cjs 2008-02-17
2 M( D8 z* B3 p, |% H# i3 D// 1 修改为中文标签. }9 ~. `/ w4 k# N9 E$ n7 B
// 2 设置书签时可以自动获取当前的书名
- ]+ i/ h) d, F// 3 自动获取当前所在的页码与总页数,方便查阅
r( t Y" K% s& A5 [// 4 实现更多功能可以参看Adobe Reader的SDK6 C2 N) A: d: a0 z
. I, w8 ~' W* J' ]" V0 V4 x' |) d7 r
// use this delimiter for serializing our array2 Q" z" T5 n% K9 @
var bp_delim= '%#%#';
- A4 ]& B3 s: q* }/ C1 ufunction SaveData( data ) ...{
, f U. F5 a! ~; a6 h; x // data is an array of arrays that needs
- b, w# }. Z7 n+ y // to be serialized and stored into a persistent5 |$ @' w/ i. o4 _) E! n j
// global string
& H0 K% }6 c3 e. ?0 v var ds= '';
6 B n0 F) N# f& ]" K3 U for( ii= 0; ii< data.length; ++ii ) ...{
6 b8 {' L% W L$ c6 y; o' w for( jj= 0; jj< 3; ++jj ) ...{
: G* o" M& v, H" e* Z# R if( ii!= 0 || jj!= 0 ). L% b: c. n- o# t3 f5 {. L0 Y
ds+= bp_delim;; @+ a$ T+ ]6 {, Q0 c) V
ds+= data[ii][jj];
! _3 k( z1 f' D0 ~( W- F }
' b& A" j; t; R7 E( C# v# C1 B. h }" C* ]/ V7 F$ r8 e4 f+ f( ~/ z
global.pdf_hacks_js_bookmarks= ds;
2 G( P. p: c; j8 s9 A/ ^ global.setPersistent( "pdf_hacks_js_bookmarks", true );
( Q D1 g; ?1 u `! d' C; P}
+ v! ^, g% Z4 V. y8 N! ^function GetData() ...{
5 v4 n) }! x/ {8 ]; n // reverse of SaveData; return an array of arrays
4 C' I1 l6 e4 U6 W0 U. i# o+ V if( global.pdf_hacks_js_bookmarks== null ) ...{
E* s- w: _% D. y/ y4 _2 d return new Array(0);/ r, K( X# U* P6 [, r% K0 J8 f( @
}* @8 v8 a% `% Y& q$ i1 _5 ?
var flat= global.pdf_hacks_js_bookmarks.split( bp_delim );
4 l! C. S V# p1 Y m$ @. e6 `6 Y var data= new Array();. W6 t, x6 ]! F' z( h
for( ii= 0; ii< flat.length; ) ...{+ w7 e! x* L3 V) h( X( m: j/ s
var record= new Array();
, m4 O' x* Y- T) X9 Z for( jj= 0; jj< 3 && ii< flat.length; ++ii, ++jj ) ...{3 r' ]. z9 e6 w1 s9 D+ X; ^. R
record.push( flat[ii] );
% } z6 ?0 y* \" E: R) ? }
T8 M) F( Q( {8 p if( record.length== 3 ) ...{
' z0 B1 \ e( Z5 m data.push( record );
9 r/ I; [& j2 m }2 K( b# p+ p. h, ~; p
} S9 m& E+ x+ `9 C
return data;& Q- U9 m9 ~. ^- M# Z, Q
}
; D* g$ f a$ |3 F) ^( L3 N+ H( z0 z4 m//Get Current Date, U( e% |6 e6 d
function DateNow()...{
) X g6 s8 @) H: p* W! i) B var d, s ;
- ^9 f0 {" N* {) v d = new Date();0 M- P+ P; D! a
s = d.getFullYear()+"/";
8 c7 ?7 Y2 K2 ~' | s += (d.getMonth() + 1) + "/";3 y% J- G( }% h( @. a
s += d.getDate() ;
/ u$ o; ], R7 C; m( l) h: F /**//*
( {/ c8 H6 ^) C% m2 }' N s += d.getHours() + ":"; T. K4 z8 O5 I: \: ~, ?
s += d.getMinutes() + ":";; l: {. L3 t+ U O* Y% Q- v
s += d.getSeconds() ;* v; L4 ~0 z" e1 [5 a3 U$ l1 j
*/0 t& Y$ G6 e" V8 j: p
return(s);
, H; X( O0 q0 F1 K2 u) G}
6 [: u% h. m; v4 W( a9 E4 @! q# ?' ?4 x r
function AddBookmark() ...{
, L# m* O2 d9 V" f7 _5 S; ? // query the user for a name, and then combine it with, H$ W4 S6 T& O% w8 d
// the current PDF page to create a record; store this record
0 o. [) z" @( L# ]) I var thisfilename=this.documentFileName;6 @1 M( e8 r* [2 d
thisfilename=thisfilename.substr(0,thisfilename.lastIndexOf("."));
5 b2 t& y7 M: [0 M% G P var numPlugInss=this.pageNum+1;
( R5 v& ^9 g0 [/ S7 ~$ }9 r var currentdate=DateNow();
?& [, A- b B8 i+ K4 j. i1 ~ var label=
* D+ n/ o# y A; f# W, E. }; N7 z app.response( "书签名称,可以修改以便于记忆:",5 S7 w" ?+ I! ^( F3 j& ~: ]5 U
"书签名称",6 t+ ?" @% `- w. ]+ R
"《"+thisfilename+"》第 "+numPlugInss+" 页/共 "+this.numPages+" 页 "+currentdate,: w1 R3 l6 ^4 `
false );
+ E/ N! \1 a: n) O+ H$ q if( label!= null ) ...{
5 X0 {# m5 m* h; ` var record= new Array(3);) ]4 B! @# N! f" G: a
record[0]= label;# Q/ k) m4 }3 T! \* f
record[1]= this.path;
9 D0 p& O, d' C f record[2]= this.pageNum;
! }4 K& R+ W+ m0 t2 g) ~ data= GetData();
- P0 j% Y! k' y( |: T& C data.push( record );9 e P% B5 P" k! ^0 `$ c$ W4 v
SaveData( data );; q' i) \5 J: V3 A- G, v
}% E7 j/ T( o; T) Z+ I+ \: B5 B6 I
}
# P& r! H$ T0 {# z0 _! a9 {. Mfunction ShowBookmarks() ...{
5 V+ K1 Q F3 [# M* A9 d$ |7 b // show a pop-up menu; this seems to only work when0 z4 G- z- ]& I" ~2 H
// a PDF is alreay in the viewer;
8 h# R& i) A& t" g; W P9 W var data= GetData();% M& Y4 l2 ? z: I, k# p
var items= '';
, Z; G$ c$ E3 b$ w for( ii= 0; ii< data.length; ++ii ) ...{
, |9 H8 k, b& Y# ?) d1 m2 l if( ii!= 0 )
, h! |9 p. l8 h% u$ } items+= ', ';0 M! d6 n. H. F0 f
items+= '"'+ ii+ ': '+ data[ii][0]+ '"';
" g+ C) ] n5 X* n }
! c8 ]* k- N! E0 _ // assemble the command and the execute it with eval()5 m) M/ d _+ j3 x; F# X3 z+ U
var command= 'app.popUpMenu( '+ items+ ' );';- K- _ v, H0 ~
var selection= eval( command );& R! o; V$ K' W6 _' J; J
if( selection== null ) ...{
) u; z1 N9 w/ \; X" Y8 d return; // exit
P& \, N) m' m$ g! `" [ }4 p" J6 A7 T& \9 t8 e9 N% n- X
// the user made a selection; parse out its index and use it
) Y* y4 |3 q+ W // to access the bookmark record/ I; W- b% _& Y
var index= 0;
" B$ x6 y1 F! N7 ^+ Q, R- P // toString() converts the String object to a string literal; K7 B2 j7 X. J5 y r% D
// eval() converts the string literal to a number
7 L' Q) C9 r' g a' E+ y index= eval( selection.substring( 0, selection.indexOf(':') ).toString() );* y/ e/ @+ o+ D, j
if( index< data.length ) ...{3 X$ \( Y: E; _4 n! v. @
try ...{8 B) N+ n. ?0 B. G* w/ y
// the document must be 'disclosed' for us to have any access
* ]' ~/ L- V* o. q6 k% o4 I( v/ n7 m // to its properties, so we use these FirstPage NextPage calls% Z1 E, S) k$ {+ b5 V
//
( Z) O {$ \$ Q) Q L7 C) \+ v7 Z app.openDoc( data[index][1] );( S. d) ?2 m# K. z2 x
app.execMenuItem( "FirstPage" );
( @$ K' f* q" V H for( ii= 0; ii< data[index][2]; ++ii ) ...{
: i# F: i) X. L* @( _ Z' j/ w app.execMenuItem( "NextPage" );
: w( Z, G: s d9 @& w. C( \" E6 ` }! j; @6 g$ J. _) y
}
8 Z! `9 M( ^$ r, I catch( ee ) ...{! Y6 i; j2 y$ w! t4 m
var response=
$ Z8 _4 F y/ C) ]! Z( X- v app.alert("打开书签错误. 是否删除本书签?", 2, 2,"删除书签");6 }2 |2 v a5 O; D; f
if( response== 4 && index< data.length ) ...{
' }+ ~& p8 T8 Y) T" |( N data.splice( index, 1 );* b+ z* u" d: O# @# X# y* T% m9 @
SaveData( data );" K' `# f. x* ?
}: \4 A% i$ s4 b1 [4 l
}8 ]/ O4 d# a9 J, I
}
" c9 G+ V( c/ m/ X! Z}
2 y! n3 N! y0 A% Jfunction DropBookmark() ...{
b0 c+ H2 O) F& b- V // modelled after ShowBookmarks()! w! ^) c V( k, h
var data= GetData();
& z& m0 x! W/ Z: t2 O0 x var items= '';
8 M/ T% k+ Q8 d5 L' [: Z for( ii= 0; ii< data.length; ++ii ) ...{
Q( f; {5 r' @* M5 f if( ii!= 0 )" Y$ c+ a- q" c0 D: a2 @
items+= ', ';
& {! g5 F" _9 R items+= '"'+ ii+ ': '+ data[ii][0]+ '"';
7 k- ]' T9 b, P; [9 w }* [$ \1 Z# d! |3 I8 \8 i' E* m3 |6 `
var command= 'app.popUpMenu( '+ items+ ' );';
& Y7 r9 l. ~- q var selection= eval( command );0 _9 P* S% T. @( r# T$ e. O1 Q- X
if( selection== null ) ...{
+ N. B8 G' O$ E" W, l* Q return; // exit0 d, Q0 Z3 E0 i) ~5 p/ l
}
1 Z1 O6 j- W8 [ var index= 0;9 K( Z+ o$ A( P. z
index= eval( selection.substring( 0, selection.indexOf(':') ).toString() );
) Q& U: Z" `" k; \3 z/ P/ ~, o# l if( index< data.length ) ...{
( x) y: a+ S; k3 Q3 B6 ~ data.splice( index, 1 );
?6 g5 j' T7 ?1 f SaveData( data );& O6 q( p) _2 ~. f3 L
}+ X3 ]4 ^9 w0 G! d# L
}: _- t$ Q0 }" p( L; t
function ClearBookmarks() ...{
. C U! t Z) s0 u1 F if( app.alert("确认要清除所有的书签吗?", 2, 2,"删除书签" )== 4 ) ...{
. ?& m, h; @' _' z9 N7 s: D3 c. M! P5 c SaveData( new Array(0) );
M2 ^4 y$ q( Y0 N' f& o }" _1 _" g4 u) D* O1 s4 N: f
}: y# g9 S% p: p, T8 Q) i
app.addMenuItem( ...{
0 s9 d# G2 F$ N0 I4 gcName: "-", // menu divider
' ~" {/ ^. t. XcParent: "View", // append to the View menu
. n1 d/ I/ `" AcExec: "void(0);" } );1 |$ N- E- M- G8 s7 q
app.addMenuItem( ...{
! ~5 l5 \2 h1 I$ f2 Z: g2 z& YcName: "设置本页为书签(&B)",3 i: M8 J! C; Q) c
cParent: "View",2 @) p5 P! [* q* G
cExec: "AddBookmark();",
1 D' q& h" L5 i( D2 JcEnable: "event.rc= (event.target != null);" } );
* Q; t- B3 Z; x. E9 uapp.addMenuItem( ...{: x: t1 t) B9 a, X" x2 O
cName: "转到指定书签(&T)",
' Y; w" H/ t, T0 }; mcParent: "View",
3 G3 [3 C, `8 v7 `6 R) t7 [1 D/ xcExec: "ShowBookmarks();",& J3 w8 c5 c+ ^% l
cEnable: "event.rc= (event.target != null);" } );
8 t8 u) z2 A: _- s+ L6 M//cEnable: "event.rc= true;" } );1 m6 Y5 S7 V/ J7 T
app.addMenuItem( ...{
/ t' e( Z( A: m! @+ F% rcName: "删除一个书签(&D)",1 j5 }( e3 I6 C8 s" F' j1 a
cParent: "View",& f, I/ a6 p5 g- x- x
cExec: "DropBookmark();",
! X/ f& K' s! S" J0 K4 lcEnable: "event.rc= (event.target != null);" } );7 ]% r$ D+ b% ]- o- f9 C
app.addMenuItem( ...{
W4 Y0 ]" i* U" }. M3 ?' Q# p) RcName: "清除所有书签(&C)",# ]! n2 N8 i5 ~5 S$ v2 [& H% H, D
cParent: "View",% j' w( j/ M( a5 N1 k
cExec: "ClearBookmarks();",8 L% E$ r% T" w; D/ l
cEnable: "event.rc= true;" } );# g2 @9 d& q1 L" M0 p( r" u8 l1 i" h
4 将文件拷贝到C:\Program Files\Adobe\Reader 8.0\Reader\Javascripts目录下
; z7 }, U8 z; |- \. x u5 重新打开Adobe Reader,看看下面的图形,相信不用说任何东西,大家都会用了- ]) I8 ^7 V8 q% u: z+ \
w' }) q# J o. B+ t! O
; |* a- c& V: d& ?8 | L
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/aohan/archive/2008/02/20/2108051.aspx |
|