Update: JPDF2HTM5 has been rebranded as BuildVu and JPDFForms has been rebranded as FormVu

PDF to HTML5 conversion – changing the page size

By default, a we convert a PDF file into an HTML file with the same size. So a 200×100 pixel PDF file becomes a 200×100 pixel HTML file. You can easily scale in/out on most mobile devices but sometimes it is just too big or small as a sensible default.

So we have added a scaling setting into the latest release (so that you can have 400×200 pixels or 50×25 pixels). Note that the scaling has to be the same on both sized (if you turn a 200×100 pixel image into a 150x150pixel image, it will becomes ‘squashed’. It is important to maintain the aspect ratio and treat both sides the same.

Sometimes, however, we might want to scale the image up so that it makes the best fit into a different sized hole in our case, we would need to scale the image to 150x75pixels. This is very easy to do. All we need to do is calculate the scaling needed for height and width to fit the maximum gap and choose the smaller of the two values. Here is the sample code (which is also in our updated example). Happy scaling!

 

DynamicVectorRenderer HTMLoutput=new HTMLDisplay(page, midPoint, 
cropBox ,false, 100, new ObjectStore());

//have a scaling factor so we can alter the page size
float scaling=1.0f;

/**
 * if you want to fit it to a certain size, use this code
 * work out max possible scaling for both width and height
 * and use smaller to get max possible size but retain aspect ratio
 * - will not always be exact match as preserves aspect ratio
**/float preferredWidth=1000,preferredHeight=1000;

// scaling we need to scale w up to our value
float scalingX=preferredWidth/cropW; 
// scaling we need to scale w up to our value
float scalingY=preferredHeight/cropH; 

if(scalingX>scalingY)
   scaling=scalingY;
else
   scaling=scalingX;

//set page scaling (default is 100%)
HTMLoutput.setValue(HTMLDisplay.PercentageScaling, (int) (scaling*100));

Click here to see all the articles in the PDF to HTML5 conversion series.

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>