You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Shawn A <mr...@gmail.com> on 2009/02/14 05:18:13 UTC
DrawString with TrueType embedded fonts.
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?
This is with pdfbox 0.7.4.
Re: DrawString with TrueType embedded fonts.
Posted by Andreas Lehmkühler <an...@lehmi.de>.
Shawn A schrieb:
>> 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:
>
> What I am trying to accomplish right now is just use the embedded truetype font
> and render some text to an image. Are you talking about the previous
> time we talked
> when you mentioned implementation?
No, last time we talk about overlapping characters.
Perhaps there is a easier or better way to do the same. But first of all
I have to understand in detail what you are trying to do. And that's why
I'm asking so much ;-))
>> - 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
>
> I could do that, except that the TTF's I have are still inside of an
> existing PDF, so I need to add it another way.
So, is it correct, that the only reason for you to load the pdf is to
use the embedded fonts for drawing strings on a buffered image?
> I am also not sure if you are mentioning
> this in the context
> of the last time we spoke or not?
No, see above.
Andreas
Re: DrawString with TrueType embedded fonts.
Posted by Shawn A <mr...@gmail.com>.
> 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:
What I am trying to accomplish right now is just use the embedded truetype font
and render some text to an image. Are you talking about the previous
time we talked
when you mentioned implementation?
> - 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
I could do that, except that the TTF's I have are still inside of an
existing PDF, so I
need to add it another way. I am also not sure if you are mentioning
this in the context
of the last time we spoke or not?
Thank you for your help.
On Sat, Feb 14, 2009 at 8:08 PM, Andreas Lehmkühler <an...@lehmi.de> wrote:
>
> > 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
>
Re: DrawString with TrueType embedded fonts.
Posted by Andreas Lehmkühler <an...@lehmi.de>.
> 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