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.

PDF to HTML5 conversion – setting your own text widths

1 min read

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?

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.

Converting your PDF files to HTML5 with BuildVuĀ 

Recently we announced our updated product range for 2018 and are rebranding some existing products, like JPDF2HTML5 which has been renamed to BuildVu. It...
Georgia Ingham
3 min read

Favourite resources from our HTML development team

As the web progresses and grows, so do the technologies that come along with it. Trying to keep on top of everything you need...
Ovidijus Okinskas
1 min read

How HTML5 Javadocs in Java 9 will make your…

Here at IDRsolutions we are very excited about Java 9 and have written a series of articles explaining some of the main features. In...
Rob
1 min read

Leave a Reply

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