If anybody can explain this I would be grateful, but I got this to work by
using FontSelector
to generate the font instead of the other approaches I tried.
What was very odd about using FontFactory.getFont() was that both my debugger
and
BaseFont.getPostscriptFontName() indicated that the font in the document was
different
from what the actual PDF document told me in a text editor with
CompressionLevel set to 0;
the document itself said it used Helvetica, but my debugger told me the value
of getFont()
was sanskrit2003.
Here is the working method, if anybody later on has a similar problem as mine:
protected void drawText(PdfContentByte pcb)
throws IOException, DocumentException {
FontSelector fontSel = new FontSelector();
BaseFont bf_san2003;
Font sans;
FontFactory.registerDirectories();
bf_san2003 =
BaseFont.createFont("c:/windows/fonts/sanskrit2003.ttf",BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
sans = new Font(bf_san2003,16,Font.NORMAL);
fontSel.addFont(sans);
readfile();
pcb.saveState();
for (int i=0;i<10;i++){
ColumnText ct = new ColumnText(pcb);
String test =front.get(i);
Phrase myText;
myText = fontSel.process(test);
myText.setFont(sans);
ct.setSimpleColumn(myText,
VRT_2BY5_BCARDS[i][0],
VRT_2BY5_BCARDS[i][1],
VRT_2BY5_BCARDS[i][2],
VRT_2BY5_BCARDS[i][3],
24,
Element.ALIGN_CENTER);
ct.go();
}
pcb.restoreState();
}
----- Original Message -----
From: "John Kilbourne" <[email protected]>
To: "Post all your questions about iText here"
<[email protected]>
Sent: Tuesday, March 29, 2011 8:58:12 PM GMT -05:00 US/Canada Eastern
Subject: Re: [iText-questions] writing text with a specific UTF-8 font
I tried changint the font to arial unicode ms, which I verified correctly
displays the text being loaded to the PDF. iText sees that font too...
PostScriptName ArialUnicodeMS
Encoding Identity-H
but still does not print the text.
----- Original Message -----
From: "John Kilbourne" <[email protected]>
To: "Post all your questions about iText here"
<[email protected]>
Sent: Tuesday, March 29, 2011 8:42:30 PM GMT -05:00 US/Canada Eastern
Subject: Re: [iText-questions] writing text with a specific UTF-8 font
Interesting. I took your suggestion and altered the method accordingly. It
looks like iText has the right font, but the PDF output is the same:
System.out.println("PostScriptName " + bf.getPostscriptFontName());
System.out.println("Encoding " + bf.getEncoding());
--->PostScriptName Sanskrit2003
--->Encoding Identity-H
protected void drawText(PdfContentByte pcb)
throws IOException, DocumentException {
---> FontFactory.registerDirectories();
---> sans=FontFactory.getFont("Sanskrit 2003", "Identity-H");
readfile();
pcb.saveState();
---> BaseFont bf = sans.getBaseFont();
---> System.out.println("PostScriptName " +
bf.getPostscriptFontName());
---> System.out.println("Encoding " + bf.getEncoding());
for (int i=0;i<10;i++){
ColumnText ct = new ColumnText(pcb);
//String test = "read this";
//test.concat(front.get(i));
String test =front.get(i);
test.concat("read this");
Phrase myText;
//Phrase myText = new Phrase(front.get(i));
myText = new Phrase(test);
myText.setFont(sans);
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();
}
----- Original Message -----
From: "Mark Storer" <[email protected]>
To: "Post all your questions about iText here"
<[email protected]>
Sent: Tuesday, March 29, 2011 8:09:19 PM GMT -05:00 US/Canada Eastern
Subject: Re: [iText-questions] writing text with a specific UTF-8 font
Here's the relevant portion of your content stream:
BT
1 0 0 1 144.41 707 Tm
/F1 12 Tf
( )Tj
ET
BT
1 0 0 1 396.41 707 Tm
/F1 12 Tf
( )Tj
ET
...
So what's F1? It's Helvetica, with WinAnsiEncoding.
Warning: BaseFont.createFont will always return SOMETHING. If it
doesn't recognise the font name/path, it'll generally fall back to one
of the Base 14 fonts (Helvetica). And the base 14 fonts don't support
Idenity-H encoding, so you're back at WinAnsiEncoding.
Check the return value of your CreateFont call. I'd be astonished to
find you're getting what you expect.
PS: I like FontFactory.registerDirectories() &
FontFactory.getFont(familyName, encoding). If registerDirectories takes
to long for your taste, you can register individual files or directories
as well.
--Mark Storer
Senior Software Engineer
Cardiff.com
import legalese.Disclaimer;
Disclaimer<Cardiff> DisCard = null;
> -----Original Message-----
> From: John Kilbourne [mailto:[email protected]]
> Sent: Tuesday, March 29, 2011 4:45 PM
> To: Post all your questions about iText here
> Subject: Re: [iText-questions] writing text with a specific UTF-8 font
>
> Here is the PDF output showing the empty boxes.
>
>
> If I uncomment the lines below with "----->" and comment the
> lines with "xxxxxx->" (i.e. concatenate in a different
> order), the document does print "read this" in all the boxes,
> but does not print the text that is concatenated after "read
> this" (the UTF-8 sanskrit text).
>
>
> protected void drawText(PdfContentByte pcb)
> throws IOException, DocumentException {
>
> FontFactory.register("c:/windows/fonts/sanskrit2003.ttf", "sanskrit");
> sans = FontFactory.getFont("sanskrit",
> "Identity-H", true);
> //sans=new Font(BaseFont.createFont(
> //
> "c:/windows/fonts/sanskrit2003.ttf",BaseFont.IDENTITY_H,
> BaseFont.EMBEDDED ));
> readfile();
> pcb.saveState();
> for (int i=0;i<10;i++){
> ColumnText ct = new ColumnText(pcb);
> --------> //String test = "read this";
> --------> //test.concat(front.get(i));
> xxxxxxx-> String test =front.get(i);
> xxxxxxx-> test.concat("read this");
> Phrase myText;
> myText = new Phrase(test);
> myText.setFont(sans);
> 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();
> }
>
> ----- Original Message -----
> From: "Mark Storer" <[email protected]>
> To: "Post all your questions about iText here"
> <[email protected]>
> Sent: Tuesday, March 29, 2011 7:37:19 PM GMT -05:00 US/Canada Eastern
> Subject: Re: [iText-questions] writing text with a specific UTF-8 font
>
> Can we see your PDF output?
>
> --Mark Storer
> Senior Software Engineer
> Cardiff.com
>
> import legalese.Disclaimer;
> Disclaimer<Cardiff> DisCard = null;
>
>
>
> > -----Original Message-----
> > From: John Kilbourne [mailto:[email protected]]
> > Sent: Tuesday, March 29, 2011 3:43 PM
> > To: Post all your questions about iText here
> > Subject: Re: [iText-questions] writing text with a specific
> UTF-8 font
> >
> > I tried concatenating a simple text piece to the UTF-8
> string to see
> > if at least the concatenated part would show, but still none of the
> > text is visible in the PDF:
> >
> > protected void drawText(PdfContentByte pcb)
> > throws IOException, DocumentException {
> >
> > FontFactory.register("c:/windows/fonts/sanskrit2003.ttf",
> "sanskrit");
> > sans = FontFactory.getFont("sanskrit",
> "Identity-H", true);
> > readfile();
> > pcb.saveState();
> > for (int i=0;i<10;i++){
> > ColumnText ct = new ColumnText(pcb);
> > --> String test = front.get(i);
> > --> test.concat("read this");
> > --> Phrase myText = new Phrase(test);
> > //Phrase myText = new Phrase(front.get(i));
> > --> myText = new Phrase(test);
> > myText.setFont(sans);
> > 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();
> > }
> > ----- Original Message -----
> > From: "John Kilbourne" <[email protected]>
> > To: "Post all your questions about iText here"
> > <[email protected]>
> > Sent: Tuesday, March 29, 2011 6:33:51 PM GMT -05:00
> US/Canada Eastern
> > Subject: Re: [iText-questions] writing text with a specific
> UTF-8 font
> >
> > Thank you, but that did not do the trick; I still get only
> blank cards
> > printed.
> > Do I need to embed the font if my system already has the font?
> >
> > John
> > ----- Original Message -----
> > From: "Paulo Soares" <[email protected]>
> > To: "Post all your questions about iText here"
> > <[email protected]>
> > Sent: Tuesday, March 29, 2011 5:33:54 PM GMT -05:00
> US/Canada Eastern
> > Subject: Re: [iText-questions] writing text with a specific
> UTF-8 font
> >
> >
> >
> > 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
> >
> >
> > --------------------------------------------------------------
> > ----------------
> > 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
> >
> >
>
> --------------------------------------------------------------
> ----------------
> 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
------------------------------------------------------------------------------
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
------------------------------------------------------------------------------
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