Thursday, July 16, 2015 InDesign-IDML,groovy

How to get page-width and page-height from an IDML file

I will show you how to get page width and height value in an IDML file.

idml

//
// Get page width and height from an idml file.
//

@Grab(group='net.lingala.zip4j', module='zip4j', version='1.3.2')
import net.lingala.zip4j.core.ZipFile


class IdmlDocument {
    private File idmlFile
    float pageWidth
    float pageHeight

    IdmlDocument( File idmlFile ){
       this.idmlFile = idmlFile

       def zipFile = new ZipFile( idmlFile )
       zipFile.getFileHeaders().findAll( { (it.fileName =~ /Preferences.xml/).find() } ).each { FileHeader fileHeader->
         InputStream is = zipFile.getInputStream(fileHeader)
         def reader = new BufferedReader( new InputStreamReader(is,'UTF-8') )
         def document = new SAXBuilder().build( reader )
         def e = document.getDescendants().find( { it instanceof Element && it.name=='DocumentPreference' } )
         setPageWidth ( e.getAttributeValue('PageWidth') as float)
         setPageHeight( e.getAttributeValue('PageHeight') as float )
       }
    }
}

def idmlFile = new File(args[0])
def idmlDocument = new IdmlDocument( idmlFile )

println "pageWidth = ${idmlDocument.pageWidth}"
println "pageHeight = ${idmlDocument.pageHeight}"

usage

groovy main input.idml