Re: Possible to change the starting page number?

393 views
Skip to first unread message

ch...@cwfent.com

unread,
Aug 27, 2013, 1:54:09 PM8/27/13
to pdfnet-w...@googlegroups.com
I've been trying to follow this thread without much success, ReadControl.js has changed a bit since v1.4.

I'm looking to do something similar but easier I hope, I just want to load a single document at a specific page number. So if for example I have a 10 page document, I want to load that document with the default starting page being page x. In a perfect world something like

            <div id="viewer" style="width:800px;height:800px;"></div>
            <script type="text/javascript">   
                $(function() {   
                    var viewerElement = document.getElementById('viewer');   
                    var myWebViewer = new PDFTron.WebViewer({   
                        type: 'html5,flash,Silverlight',
                        pageNumberBox: 4,
                        initialDoc: "pathtoxod"   
                    }, viewerElement);   
                });
            </script>

And then the viewer initialized the document starting on page 4.

Is there any easy way to accomplish that?

TIA

Matt Parizeau

unread,
Aug 27, 2013, 3:06:37 PM8/27/13
to pdfnet-w...@googlegroups.com
To get this to work you'll have to create a custom config file (see the HTML5 samples for examples).  Then you need to add a config: myConfigFile.js option to the WebViewer constructor.  If you always know what page you're going to be starting at then the config file could be as simple as:
$(document).bind("documentLoaded", function(event) {
    readerControl
.setCurrentPageNumber(4);
});

If you want to have it be any page number then you'll need to use custom data like I mentioned above.  So you'll add another option to the WebViewer constructor like: custom: JSON.stringify({startPage: 4}) and then in the config file have:
$(document).bind("documentLoaded", function(event) {
   
var custom = JSON.parse(window.ControlUtils.getCustomData());
    readerControl
.setCurrentPageNumber(custom.startPage);
});

Matt Parizeau
Software Developer
PDFTron Systems Inc.

ch...@cwfent.com

unread,
Aug 27, 2013, 3:10:37 PM8/27/13
to pdfnet-w...@googlegroups.com
I finally got this, but its ugly. I used the custom param in WebvViewer.js as described above, then in ReadControl.js I changed

$('#pageNumberBox').val(pageIndex + 1)

To

var custom = JSON.parse(window.ControlUtils.getCustomData());
var spn = custom ? custom.StartPageNum : 1;           
$('#pageNumberBox').val(spn);
readerControl.setCurrentPageNumber(spn);

Like I said ugly (and I'm probably luckly that my readerControl.setCurrentPageNumber there is being set after the initial one I would guess).

Should I be doing it a different way? Like a lot of people I'm serving up everything dynamically so using the "config" option and then in there doing extra ajax requests to get the number I want wouldn't be ideal.

Thanks

ch...@cwfent.com

unread,
Aug 27, 2013, 3:43:22 PM8/27/13
to
We were replying at the same time :)

I tried what you had there for the custom, exactly as you had it and it didn't work. I tried it all over the place in ReaderControl.js starting right under

var CoreControls = exports.CoreControls;

    var custom = JSON.parse(window.ControlUtils.getCustomData());
   
ReaderControl.config.StartPageNum = custom ? custom.StartPageNum : 1;
    $
(document).bind("documentLoaded", function(event){
       
//document finished loading
       readerControl
.setCurrentPageNumber(ReaderControl.config.StartPageNum);
   
});        

No matter what I tried I would get page 1 (I didn't see any errors in the console and I did get the viewer).

-Chris

Matt Parizeau

unread,
Aug 27, 2013, 3:48:49 PM8/27/13
to pdfnet-w...@googlegroups.com
I tried the code that you have there in ReaderControl.js where you mentioned and I get an error about ReaderControl.config being undefined (which is expected since it won't have been defined yet).  If I change it to use a local variable like the following then it works:
var custom = JSON.parse(window.ControlUtils.getCustomData());
var startPageNum = custom ? custom.StartPageNum : 1;

$
(document).bind("documentLoaded", function(event){
   
//document finished loading

   readerControl
.setCurrentPageNumber(startPageNum);
});

I would recommend you add the customization to either ReaderControlConfig.js or your own custom config file.  In that case your original code should work since ReaderControl.config will be defined and ideally you can keep your customizations contained within that file.  Also be careful with the case for your custom property, the first time I was trying this it didn't work because I specified the property as startPageNum instead of StartPageNum in the WebViewer options.

Matt Parizeau
Software Developer
PDFTron Systems Inc.

On Tuesday, August 27, 2013 12:16:54 PM UTC-7, (unknown) wrote:
We were replying at the same time :)

I tried what you had there for the custom, exactly as you had it and it did work. I tried it all over the place in ReaderControl.js starting right under


var CoreControls = exports.CoreControls;

    var custom = JSON.parse(window.ControlUtils.getCustomData());
   
ReaderControl.config.StartPageNum = custom ? custom.StartPageNum : 1;
    $
(document).bind("documentLoaded", function(event){
       
//document finished loading
       readerControl
.setCurrentPageNumber(ReaderControl.config.StartPageNum);
   
});        

No matter what I tried I would get page 1 (I didn't see any errors in the console and I did get the viewer).

-Chris



On Tuesday, August 27, 2013 3:06:37 PM UTC-4, Matt Parizeau wrote:

ch...@cwfent.com

unread,
Aug 28, 2013, 8:00:26 AM8/28/13
to pdfnet-w...@googlegroups.com
Thanks Matt, that did the trick!

ch...@cwfent.com

unread,
Sep 10, 2013, 1:11:10 PM9/10/13
to pdfnet-w...@googlegroups.com
Matt, thanks again for helping me get this worked out for the html5 browser. Can you give me any tips on how I could go about this for the flash version? I'd really like to be able to open the html5 and flash viewers on a specific page.

TIA!

Matt Parizeau

unread,
Sep 10, 2013, 4:22:37 PM9/10/13
to pdfnet-w...@googlegroups.com
It looks like I overlooked a simple way to get this to work for all WebViewer viewers!  You can actually just bind the WebViewer element to the documentLoaded event and then call setCurrentPageNumber there.  Code would be like the following:

var viewerElement = document.getElementById('viewer');    
var myWebViewer = new PDFTron.WebViewer({
   
    type
: 'html5,flash,silverlight',
    initialDoc
: "pathtoxod"    
}, viewerElement);

$
(viewerElement).on('documentLoaded', function(evt) {
    setTimeout
(function() {
        myWebViewer
.setCurrentPageNumber(4);
   
}, 0);
});

It seems like you need to wrap it in a setTimeout for Silverlight to work, but this should be all there is to it.

Matt Parizeau
Software Developer
PDFTron Systems Inc.

ch...@cwfent.com

unread,
Sep 11, 2013, 1:02:06 PM9/11/13
to pdfnet-w...@googlegroups.com
Matt, that's AWESOME!! Its working great for me.

Thanks!!!
Reply all
Reply to author
Forward
0 new messages