PDF to HTML5 conversion – setting your own text widths

When we convert PDF to HTML5 content, we allow several options to embed or replace fonts. Sometimes you want something more sophisticated. How, for example could you use different font widths if you want to replace the font?

What we do not want is for the user to have to customise every new version of the code we release. So we can make use of Interfaces to make this easy to do. The actual PDF to HTML5 conversion is done by a class HTMLDisplay setup in the example ExtractPagesAsHTML. This is actually an implementation of the interface DynamicVectorRenderer so you can change


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


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

MyCustomHTMLDisplay is your class which extends HTMLDisplay.

The reason to do this is to add a small change into your new class. The PDF to HTML5 conversion calls this routine for each glyf. We set the width to a non-possible value (-100) and override it.


public void drawEmbeddedText(float[][] Trm, int fontSize, PdfGlyph embeddedGlyph,
Object javaGlyph, int type, GraphicsState gs,
AffineTransform at, String glyf, PdfFont currentFontData, float glyfWidth){

//if -100 we get value – allows user to override
if(glyfWidth==-100){
glyfWidth=currentFontData.getWidth(-1);
}

 

So we just need to override this in our new class and call the super method!


public void drawEmbeddedText(float[][] Trm, int fontSize, PdfGlyph embeddedGlyph,
Object javaGlyph, int type, GraphicsState gs,
AffineTransform at, String glyf, PdfFont currentFontData, float glyfWidth){

//adjust the width (which will usually be 0-1000 here) and anything else

super.drawEmbeddedText(Trm, fontSize, embeddedGlyph, javaGlyph, type, gs, at, glyf, currentFontData, glyfWidth)

That gives us a robust solution with minimal changes between releases and as little code duplication as possible. What do you think?

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>