Hummusjs: text alignment for PDF

Created on 11 Sep 2013  ·  3Comments  ·  Source: galkahana/HummusJS

Hi,

I'm using this module to create PDF Files.
I need some texts alignment properties like center, left align or right align.
Does HummusJS support this one?
Because it's not useful for me to use the constant location.

Thanks in advance.

Most helpful comment

This might not be groundbreaking, but if anyone else is coming across this trying to center text here's a little function to do so. It assumes you have a box within which you want to center your text, and you use the left coordinate and the width of that box to perform the calculation. "fontObject" should be the output of getFontForFile, such as:

var fontObject = pdfWriter.getFontForFile('./TestMaterials/fonts/arial.ttf');

function CalculateLeftForCentering(fontObject, fontSize, textContent, containerLeft, containerWidth)
{
    var leftForCenter = containerLeft;
    var textDimensions = fontObject.calculateTextDimensions(textContent, fontSize);

    var marginAmount = (containerWidth - textDimensions.width)/2;

    if(marginAmount > 0)
    {
        leftForCenter = containerLeft + marginAmount;
    }

    return leftForCenter;
}

All 3 comments

nope. nothing like that. you can implement this though using the text measuring method. here: https://github.com/galkahana/HummusJS/wiki/Show-text#measuring-text

This might not be groundbreaking, but if anyone else is coming across this trying to center text here's a little function to do so. It assumes you have a box within which you want to center your text, and you use the left coordinate and the width of that box to perform the calculation. "fontObject" should be the output of getFontForFile, such as:

var fontObject = pdfWriter.getFontForFile('./TestMaterials/fonts/arial.ttf');

function CalculateLeftForCentering(fontObject, fontSize, textContent, containerLeft, containerWidth)
{
    var leftForCenter = containerLeft;
    var textDimensions = fontObject.calculateTextDimensions(textContent, fontSize);

    var marginAmount = (containerWidth - textDimensions.width)/2;

    if(marginAmount > 0)
    {
        leftForCenter = containerLeft + marginAmount;
    }

    return leftForCenter;
}

thank you.

Was this page helpful?
0 / 5 - 0 ratings