Skip to main content
daitranthanhoa
Inspiring
October 16, 2019
Question

How can get total page of pdf file?

  • October 16, 2019
  • 4 replies
  • 1768 views

 

var strFile="c://test.pdf"; // 50 pages

while(true)
{
try {
pdfOptions.pageToOpen=iPage;
doc = app.open(strFile); 
//backup pdf
var dot = doc.name.lastIndexOf('.');
var newName = doc.name.substring(0, dot) +"_p" +iPage +".pdf";
targetFile = new File( srcFolderStr + '/' + newName );;
var pdfsaveoptions = new PDFSaveOptions();
pdfsaveoptions.pDFPreset=""; 
doc.saveAs(targetFile,pdfsaveoptions);
doc.close(SaveOptions.DONOTSAVECHANGES); 

iPage=iPage+1;
if(iPage>100) break;
}
catch(e) {
break;
}
}

I want open each page of pdf.

I want stop when it is last page.

How can get total page of pdf file? 

This topic has been closed for replies.

4 replies

Ten A
Community Expert
Community Expert
October 23, 2019

You can check PDF files directly.

Here is a sample code.

alert(getPageLength(File("~/Desktop/test.pdf")))


function getPageLength(f){
	var tg = /<<\/Count\s(\d+)/;
	var count = /<<\/Type\/Page\/Parent/;
	var count_2 = /\/Type\s\/Page\s/;
	var count_3 = /\/StructParents\s\d+.*\/Type\/Page>>/;
	var tg2 = /<<\/Linearized\s.+\/N\s(\d+)\/T\s.+>>/; //add target "Linearized PDF Document Structure"
	var tg3 = /\/Type\/Pages/; 
	var tg4 = /\/Count\s(\d+)/;
	var tg5 = /\/Type\/Pages\/Count\s(\d+)/;
	var len = 0;
	var num = 0;

	if(f.open('r')){
		while(!f.eof){
			wd = f.readln();
			if (tg.test(wd)) return RegExp.$1 - 0;
			if (tg2.test(wd)) return RegExp.$1 - 0;
			if (count.test(wd)) len++;
			if (count_2.test(wd)) len++;
			if (count_3.test(wd)) len++;
			if (tg5.test(wd)){
				num = RegExp.$1 - 0;
				if (len<num) len = num;
				} else if (tg3.test(wd)){
					wd = f.readln();
					if (tg4.test(wd)){
						num = RegExp.$1 - 0;
						if (len<num) len = num;
						}
					}
			}
		}
	if(len>0) return len;
	return null;
	}

 

 

 

daitranthanhoa
Inspiring
October 23, 2019

It return not correct page number.

I try pdf 39 page, but it return len is 78.

I debug value of  num is 39.

Ten A
Community Expert
Community Expert
October 23, 2019

In some cases, We can't count pages correctly.

If you can manage PDF files, linearize it and we only search "Linearized" operator.

renél80416020
Inspiring
October 18, 2019

Salut!

Tous les scripteurs ont été confrontés à ce même problème (Il ne faut pas faire de concurrence à Accrobate),
c'est pourquoi il faut être curieux et observateur pour se sortir des situations délicates.

 

-1 Ton script est pour Illustrator oui mais il est plein d'erreurs et ne peut fonctionner...

-2 Carlos t'as précisé que si iPage dépasse le nombre de pages du document pdf on retourne à la page 1 sans message d'erreur.

- 3 Ce que tu dois faire est de comparer chaque page (iPage) avec la page 1.

- 4 Si les 2 pages sont identiques on arrête le traitement.

Si tu n'y parviens pas je te donnerai une solution.

elleere

Legend
October 16, 2019

This is not using Acrobat, or any other Adobe code. It does not belong in the Adobe forums at all.

Legend
October 16, 2019
Oh, I may be mistaken, because PDFSaveOptions is found in C# libraries. It's also in Illustrator scripting, so this may well be the perfect, and only, forum for the question. if so, why not just try pages until an exception is raised...?
CarlosCanto
Community Expert
Community Expert
October 17, 2019

it's Javascript for Illustrator. Getting last page it's not that easy, trying to open a page beyond the upper limit does not raise any errors, instead it just opens the first page.

Inspiring
October 16, 2019

Are you sure this is a Illustrator question?

I think you have to poste this question in the Acrobat forum.

Legend
October 23, 2019

I thought first this was the wrong forum, but although it concerns PDF, the JavaScript used is Illustrator. Unfortunately this in not an app I have programmed. Any ideas?