B4J Question string length in pixels

strupp01

Active Member
Licensed User
Longtime User
I work with jPDFjet to output data.
Some lines of the data should be centered. Because the output length is different for different fonts and character strings, I need a calculation of the width of a line in pixels.
Is there such a thing?
 

DarkMann

Member
Licensed User
Longtime User
I use PDFJet quite a lot and have a few routines for doing stuff like this.

This is the one to centre a piece of text on the page that I have in my helper code module. It may not be the best way to do it, but it works well.

B4X:
' AddTextLine adds a single line of text to the current target page
' using the font and position specified, centred on the page
Sub AddTextCentered(Target As PDFjetPage, Font As PDFjetFont, Text As String, ypos As Float) 'ignore
    Dim textline As PDFjetTextLine
    textline.Initialize2(Font,Text)
    Dim xpos As Float
    Dim width As Float=textline.GetWidth
    Dim pagewidth As Float=Target.GetWidth
    xpos=(pagewidth/2)-(width/2)
    textline.SetPosition(xpos,ypos)
    textline.DrawOn(Target)
End Sub
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Hello DarkMann,
your code module works wonders. Thank you for your help.
Since you know well with PDFJet from know two more questions.
1. Is there a way to expand fonts? I need the font 'Palatino LinoType' in PDFJet. Is there a possibility for this?
2. Do you have a function to output output lines right-aligned at the same position?
 
Upvote 0

DarkMann

Member
Licensed User
Longtime User
Hello Strupp01,

Here's the whole code module that I use in association with my PDFJet projects. It's lots of helper subs and should give you a bit of help with alignment.

It also has some work-arounds for getting text to justify and wrap (which isn't supported in the free version of PDFJet).

I haven't needed to use other fonts, but there seems to be a font.initialize3 or font.initialize4 that look like they might load a font and embed it into the document you are creating. I must try this, but suspect that only .otf files will work (That's Adobe OpenType Fonts).

David
 

Attachments

  • PDFjetUtils.bas
    11.6 KB · Views: 335
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Hello David,
Thank you for your collection. Help me out anyway.
Of course, super if you can find something to the fonts.

strupp01
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Of course, super if you can find something to the fonts.
Having a license for the full library (pdfjet) you should be able to use other fonts too
 
Upvote 0
Top