获取PDF Java Web中的当前页面 [英] Get the current page in PDF Java Web

查看:177
本文介绍了获取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;" />

在页面= 5上打开PDF,用户可以导航到另一个页面。然后,当用户点击按钮/链接时,我需要以PDF格式获取页面的当前索引。

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.

我的用户使用Acrobat Reader,我无法使用不是来自Adobe的插件。我可以使用Javascript / Java代码。

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

谢谢
Tami

Thanks Tami

推荐答案

我发现我可以使用iText(免费软件)将JavaScript注入pdf文件,
PDF中的JS向html中的容器发送消息。
消息包含当前页面,每次页面更改时都会传递给容器。

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.

我使用pdfobject.js pdfobject.min.js创建捕获消息的PDFJS对象。

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

用Java注入JS到PDF的代码:

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;

}

html页面:

 <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>

html上的JS:

  <script type="text/javascript">

     var PDF1;
     var PDF2;

     window.onload = function(evt)
     {
         createPDF();

         createMessageHandler();
     }

function createPDF(){

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>

这篇关于获取PDF Java Web中的当前页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆