Problem with PDFont.getStringWidth()

2012-12-05 Thread Gerrit Lober
Dear all,

I've the following problem with PDFbox. I try to paint a rectangle with 
PDPageContentStream and then put a figure in the center of this rectangle.

Therefore I try to calculate the width of the text. The Method getStringWidth 
returns everytime the same width for all figures. That means that I get the 
same result for "12" and "32". Because the "1" is smaller than the "3" 
something is not correct and my "12" is a bit to right.

What is the reason for this?

I get the Font with the following code:
private PDFont getFont() throws IOException {
  return PDType1Font.HELVETICA;
}

Thanks!



Re: Problem with PDFont.getStringWidth()

2012-12-05 Thread Maruan Sahyoun
Hi Gerrit,

which version of PDFBox are you using? Could you post a small code snippet to 
reproduce the issue? With a quick test I created  the numbers do print fine 
i.e. they have the same width.

Her my code:

PDDocument doc = new PDDocument();

PDPage page = new PDPage();
doc.addPage( page );
PDFont font = PDType1Font.HELVETICA;

PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString( " " + font.getStringWidth(""));
contentStream.moveTextPositionByAmount( 0, 20 );
contentStream.drawString( " " + font.getStringWidth(""));
contentStream.moveTextPositionByAmount( 0, 20 );
contentStream.drawString( "12 " + font.getStringWidth("12"));
contentStream.moveTextPositionByAmount( 0, 20 );
contentStream.drawString( "32 " + font.getStringWidth("32"));
contentStream.endText();
contentStream.close();
doc.save( "test.pdf" );


Kind regards

Maruan Sahyoun


Am 05.12.2012 um 14:49 schrieb Gerrit Lober :

> Dear all,
> 
> I've the following problem with PDFbox. I try to paint a rectangle with 
> PDPageContentStream and then put a figure in the center of this rectangle.
> 
> Therefore I try to calculate the width of the text. The Method getStringWidth 
> returns everytime the same width for all figures. That means that I get the 
> same result for "12" and "32". Because the "1" is smaller than the "3" 
> something is not correct and my "12" is a bit to right.
> 
> What is the reason for this?
> 
> I get the Font with the following code:
> private PDFont getFont() throws IOException {
>  return PDType1Font.HELVETICA;
> }
> 
> Thanks!
> 



Re: Problem with PDFont.getStringWidth()

2012-12-05 Thread Andreas Lehmkuehler

Hi,

Am 05.12.2012 14:49, schrieb Gerrit Lober:

Dear all,

I've the following problem with PDFbox. I try to paint a rectangle with 
PDPageContentStream and then put a figure in the center of this rectangle.

Therefore I try to calculate the width of the text. The Method getStringWidth 
returns everytime the same width for all figures. That means that
I get the same result for "12" and "32". Because the "1" is smaller than the "3" 
something is not correct and my "12" is a bit to right.

What is the reason for this?

You are right, helvetica is a proportional font (the character width may vary)
but according to the afm-metric file (helvetica.afm) all figures have the same
with of 556. So far so good. Where do you see the displacement, in adobe reader
or when printing?


I get the Font with the following code:
private PDFont getFont() throws IOException {
   return PDType1Font.HELVETICA;
}

Thanks!



BR
Andreas Lehmkühler



Re: Problem with PDFont.getStringWidth()

2012-12-05 Thread Gerrit Lober
Dear Andreas, thanks, I think you answered my question and it is a problem with 
helvetica font, that every letter has the same width. Therefore it seem when I 
try to center the textposition with

float textPosition = pageCenter.x - (font.getWidth("12")/2);

that my text is not centered because the "1" needs less space than a "3" for 
example in Helvetica.

I see that there is an included resource in PDFbox for ArialMT. How can I load 
this font?

How can I answer a reply in this mailing list? I don't get your mail in my 
inbox.

Thanks!


Re: Problem with PDFont.getStringWidth()

2012-12-06 Thread Andreas Lehmkuehler

Am 06.12.2012 08:22, schrieb Gerrit Lober:

Dear Andreas, thanks, I think you answered my question and it is a problem with 
helvetica font, that every letter has the same width.

Not every letter but the figures


Therefore it seem when I try to center the textposition with

float textPosition = pageCenter.x - (font.getWidth("12")/2);

that my text is not centered because the "1" needs less space than a "3" for 
example in Helvetica.

Try to position each character separately


I see that there is an included resource in PDFbox for ArialMT. How can I load 
this font?

It is a true type font. Use PDTrueTypeFont#loadTTF()


How can I answer a reply in this mailing list? I don't get your mail in my 
inbox.
Send an email to dev-subscr...@pdfbox.apache.org to subscribe to the mailing 
list, see [1] for further information.




Thanks!



BR
Andreas Lehmkühler

[1] http://pdfbox.apache.org/mail-lists.html


Re: Problem with PDFont.getStringWidth()

2012-12-07 Thread Gerrit Lober
Thanks, but which filename should I use to tell pdfbox to use the font from the 
delivered lib?

 Original-Nachricht 
> Datum: Thu, 06 Dec 2012 18:35:58 +0100
> Von: Andreas Lehmkuehler 
> An: dev@pdfbox.apache.org
> CC: Gerrit Lober 
> Betreff: Re: Problem with PDFont.getStringWidth()

> Am 06.12.2012 08:22, schrieb Gerrit Lober:
> > Dear Andreas, thanks, I think you answered my question and it is a
> problem with helvetica font, that every letter has the same width.
> Not every letter but the figures
> 
> > Therefore it seem when I try to center the textposition with
> >
> > float textPosition = pageCenter.x - (font.getWidth("12")/2);
> >
> > that my text is not centered because the "1" needs less space than a "3"
> for example in Helvetica.
> Try to position each character separately
> 
> > I see that there is an included resource in PDFbox for ArialMT. How can
> I load this font?
> It is a true type font. Use PDTrueTypeFont#loadTTF()
> 
> > How can I answer a reply in this mailing list? I don't get your mail in
> my inbox.
> Send an email to dev-subscr...@pdfbox.apache.org to subscribe to the
> mailing 
> list, see [1] for further information.
> 
> 
> > Thanks!
> >
> 
> BR
> Andreas Lehmkühler
> 
> [1] http://pdfbox.apache.org/mail-lists.html