Hi Michal, Jozef and Martin,

For a very long time, I have wanted a program that would automatically resize and rescale a PDF page so that it fits on a standard-size sheet of paper. After many bad hacks and after receiving a lot of help, I have gotten closer to my dream.

The attached script:

   *   determines the page orientation
   *   calculates the optimal page metrics and scale factor and
   *   resizes and rescales the pages.

For most purposes, this script will do what I need it to do -- fit the text of an journal article onto a standard size sheet of paper.

But occasionally, I receive a PDF file with enormous white margins. In such cases, I want to crop the pages down to the bounding box before resizing and rescaling the page.

So I would like to include the bounding box values in my script. I know that there is a "getBBox()" function, but I cannot get it to work.

How do I obtain the bounding box values?

Thanks,
- Eric


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 runPDFrsrs( doc, pagenum , papersize ) {

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

    /* determine orientation -- portrait or landscape */
    /* if horizontal longer then landscape, else portrait */
    if ( mbhr - mbhl > mbvt - mbvb ) { 
        if ( papersize == "letter" ) { 
            pgwidth  =  792 ; 
            pgheight =  612 ; 
        }
        if ( papersize == "A4" ) {
            pgwidth  =  29.7 * 28.3464567 ;
            pgheight =  21.0 * 28.3464567 ; 
        }
    } else {
        if ( papersize == "letter" ) {
            pgwidth  =  612 ; 
            pgheight =  792 ; 
        }
        if ( papersize == "A4" ) {
            pgwidth  = 21.0 * 28.3464567 ; 
            pgheight = 29.7 * 28.3464567 ;
        }
    }
    
    /* get the borders of the bounding box */
    hl = mbhl ; 
    vb = mbvb ; 
    hr = mbhr ; 
    vt = mbvt ; 

    /* get scale factor */
    horiz = pgwidth  / (mbhr-mbhl) ;
    vert  = pgheight / (mbvt-mbvb) ;
    
    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 );
}



function pdfrsrs( papersize ) {

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



createMenuItem('pagemenu','Resize and Rescale to Letter',tr('Resize and Rescale 
to Letter'),'pdfrsrs("letter")','','item_edit.png',['need_page']);
createMenuItem('pagemenu','Resize and Rescale to A4',tr('Resize and Rescale to 
A4'),'pdfrsrs("A4")','','item_edit.png',['need_page']);

------------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/pdfedit-support

Reply via email to