On 03/01/2011 12:48 PM, Eric Doviak wrote:
Everything should be done entirely within PDFedit.


Making some progress. (See attached).
- E




function setProp4( dict, p, a, b, c, d ) {
    if ( !dict.exist( p ) ) {
        var n = createArray();
        n.add( createReal( 0 ) );
        n.add( createReal( 0 ) );
        n.add( createReal( 0 ) );
        n.add( createReal( 0 ) );
        dict.add( p, n );
    }
    
    x = dict.property( p );
    x.property( 0 ).set( a );
    x.property( 1 ).set( b );
    x.property( 2 ).set( c );
    x.property( 3 ).set( d );
}


function setScale( doc , pagenum , sx , sy ) {
    var pg = doc.getPage( pagenum ) ; 
    pg.setTransformMatrix([sx,0,0,sy,0,0]);
}


function runPDFCrop( doc, pagenum ) {

    dict = doc.getPage( pagenum ).getDictionary();
    
    /* get the borders:  hl  vb  hr  vt */ 
    media = dict.property( "MediaBox" );
    hl = media.property( 0 ).value();
    vb = media.property( 1 ).value();
    hr = media.property( 2 ).value();
    vt = media.property( 3 ).value();

    /* assume letter-size paper -- TERRIBLE !!! */ 

    /* determine orientation -- portrait or landscape */
    /* if horizontal longer then landscape, else portrait */
    if ( hr - hl > vt - vb ) { 
        pgwidth  =  792 ; 
        pgheight =  612 ; 
    } else {
        pgwidth  =  612 ; 
        pgheight =  792 ; 
    }
    
    /* get scale factor */
    horiz = pgwidth  / (hr-hl) ;
    vert  = pgheight / (vt-vb) ;
    
    if ( horiz < vert ) { 
        scale = horiz ; 
    } else {
        scale = vert  ;
    }

    /* find midpoints on the page */
    midh = ((hr - hl) / 2 ) + hl ;
    midv = ((vt - vb) / 2 ) + vb ;

    /* we want 5 percent margins on each side */
    margins = 1.10 ; 

    /* here are the new page metrics -- everything centered */
    nhl = ( midh * scale ) - ( pgwidth  / 2 ) ;
    nhr = nhl + pgwidth   ; 
    /* nvb = ( midv * scale ) - ( pgheight / 2 ) ;  */
    /* nvt = nvb + pgheight ;                       */

    /* alternative is to top-align the pages */
    halfmargin = 1 + (( margins - 1 )/2) ; 
    nvb = vb * scale * halfmargin ;
    nvt = nvb + pgheight  ; 

    /* set CropBox, MediaBox and TrimBox. */
    setProp4( dict, "CropBox",  nhl, nvb, nhr, nvt ) ;
    setProp4( dict, "MediaBox", nhl, nvb, nhr, nvt ) ;
    setProp4( dict, "TrimBox",  nhl, nvb, nhr, nvt ) ;
   
    /* rescale the page */
    setScale( doc , pagenum , scale , scale );
}


doc = loadPdf( takeParameter(), false );

/* resize and rescale all of the pages */
for (i=1; i<= doc.getPageCount(); ++i) {
    runPDFCrop( doc , i ) ; 
}

doc.save( false );
exit( 0 );
------------------------------------------------------------------------------
Free Software Download: Index, Search & Analyze Logs and other IT data in 
Real-Time with Splunk. Collect, index and harness all the fast moving IT data 
generated by your applications, servers and devices whether physical, virtual
or in the cloud. Deliver compliance at lower cost and gain new business 
insights. http://p.sf.net/sfu/splunk-dev2dev 
_______________________________________________
Pdfedit-support mailing list
Pdfedit-support@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pdfedit-support

Reply via email to