pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andreas Lehmkühler <andr...@lehmi.de>
Subject Re: DrawString with TrueType embedded fonts.
Date Sat, 14 Feb 2009 20:08:15 GMT
> When I use drawString on a PDFont object loaded from an embedded TTF,
> the resulting string shows a different font. Here is my code:
> 
> PDDocument pdf = PDDocument.load(is);
> 
> BufferedImage img = new BufferedImage(160,160,BufferedImage.TYPE_INT_RGB);
> java.util.Map m =
> ((PDPage)pdf.getDocumentCatalog().getAllPages().get(0)).getResources().getFonts();
> int x =0;
> for(Object key : m.keySet())
> {
> Object val = m.get(key);
> PDFont f = (PDFont)val;
>                        System.out.println(f.getBaseFont());
> f.drawString("AXIL@UAXICUIUASD",img.getGraphics(),
> (float)12.0,
> (float)1.0,
> (float)1.0,
> (float)50.0,
> (float)100.0 + x);
> x+=10;
> }
> javax.imageio.ImageIO.write(img,"bmp",new
> java.io.FileOutputStream("/tmp/out.bmp"));
> pdf.close();
> 
> The font families that are rendered in a sample document are:
> Arial-BoldMT
> TimesNewRomanPS-BoldMT
> ArialMT
> TimesNewRomanPSMT
> 
> And here is the resulting image:
> http://i39.tinypic.com/o8xr8w.png
> 
> Also, these embedded fonts are bad, I know that, so that the letters I chose
> to render should run together and become illegible.
> 
> Is it something wrong I am doing; or is drawString ignoring the TrueType
> fonts?
Hmmm, I still try to understand what you're trying to do? What is the
goal of your implementation? Do you try to put some text on a page and
convert this page to a png image?
If I am right I suggest to do it like this:

- create a PDDocument
- add a PDPage to the document
- add the needed fonts to the document wiht PDTrueTypeFont.loadTTF
- use PDPageContentStream to add some text with beginText, drawString,
endText etc.
- use PDPage.convertToImage to create the image


> This is with pdfbox 0.7.4.
There are some improvements concerning font-handling in later 0.7.4 versions
and in the upcoming version 0.8.


HTH
Andreas


Mime
View raw message