Skip to main content
New Participant
February 1, 2024
Question

How to delete pages based on page labels rather than page numbers in Javascript?

  • February 1, 2024
  • 2 replies
  • 920 views

Hey everybody,

 

First post on here. Searched extensively and I couldn't find any answers to this question.

I have a fillable form that I'd like to add a button to that will delete a set of pages. The page numbers on these may change as someone edits the pdf, so I cannot delete them using the deletePages() function. I have tried giving these pages specific labels (which seem like they should stay the same regardless of whether people add or remove pages throughout).

 

Is there a javascript function to delete pages based on the page label or convert the label to the page number? Or another workaround for this?

This topic has been closed for replies.

2 replies

try67
Community Expert
February 1, 2024

The idea of adding fields to the pages is a good one, but I don't understand the original issue, I must say.

Someone editing a page does not affect its number, unless they move it about in the file. Is that issue?

Anyway, to identify a page by its label would require using a for-loop over the entire array of pages, checking the label of each one (using getPageLabel) until a match is found. Then you use that page's number as the input parameter of deletePages.

JR Boulay
Community Expert
February 1, 2024

You can add a (hidden) form field on these pages, then detect their page number and delete the corresponding pages.

Acrobate du PDF, InDesigner et Photoshopographe
New Participant
February 1, 2024

Ok cool - what's the function to find the page number of a form field?

try67
Community Expert
February 1, 2024

this.getField("FieldName").page