Read the PDF page size

Hi there

I need to find the size of a PDF document, the PDF’s are my source data, but the client also sends A3 (and other sizes) which i need to exclude (only A4 in this run)

I use the below for the number of pages, I just need to size:

’ get number of pages in the PDF
Set MyPDF=Watch.GetPDFEditObject()
Call MyPDF.Open (myPDFfile, true)
Watch.SetVariable “pdfFilePageCount”, MyPDF.Pages.Count()
MyPDF.Close()

A PDF does not have a “size” for the entire file, each page may have different dimensions.
That’s why you have to look at the MediaSize() for each page.

Also, keep in mind that “length” and “width” are arbitrary values that depend not only on page size but also on the orientation of each page. So for an A4 page, the dimensions could be either 595x842 points or 842x595 points.

Thanks Phil

i have put this together - seems to work, is there a better way?

// i have the path and filename in variables - this just strings the path & name together
myPDFfile = Watch.GetVariable("pdfFilePath");
myPDFfile = myPDFfile + Watch.GetVariable("pdfFileName");
Watch.Log("RT" + myPDFfile, 3);

var pdf = Watch.GetPDFEditObject();

// removing the double "//"
var myPDFfile1;
myPDFfile1 = myPDFfile.replace(/\\\\/g,"\\");
Watch.Log("RT new " + myPDFfile1, 3);

// read page 1

pdf.open(myPDFfile1,false);
var pdfRect = pdf.Pages(0).Size();
var pageWidth = pdfRect.right - pdfRect.left ;
var pageHeight = pdfRect.top - pdfRect.bottom ;

Watch.Log("RT Width:" + pageWidth, 3);
Watch.Log("RT Height" + pageHeight, 3);

// same idea in a loop for all pages 
var objPages = pdf.Pages();
var objPage = null;
for(var i=0; i<objPages.Count(); i++) {
   objPage = objPages.Item(i);


        pdfRect = pdf.Pages(i).Size();
        pageWidth = pdfRect.right - pdfRect.left ;
        pageHeight = pdfRect.top - pdfRect.bottom ;

        Watch.Log("RT Page:"+i, 3);
        Watch.Log("RT Width:" + pageWidth, 3);
        Watch.Log("RT Height" + pageHeight, 3);

}

objPage = null;
pdfRect = null;
pageWidth = null;
pageHeight = null;
CollectGarbage();
pdf.close();

Yep, that’s pretty much the idea, although I would recommend using the MediaSize() property, which specifies the actual media size as opposed to Size(), which specifies the expected displayable size.
Most times, they’re the same… but not always.