tamih
tamih

Reputation: 93

Get the current page in PDF Java Web

I need to read the current page number from my web application. I have:

 <iframe id="myPdf" src="C:\test.pdf#page=5" style="width: 650px; height: 550px;" />

That open the PDF on page = 5, and the user can navigate to another page. Then, when the user click on a button/link, I need to get the current index of the page in PDF.

My users use Acrobat Reader, and I cannot use plugins that aren't from Adobe. I can use Javascript/Java code.

Thanks Tami

Upvotes: 1

Views: 1830

Answers (1)

tamih
tamih

Reputation: 93

I found out that I can use iText (free software) to Inject JavaScript to the pdf file , the JS in the PDF send a message to a container in the html . the message contain the current page , and it is passed to the container each time the page is changed.

I use pdfobject.js pdfobject.min.js to create the PDFJS object that catch the message .

the code in Java to inject JS to 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 a page-open script, 1 is the 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]=" + i + ";" +
                     "try{this.hostContainer.postMessage(names);}" +
                     "catch(e){app.alert(e.message); }"+
                     "}", myStamper.getWriter());

             myStamper.setPageAction( PdfWriter.PAGE_OPEN, jsAction, i); 

        }

        myStamper.close(); // write everything 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 the 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>

Upvotes: 1

Related Questions