javascripthtmlpdfpdfobject

How to use the search feature in PDFObject?


I'm using PDFObject to try to search a PDF for a certain term and highlight it. The website actually generates code for such an application automatically but for some reason this code isn't actually searching my PDF at all, nor is it highlighting the word I want to highlight... does anyone know how I can do this or rectify my code?

Here is the code that the website auto generates for me (in HTML):

    <!-- insert in the document body -->

<object data='sample#search=location&highlight=20,20,20,20' 
        type='application/pdf' 
        width='100%' 
        height='100%'>

<p>It appears your Web browser is not configured to display PDF files. 
No worries, just <a href='sample'>click here to download the PDF file.</a></p>

</object>

The search term I want to find (and have highlighted) in my PDF Object is a word called 'location'.

I've copied this code into my overall HTML code, which is as follows:

    <html>
    <head>
     <title>PDFObject Example</title>
     <script type="text/javascript" src="pdfobject.js"></script>
     <script type="text/javascript">
      window.onload = function (){
        var myPDF = new PDFObject({ url: "sample.pdf" }).embed();
      };
    </script>
  </head>
  <body>
      <object data='sample.pdf#search=location&highlight=20,20,20,20' 
              type='application/pdf' 
              width='100%' 
              height='100%'>

      <p>It appears your Web browser is not configured to display PDF files. 
      No worries, just <a href='sample.pdf'>click here to download the PDF file.</a></p>
</body>
      </object>
</html>

Thanks in advance!


UPDATE (7/13)

The code provided by Pipwerks works perfectly, thank you. I just have one remaining question; I used 'this' code (see below) to add a highlight for a search term, but the highlighting itself does NOT automatically show up when I search for a term; any idea how to resolve that? So essentially, when the PDF opens in Acrobat, the search function is working fine, and in the left menu pane, I can see the results of the search; however the terms I am searching for themselves are not automatically highlighted on the screen upon the PDF loading; is there anyway to set this up so that the terms I'm searching for are highlighted upon loading the PDF?

 <html>
  <head>
     <script type="text/javascript" src="pdfobject.js"></script>
     <script type='text/javascript'>

       function embedPDF(){

         var myPDF = new PDFObject({ 

           url: 'sample.pdf',
           pdfOpenParams: { search: 'B27-1', highlight: '30,30,30,10' }

         }).embed();  

       }

       window.onload = embedPDF; //Feel free to replace window.onload if needed.

     </script>
  </head>
      </object>
</html>

HERE is what the PDF currently looks like when index.html (the search feature works correctly, but my term isn't highlighted by default).

HERE is what I want the PDF to look like upon loading index.html; if you actually click the search term, then a highlight box appears (as seen in the image). I would like for that highlight box to load with the PDF for whatever search term I am trying to find on my map.

Any help would be appreciated, you've really been a lifesaver Pipwerks, thanks for your help!


Solution

  • Unfortunately, the PDF Open Parameters are Adobe-specific and don't work in most PDF readers, including the built-in PDF viewers in Chrome and Firefox. In fact, it's been an issue/feature request in Google's Chromium project for nearly five years.

    Does it work for you in Adobe Reader?

    -- UPDATE --

    Re-reading your post, your code is not correct. You have mixed up the static and JavaScript options -- you need to pick one or the other. As you have written it, your JavaScript (which doesn't include search params) is overwriting your HTML version.

    JavaScript method:

    <html>
    <head>
    <title>PDFObject Example</title>
    <script type="text/javascript" src="pdfobject.js"></script>
    <script type='text/javascript'>
    
    function embedPDF(){
    
      var myPDF = new PDFObject({ 
    
        url: 'sample.pdf',
        pdfOpenParams: { search: 'location' }
    
      }).embed();  
    
    }
    
    window.onload = embedPDF;
    
    </script></head>
    <body>
    </body>
    </html>
    

    Static HTML method:

    <html>
    <head>
    <title>PDFObject Example</title>
    </head>
    <body>
    <object data='sample.pdf#search=location' 
            type='application/pdf' 
            width='100%' 
            height='100%'>
    
      <p>It appears your Web browser is not configured to display PDF files. 
      No worries, just <a href='sample.pdf'>click here to download the PDF file.</a></p>
    
    </object>
    </body>
    </html>