javascript - Get the current page in PDF Java Web -
i need read current page number web application. have:
<iframe id="mypdf" src="c:\test.pdf#page=5" style="width: 650px; height: 550px;" />
that open pdf on page = 5, , user can navigate page. then, when user click on button/link, need current index of page in pdf.
my users use acrobat reader, , cannot use plugins aren't adobe. can use javascript/java code.
thanks tami
i found out can use itext (free software) inject javascript pdf file , js in pdf send message container in html . message contain current page , , passed container each time page changed.
i use pdfobject.js pdfobject.min.js create pdfjs object catch message .
the code in java inject js pdf :
public string maddjs() { inputstream vbreaderfile = null; try { vbreaderfile = new fileinputstream("c:\\test.pdf"); pdfreader myreader = new pdfreader( vbreaderfile ); // throws ioexception pdfstamper mystamper = new pdfstamper( myreader, new fileoutputstream("c:\\outtest.pdf") ); // throws ioe, documentexception // add page-open script, 1 first page, not zero0 pdfaction jsaction = pdfaction.javascript ("app.alert('think again next time!');", mystamper.getwriter()); int pagenum = myreader.getnumberofpages(); for(int i=1 ; i<pagenum ; i++ ) { jsaction = pdfaction.javascript ( "this.disclosed=true;" + "if(this.hostcontainer){" + "var names = new array();names[0]=" + + ";" + "try{this.hostcontainer.postmessage(names);}" + "catch(e){app.alert(e.message); }"+ "}", mystamper.getwriter()); mystamper.setpageaction( pdfwriter.page_open, jsaction, i); } mystamper.close(); // write out, throws documentexception, ioe vpath = "c:\\outtest.pdf"; // javascriptcontext.addjavascriptcall(facescontext.getcurrentinstance(), "mchangepdf('c:\\outtest.pdf');"); } catch (exception e) { e.printstacktrace(); } return null; }
the html page :
<h:body > <ice:form id="myform"> <object id="mypdfobj" type="application/pdf" data="test.pdf#page=3" height="550px" width="650px"></object> <br/><ice:commandbutton onclick="test();" value="test" ></ice:commandbutton> <ice:commandbutton action="#{pdftest.maddjs}" value="java" partialsubmit="false"></ice:commandbutton> <div id="mydivpdf"> </div> </ice:form> </h:body>
the js on html:
<script type="text/javascript"> var pdf1; var pdf2; window.onload = function(evt) { createpdf(); createmessagehandler(); }
function createpdf(){
pdf1 = new pdfobject({ url: "test.pdf#page=3", id: "mypdfobj", width: "500px", height: "300px" }); pdf2 = pdf1.embed("myform:mydivpdf");
};
function createmessagehandler() { var pdfobject = document.getelementbyid("mypdfobj"); if(pdfobject==null) alert('e1'); pdfobject.messagehandler = { onmessage: function(msg) { // alert('app'); alert( msg[0]); }, onerror: function(error, msg) { alert('error.message'); } } } </script>
Comments
Post a Comment