>> When I make a pdf file, the images from Canvas (that contains strings 
>> too) and the strings that I directly insert in pdf have differents Font, 
>> and I don't like this.

> Counter-question: have you mapped the Java Font
> with the correct font file? Or are you just using
> the DefaultFontMapper?

This is an example of the code I use.

I know that the problem is in pixel method, that transform Image, that 
becomes almost a bitmap, but I need to fix Font in Canvas, because 
anothe Font can make unwanted effects when I combine lines, String and 
another graphics elements. Now, I think that default pdf Font (the one 
of first String) is Helvetica, while default of Canvas is named Dialog. 
Moreover, default java.awt.Font for paint appears different in Windows 
and Linux. As com.lowagie.text use some font saved in 
com/lowagie/text/pdf/fonts, can I use this Fonts (that are the same in 
all PC) for Frame?

I need something as "public java.awt.Font getDefaultPdfFont()" for 
insert a g.setFont in paint method of Canvas. It exists?

Thank you.

Marco



import java.io.*;
import java.awt.*;
import java.awt.Color;
import java.awt.image.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

class pdf
  {
   public static void main(String[]args)
    {
     picture p=new picture("...and this is a String in a Canvas",new 
int[]{20,20},new int[]{300,40});
     testpdf t=new testpdf("This is a String...",p);
    }
  }

class picture extends Canvas
  {
   public int[]dim;
   private int[]pos;
   private String s;

   public picture(String s,int[]pos,int[]dim)
    {
     super();
     this.pos=pos;
     this.dim=dim;
     this.s=s;
    }

   public void paint(Graphics g)
    {
     g.setColor(Color.yellow);
     g.fillRect(0,0,dim[0],dim[1]);
     g.setColor(Color.black);
     g.drawString(s+" ("+g.getFont().getName()+")",pos[0],pos[1]);
    }

   private java.awt.Image getImage(picture p)
    {
     Frame f=new Frame();
     f.setSize(dim[0],dim[1]);
     f.addNotify();
     java.awt.Image i=f.createImage(dim[0],dim[1]);
     p.paint(i.getGraphics());
     f.dispose();
     return i;
    }

   private int[]pixels(java.awt.Image i)
    {
     int[]p=new int[dim[0]*dim[1]];
     java.awt.image.PixelGrabber pg=new 
java.awt.image.PixelGrabber(i,0,0,dim[0],dim[1],p,0,dim[0]);
     try
      {pg.grabPixels();}
     catch (InterruptedException e)
      {p=null;}
     catch (NullPointerException n)
      {p=null;}
     if ((pg.getStatus()&ImageObserver.ABORT)!=0)
      {p=null;}
     if (p!=null)
      {
       if (p.length==0)
        {p=null;}
      }
     if (p==null)
      {System.out.println("Error");}
     return p;
    }

   public com.lowagie.text.Image getItextImage(picture p) throws 
DocumentException,IOException
    {
     java.awt.Image awtImg=getImage(p);
     com.lowagie.text.Image i=null;
     int[]pixels=pixels(awtImg);
     if (pixels!=null)
      {
       byte[]bytepixels=new byte[pixels.length*3];
       for (int n=0;n<pixels.length;n++)
        {
         bytepixels[3*n]=(byte)(pixels[n]>>16);//red
         bytepixels[3*n+1]=(byte)(pixels[n]>>8);//green
         bytepixels[3*n+2]=(byte)(pixels[n]);//blue
        }
 
i=com.lowagie.text.Image.getInstance(dim[0],dim[1],3,8,bytepixels,new 
int[]{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF});
      }
     return i;
    }
  }

class testpdf
  {
   public testpdf(String s,picture p)
    {
     try
      {
       Document d=new Document();
       PdfWriter w=PdfWriter.getInstance(d,new 
FileOutputStream("testfont.pdf"));
       d.open();
       d.add(new Phrase(s));
       d.add(new Paragraph(""));
       d.add(new Paragraph(""));
       Phrase f=new Phrase();
       f.add(new Chunk(p.getItextImage(p),0,-p.dim[1]));
       d.add(f);
       d.close();
      }
     catch (DocumentException e)
      {e.printStackTrace();}
     catch (IOException e)
      {e.printStackTrace();}
    }
  }


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to