您的位置:首页 > Web前端 > JavaScript

使用Javascript为Adobe Reader X 添加书签功能

2012-07-10 22:58 337 查看
http://hi.baidu.com/tlptotop/item/36b2cef3449c0b10ce9f3212

以前使用的Javascript 为Adobe Reader添加书签功能,在Adobe Reader X中竟然中文菜单全是乱码,下午参考Adobe SDK的文档,修改了下。Adobe SDK 帮助地址:http://livedocs.adobe.com/acrobat_sdk/10/Acrobat10_HTMLHelp

1.解决了菜单乱码问题。

   使用cUser成员为菜单命名,防止乱码。



2.优化了书签跳转算法,使打开书签没有延迟。

   原程序中使用的是for(page = 0; page < gotogage; ++page) nextPage(); 这样的机制,使得书签跳转速度慢,修改为gotoPage( gotopage);。

3.添加了书签自动排序功能。



 



   书签保存形式为“《文件名》第N...N页/共N...N页 yyyy/mm/dd 备注”(其中备注为可选填项)书签会按照字母顺序自动排序,排序优先级为:文件名>页面>日期>备注。可以打开不同文件。

 

【使用方法】

1.下载文件,将Bookmarks.js解压到Adobe Reader安装目录\Reader\Javascripts文件夹下。

2.运行Adobe Reader,菜单编辑->首选项->JavaScript->勾选启用Acrobat JavaScript和启用菜单项目JavaScript执行权限。

bookmark_page.js

//修改:TOTOP 2011/12/6
//1修改菜单中文显示乱码
//2优先页面跳转的速度
//3书签自动排序
//http://hi.baidu.com/tlptotop tlp.totop@foxmail.com
//SDK 帮助地址:http://livedocs.adobe.com/acrobat_sdk/10/Acrobat10_HTMLHelp

var bp_delim= '%#%#';

function SaveData( data )
{
// data is an array of arrays that needs
// to be serialized and stored into a persistent
// global string
var ds= '';
data.sort();
for( ii= 0; ii< data.length; ++ii )
{
for( jj= 0; jj< 3; ++jj )
{
if( ii!= 0 || jj!= 0 )
ds+= bp_delim;
ds+= data[ii][jj];
}
}
global.pdf_hacks_js_bookmarks= ds;
global.setPersistent( "pdf_hacks_js_bookmarks", true );
}

function GetData()
{
// reverse of SaveData; return an array of arrays
if( global.pdf_hacks_js_bookmarks== null )
{
return new Array(0);
}

var flat= global.pdf_hacks_js_bookmarks.split( bp_delim );
var data= new Array();
for( ii= 0; ii< flat.length; )
{
var record= new Array();
for( jj= 0; jj< 3 && ii< flat.length; ++ii, ++jj )
{
record.push( flat[ii] );
}
if( record.length== 3 )
{
data.push( record );
}
}
return data;
}

//Get Current Date
function DateNow()
{
var d, tmp ,s ;
d = new Date();
t = d.getFullYear();
s = t + "/";
t = (d.getMonth() + 1).toString();
if(t.length!=2) t = "0" + t;
s += t + "/";
t = (d.getDate()).toString();
if(t.length!=2) t = "0" + t;
s += t;
return(s);
}

function AddBookmark()
{
// query the user for a name, and then combine it with
// the current PDF page to create a record; store this record
var thisfilename=this.documentFileName;
thisfilename=thisfilename.substr(0,thisfilename.lastIndexOf("."));
var numLen = this.numPages.toString().length;
var numPlugInss=this.pageNum+1;

while(numPlugInss.toString().length < numLen)
{
numPlugInss = "0" + numPlugInss;
}
var currentdate=DateNow();
var label="《"+thisfilename+"》第 "+numPlugInss+" 页/共 "+this.numPages+" 页 " + currentdate;

var cResponse = app.response(
{
cQuestion: label,
cTitle: "添加书签",
cDefault: "无备注",
cLabel: "备注:"
});

if( cResponse!= null )
{
var record= new Array(3);
record[0]= label + " " + cResponse;
record[1]= this.path;
record[2]= this.pageNum;
data= GetData();
data.push( record );
SaveData( data );
}
}

function ShowBookmarks()
{
// show a pop-up menu; this seems to only work when
// a PDF is alreay in the viewer;
var data= GetData();
var items= '';
for( ii= 0; ii< data.length; ++ii )
{
if( ii!= 0 )
items+= ', ';
items+= '"'+ ii+ ': '+ data[ii][0]+ '"';
}
// assemble the command and the execute it with eval()
var command= 'app.popUpMenu( '+ items+ ' );';
var selection= eval( command );
if( selection== null )
{
return; // exit
}

// the user made a selection; parse out its index and use it
// to access the bookmark record
var index= 0;
// toString() converts the String object to a string literal
// eval() converts the string literal to a number
index= eval( selection.substring( 0, selection.indexOf(':') ).toString() );
if( index< data.length )
{
try
{
// the document must be 'disclosed' for us to have any access
// to its properties, so we use these FirstPage NextPage calls
//
if(this.path == data[index][1])
{
this.pageNum = data[index][2];
}
else
{
var otherDoc = app.openDoc( data[index][1]);
otherDoc.pageNum = data[index][2];
}
}
catch( ee )
{
var response=
app.alert("打开书签错误. 是否删除本书签?", 2, 2,"删除书签");
if( response== 4 && index< data.length )
{
data.splice( index, 1 );
SaveData( data );
}
}
}
}

function DropBookmark()
{
// modelled after ShowBookmarks()
var data= GetData();
var items= '';
for( ii= 0; ii< data.length; ++ii )
{
if( ii!= 0 )
items+= ', ';
items+= '"'+ ii+ ': '+ data[ii][0]+ '"';
}
var command= 'app.popUpMenu( '+ items+ ' );';
var selection= eval( command );
if( selection== null )
{
return; // exit
}

var index= 0;
index= eval( selection.substring( 0, selection.indexOf(':') ).toString() );
if( index< data.length )
{
data.splice( index, 1 );
SaveData( data );
}
}

function ClearBookmarks()
{
if( app.alert("确认要清除所有的书签吗,删除后将不可恢复?", 2, 2,"删除书签" )== 4 )
{
SaveData( new Array(0) );
}
}

app.addMenuItem(
{
cName: "-", // menu divider
cParent: "View", // append to the View menu
cExec: "void(0);"
} );

app.addMenuItem(
{
cName: "AddBookmark",
cUser: "设置本页为书签(&B)",
cParent: "View",
cExec: "AddBookmark();",
cEnable: "event.rc= (event.target != null);"
} );

app.addMenuItem(
{
cName: "ShowBookmarks",
cUser: "转到指定书签(&T)",
cParent: "View",
cExec: "ShowBookmarks();",
cEnable: "event.rc= (event.target != null);"
} );

app.addMenuItem(
{
cName: "DropBookmark",
cUser: "删除一个书签(&D)",
cParent: "View",
cExec: "DropBookmark();",
cEnable: "event.rc= (event.target != null);"
} );

app.addMenuItem(
{
cName: "ClearBookmarks",
cUser: "删除所有书签(&C)",
cParent: "View",
cExec: "ClearBookmarks();",
cEnable: "event.rc= true;"
} );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息