There's no UTF-8 encoding for fonts in PDFs. Try:

sans = FontFactory.getFont("sanskrit", "Identity-H", true);

Paulo

  ----- Original Message ----- 
  From: John Kilbourne 
  To: . 
  Sent: Tuesday, March 29, 2011 10:08 PM
  Subject: [iText-questions] writing text with a specific UTF-8 font


  I am having difficulty reading UTF-8 encoded text (Sanskrit) from a text file 
and writing it onto a PDF page. No text is printed at all when I read from the 
file. However, when I simply generate text in the program, it prints as I would 
expect. The non-text portions of the page (the lines for the flashcards) print 
fine. Also, I am able to do a round trip of reading from the file and writing 
to another (text) file in UTF-8 such that I can read it as I expect; I 
commented that part out.

  I wonder if I am using the correct the correct Font invocations on in my 
drawText() method. There seem to be a number of ways to get a font 
(FontFactory, Font, BaseFont..) and I am not compeletely positive that i am 
doing it the correct way. Any help is appreciated.


  import java.io.*;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.util.Scanner;
  import java.util.List;
  import java.util.ArrayList;

  import com.itextpdf.text.Document;
  import com.itextpdf.text.DocumentException;
  import com.itextpdf.text.Element;
  import com.itextpdf.text.Phrase;
  import com.itextpdf.text.Font;
  import com.itextpdf.text.FontFactory;
  import com.itextpdf.text.PageSize;
  import com.itextpdf.text.pdf.ColumnText;
  import com.itextpdf.text.pdf.PdfContentByte;
  import com.itextpdf.text.pdf.PdfWriter;

  public class BusCard {

  float LTMARG= 0.5f*72;
  float BOTMARG= 0.25f*72;
  float CARDWIDTH= 3.5f*72;
  float CARDHT= 2*72f;
  float PAD=15;

  float [][] VRT_2BY5_BCARDS = new float[][]{
  {LTMARG+PAD, BOTMARG+(5*CARDHT)-PAD, CARDWIDTH-PAD/2, CARDHT-PAD/2},
  {LTMARG+2*CARDWIDTH+PAD, BOTMARG+(5*CARDHT)-PAD, CARDWIDTH-PAD/2, 
CARDHT-PAD/2},

  {LTMARG+PAD, BOTMARG+(4*CARDHT)-PAD, CARDWIDTH-PAD/2, CARDHT-PAD/2},
  {LTMARG+2*CARDWIDTH+PAD, BOTMARG+(4*CARDHT)-PAD, CARDWIDTH-PAD/2, 
CARDHT-PAD/2},

  {LTMARG+PAD, BOTMARG+(3*CARDHT)-PAD, CARDWIDTH-PAD/2, CARDHT-PAD/2},
  {LTMARG+2*CARDWIDTH+PAD, BOTMARG+(3*CARDHT)-PAD, CARDWIDTH-PAD/2, 
CARDHT-PAD/2},

  {LTMARG+PAD, BOTMARG+(2*CARDHT)-PAD, CARDWIDTH-PAD/2, CARDHT-PAD/2},
  {LTMARG+2*CARDWIDTH+PAD, BOTMARG+(2*CARDHT)-PAD, CARDWIDTH-PAD/2, 
CARDHT-PAD/2},

  {LTMARG+PAD, BOTMARG-PAD, CARDWIDTH-PAD/2, CARDHT-PAD/2},
  {LTMARG+2*CARDWIDTH+PAD, BOTMARG-PAD, CARDWIDTH-PAD/2, CARDHT-PAD/2},
  };

  public static final String RESULT = 
"C:/all/_sanskrit/flashcards/buscards.pdf";
  protected static final String 
cardfile="C:/all/_sanskrit/flashcards/SEL-first10.txt";

  List <String>front;
  List <String>back;
  Font sans;
       
  public void createPdf(String filename)
       throws DocumentException, IOException {
    // step 1
       Document document = new Document(PageSize.LETTER);
       // step 2
       PdfWriter writer
           = PdfWriter.getInstance(document, new FileOutputStream(filename));
       // step 3
       document.open();
       // step 4
       drawCards(writer.getDirectContentUnder());
       drawText(writer.getDirectContentUnder());
       // step 5
       document.close();
  }

       void readfile()throws IOException{
  Scanner scanner = new Scanner(new FileInputStream(cardfile), "UTF-8");
  front = new ArrayList<String>();
  back = new ArrayList<String>();
  try {
  while (scanner.hasNextLine()) {
  String line = scanner.nextLine();
  String[] fields = line.split("\\t");
  front.add(fields[0]);
  if (fields.length>1){back.add(fields[1]);}
  }
  } finally {
  scanner.close();
  }
       }
       
  protected void drawCards(PdfContentByte pcb) 
  throws DocumentException {
  int i;
  pcb.saveState();
  for (i=0;i<=4; i++){
  pcb.rectangle(LTMARG , BOTMARG + i*CARDHT, CARDWIDTH, CARDHT);
  pcb.stroke();
  }
  for (i=0;i<=4; i++){
  pcb.rectangle(LTMARG + CARDWIDTH, BOTMARG + i*CARDHT, CARDWIDTH, CARDHT);
  pcb.stroke();
  }
  pcb.restoreState();
  }

  protected void drawText(PdfContentByte pcb)
  throws IOException, DocumentException {
  FontFactory.register("c:/windows/fonts/sanskrit2003.ttf", "sanskrit");
       sans = FontFactory.getFont("sanskrit", "UTF-8", true);
  readfile();
  pcb.saveState();
  for (int i=0;i<10;i++){
  ColumnText ct = new ColumnText(pcb);
  //Phrase myText = new Phrase(front.get(i));
  Phrase myText = new Phrase(i+ " read this");
  myText.setFont(sans);
  //out.write(front.get(i));
  ct.setSimpleColumn(myText, 
  VRT_2BY5_BCARDS[i][0],
  VRT_2BY5_BCARDS[i][1],
  VRT_2BY5_BCARDS[i][2],
  VRT_2BY5_BCARDS[i][3],
  16,
  Element.ALIGN_CENTER);
  ct.go();
  }
  pcb.restoreState();
  }

  public static void main(String[] args) throws DocumentException, IOException {
          new BusCard().createPdf(RESULT);
          System.out.println("done");
      } 
  }


  ------------------------------------------------------------------------------
  Enable your software for Intel(R) Active Management Technology to meet the
  growing manageability and security demands of your customers. Businesses
  are taking advantage of Intel(R) vPro (TM) technology - will your software 
  be a part of the solution? Download the Intel(R) Manageability Checker 
  today! http://p.sf.net/sfu/intel-dev2devmar
  _______________________________________________
  iText-questions mailing list
  [email protected]
  https://lists.sourceforge.net/lists/listinfo/itext-questions

  iText(R) is a registered trademark of 1T3XT BVBA.
  Many questions posted to this list can (and will) be answered with a 
reference to the iText book: http://www.itextpdf.com/book/
  Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php
------------------------------------------------------------------------------
Enable your software for Intel(R) Active Management Technology to meet the
growing manageability and security demands of your customers. Businesses
are taking advantage of Intel(R) vPro (TM) technology - will your software 
be a part of the solution? Download the Intel(R) Manageability Checker 
today! http://p.sf.net/sfu/intel-dev2devmar
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to