Skip to main content
Participant
July 2, 2024
Answered

Printing "destination" ranges only

  • July 2, 2024
  • 1 reply
  • 465 views

Hello! I am trying to add JavaScript to a button in my PDF that will print the pages within a specified destination (and only the specified destination's pages). The plan is to have buttons at the beginning of each chapter (or destination) which the user can press to print only the chapter. We will occasionally need to add pages to this PDF so specifying page ranges to print will not work. 

Can anyone help with this?

This topic has been closed for replies.
Correct answer try67

A destination doesn't point to a range of pages, but to a specific page (or more accurately, to a specific view within a page), so you would need to figure out the range of pages based on the first page of the chapter you want to print, and the first page of the next chapter, minus one (unless it's the last chapter in the file, in which case it should be to the last page).

Also, you will have to know the names of the destinations in advance to be able to do that, as a script can't find out what destinations exist (or don't) in a file.

Assuming you know those names (and that each chapter begins at the top of a page) you can use something like this for that (let's say you want to print "Chapter1"):

 

 

 

var originalPage = this.pageNum;
this.gotoNamedDest("Chapter1");
var firstPage = this.pageNum;
this.gotoNamedDest("Chapter2");
var lastPage = this.pageNum;
if (lastPage>firstPage) lastPage--;
this.print({nStart: firstPage, nEnd: lastPage})
this.pageNum = originalPage;

 

 

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
July 2, 2024

A destination doesn't point to a range of pages, but to a specific page (or more accurately, to a specific view within a page), so you would need to figure out the range of pages based on the first page of the chapter you want to print, and the first page of the next chapter, minus one (unless it's the last chapter in the file, in which case it should be to the last page).

Also, you will have to know the names of the destinations in advance to be able to do that, as a script can't find out what destinations exist (or don't) in a file.

Assuming you know those names (and that each chapter begins at the top of a page) you can use something like this for that (let's say you want to print "Chapter1"):

 

 

 

var originalPage = this.pageNum;
this.gotoNamedDest("Chapter1");
var firstPage = this.pageNum;
this.gotoNamedDest("Chapter2");
var lastPage = this.pageNum;
if (lastPage>firstPage) lastPage--;
this.print({nStart: firstPage, nEnd: lastPage})
this.pageNum = originalPage;

 

 

Participant
July 3, 2024

Oh my gosh, thank you!!! That worked! I appreciate it. I've been trying to figure this out for weeks!!!