when I want to use a font is iText I do the following:
protected final static Font FONT_SIZE_11_BOLD = new Font(Font.HELVETICA, 11f, Font.BOLD);
and then I can use it whereever I want, as follows:
monthSize11 = new Chunk(month, FONT_SIZE_11_BOLD);
I want to use Arial instead of HELVETICA, but Arial is not directly available.
I mean, I cannot do
new Font(Font.ARIAL, 11f, Font.BOLD);
because Arial is not defined at the Font class, but the Arial.ttf file is at my System under C:\WINDOWS\Fonts.
The question is how I can bind the Arial.ttf file to iText and how can I use it.
Many thnaks in advance.
EDIT: I would like to use own fonts. I mean, I have a file called "myCompany.ttf" where own fonts have been defined and at some places I must use. The problem is not only with Arial.
BaseFont base = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.WINANSI);
Font font = new Font(base, 11f, Font.BOLD);
....
Read more here.
Load it from inside the JAR using a leading slash; otherwise, use the absolute path of your font (C:\...\fonts\Sansation_Regular.ttf). For example:
Font font = FontFactory.getFont("/fonts/Sansation_Regular.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.NORMAL, BaseColor.BLACK);
BaseFont baseFont = font.getBaseFont();
The relative path of the font is: 'src/main/resources/fonts'
Using Itext 5.4.5
Example code
Use BaseFont.createFont to create a new Font object.
You can pass any Type1 or TTF font. You will just have to ensure your font file is distributed alongwith.
Refer
BaseFont API
Creating custom fonts using itext is simple
I had written code for the same below
Will definitely help someone
public class CustomFontStyle {
public static void main(String[] args) {
// creation of the document with a certain size and certain margins
// may want to use PageSize.LETTER instead
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
// creation of the different writers
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("CustomFontsStyle.pdf"));
final String NEWLINE = "\n";
document.open();
Phrase phrase = new Phrase();
BaseFont baseFont3 = BaseFont.createFont("Xenotron.ttf", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
Font font2 = new Font(baseFont3, 12);
document.add(new Paragraph("Custom Xenotron Font: ", font2));
phrase.add(NEWLINE);
document.add(phrase);
document.close();
}
catch (Exception ex) {
System.err.println(ex.getMessage());
}
}
}
Related
I am trying to use pdfbox to add text watermark to pdf, but I encountered a problem when adding ttc format fonts.
Font2D font2D = FontUtilities.getFont2D(font);
String fontEnName = font2D.getFamilyName(Locale.ENGLISH);
PDFont pdfFont = PDType0Font.load(doc, new TrueTypeCollection(fontFile).getFontByName(fontEnName), true);
PDPageContentStream contents = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true);
contents.beginText();
contents.setFont(pdfFont, fontHeight);
First of all, Font2D and FontUtilities are internal classes, which may be deleted in the future, which is not a good solution. Second, the obtained fontEnName is not necessarily the parameter required by getFontByName, maybe fontEnName is
simsum, but the parameter of getFontByName needs SimSun, and the two names are not matching,there are other ttc Font related example, is there any other good solution?
This shows the names:
TrueTypeCollection ttc = new TrueTypeCollection(new File("c:/windows/fonts/batang.ttc"));
ttc.processAllFonts(new TrueTypeCollection.TrueTypeFontProcessor()
{
#Override
public void process(TrueTypeFont ttf) throws IOException
{
System.out.println(ttf.getName());
}
});
ttc Font:
Font font = Font.createFont(Font.TRUETYPE_FONT, fb);
font = font.deriveFont(Font.PLAIN, fontHeight);
String fontEnName = font.getPSName();
PDFont pdfFont = PDType0Font.load(doc, new TrueTypeCollection(fontFile).getFontByName(fontEnName), true);
I am using iText PDF to create PDF files in android. I need to write the '₹' symbol in the PDF.
I have the following code:-
public static final String FONT1 = "/main/assets//PlayfairDisplay-Regular.ttf";
public static final String FONT2 = "/main/assets/PT_Sans-Web-Regular.ttf";
public static final String FONT3 = "/main/assets/FreeSans.ttf";
public static final String RUPEE = "The Rupee character \u20B9 and the Rupee symbol \u20A8";
These are declared as class variables.
I have a function createPDF() which writes to the PDF document.
I have the following lines of code in the function:-
File pdfFile = new File(filePath);
OutputStream output = new FileOutputStream(pdfFile);
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, output);
document.open();
Font f1 = FontFactory.getFont(FONT1, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f2 = FontFactory.getFont(FONT2, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f3 = FontFactory.getFont(FONT3, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f4 = FontFactory.getFont(FONT3, BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
document.add(new Paragraph(RUPEE, f1));
document.add(new Paragraph(RUPEE, f2));
document.add(new Paragraph(RUPEE, f3));
document.add(new Paragraph(RUPEE, f4));
document.close();
However neither the font gets reflected in the PDF nor the Rupee Symbol.
My PDF looks as below:-
I have followed steps using the below links:-
iText Developers Tutorial
and StackOverflow Question on where to place Assets folder
I am aware of another similar question on SO:-
Rupee symbol is not showing in android
however this also has not helped me.
Am I doing anything wrong here ? Are my fonts placed in the wrong location?
Try,
Remove all /main/ just keep like assets/PlayfairDisplay-Regular.ttf so on... and in the first font you have // remove that one too and try
when I want to use a font is iText I do the following:
protected final static Font FONT_SIZE_11_BOLD = new Font(Font.HELVETICA, 11f, Font.BOLD);
and then I can use it whereever I want, as follows:
monthSize11 = new Chunk(month, FONT_SIZE_11_BOLD);
I want to use Arial instead of HELVETICA, but Arial is not directly available.
I mean, I cannot do
new Font(Font.ARIAL, 11f, Font.BOLD);
because Arial is not defined at the Font class, but the Arial.ttf file is at my System under C:\WINDOWS\Fonts.
The question is how I can bind the Arial.ttf file to iText and how can I use it.
Many thnaks in advance.
EDIT: I would like to use own fonts. I mean, I have a file called "myCompany.ttf" where own fonts have been defined and at some places I must use. The problem is not only with Arial.
BaseFont base = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.WINANSI);
Font font = new Font(base, 11f, Font.BOLD);
....
Read more here.
Load it from inside the JAR using a leading slash; otherwise, use the absolute path of your font (C:\...\fonts\Sansation_Regular.ttf). For example:
Font font = FontFactory.getFont("/fonts/Sansation_Regular.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.NORMAL, BaseColor.BLACK);
BaseFont baseFont = font.getBaseFont();
The relative path of the font is: 'src/main/resources/fonts'
Using Itext 5.4.5
Example code
Use BaseFont.createFont to create a new Font object.
You can pass any Type1 or TTF font. You will just have to ensure your font file is distributed alongwith.
Refer
BaseFont API
Creating custom fonts using itext is simple
I had written code for the same below
Will definitely help someone
public class CustomFontStyle {
public static void main(String[] args) {
// creation of the document with a certain size and certain margins
// may want to use PageSize.LETTER instead
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
// creation of the different writers
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("CustomFontsStyle.pdf"));
final String NEWLINE = "\n";
document.open();
Phrase phrase = new Phrase();
BaseFont baseFont3 = BaseFont.createFont("Xenotron.ttf", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
Font font2 = new Font(baseFont3, 12);
document.add(new Paragraph("Custom Xenotron Font: ", font2));
phrase.add(NEWLINE);
document.add(phrase);
document.close();
}
catch (Exception ex) {
System.err.println(ex.getMessage());
}
}
}
I have a web application that will generate a PDF from data and allow a user to download. We have a custom font so I am registering four font files (regular, bold, italic, and bold italic ttf files).
Is this necessary? Can I just use the regular font file and set the weight/styles through the font class or do I need all the files? It looks like it works ok but I am still learning.
What is a good strategy or pattern for managing these different fonts to put into my documents? Has anyone done much with this from a Java perspective?
You're not telling us how you're registering your fonts, nor how you're using them, so let me tell you how I would work (I'm the original developer of iText). If you're using those 4 fonts, the best way is to register them like this:
BaseFont bfRegular = BaseFont.createFont(regularTTF, BaseFont.IDENTITY_H, BaseFont_EMBEDDED);
BaseFont bfBold = BaseFont.createFont(boldTTF, BaseFont.IDENTITY_H, BaseFont_EMBEDDED);
BaseFont bfItalic = BaseFont.createFont(italicTTF, BaseFont.IDENTITY_H, BaseFont_EMBEDDED);
BaseFont bfBoldItalic = BaseFont.createFont(boldItalicTTF, BaseFont.IDENTITY_H, BaseFont_EMBEDDED);
I'd use these BaseFont instances whenever adding content using low-level operations.
Then I would do:
Font fRegular = new Font(bfRegular, 12);
Font fBold = new Font(bfBold, 12);
Font fItalic = new Font(bfItalic, 12);
Font fBoldItalic = new Font(bfBoldItalic, 12);
I'd use these fonts whenever I need to create a high-level object, such as a Paragraph.
Of course, I may also need something like this:
Font fRegularSmall = new Font(bfRegular, 9);
Font fBoldSmall = new Font(bfBold, 9);
Font fItalicSmall = new Font(bfItalic, 9);
Font fBoldItalicSmall = new Font(bfBoldItalic, 9);
Font fRegularBig = new Font(bfRegular, 20);
Font fBoldBig = new Font(bfBold, 20);
Font fItalicBig = new Font(bfItalic, 20);
Font fBoldItalicBig = new Font(bfBoldItalic, 20);
Usually, I create a helper class in which I create the BaseFont objects only once (they need to be reused), and I create getters for the Font objects.
I need help with iText I look at some Google result and some here but don't find anything that work for me. I need to use polish character in my pdf but i got nothing for no. Here is a code that I think is important if need something else write in comment:
private static Font bigFont = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
another
Paragraph par = new Paragraph(Łabadzak, bigFont);
Can any1 tell me what to do to make that Ł visible in pdf and other polish character
UPDATE
I fund this but dunno how to use it for my project
Polish character in itext PDF
You need a unicode font. Here is an example:
BaseFont bf = BaseFont.createFont("arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Paragraph p = new Paragraph("Şinasi ıssız ile ağaç", new Font(bf, 22));
document.add(p);
http://abdullahakay.blogspot.com/2011/11/java-itext-unicode.html
EDIT:
Here, the font file name arialuni.tff is a static resource directly under /src/main/resources/ and can be any Unicode Font File of your choice. Here's a list of free Unicode Font Files available online.
It depends on used font and encoding. i found something like this:
http://itext-general.2136553.n4.nabble.com/Polish-National-Characters-are-not-getting-displayed-in-the-PDF-created-by-iTExt-td2163833.html
There is example like this:
BaseFont bf = BaseFont.createFont("c:/windows/fonts/arial.ttf",
BaseFont.CP1250, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
String polish = "\u0104\u0105\u0106\u0107\u0118\u0119";
document.add(new Paragraph(polish, font));
Remember that some fonts does not contain polish national characters.
In case you are using com.itextpdf.kernel package you can use any encoding which is not present in PdfEncodings class
PdfWriter writer ;
writer = new PdfWriter( dest ) ;
PdfDocument pdf = new PdfDocument( writer ) ;
Document document = new Document( pdf ) ;
FontProgram fontProgram = FontProgramFactory.createFont( ) ;
PdfFont font = PdfFontFactory.createFont( fontProgram, "Cp1254" ) ;
document.setFont( font );
for turkish characters I used "Cp1254"