I need to use a particular font to make a document because it contains specia charactesrs such as "Đ" that are not supported by the normal fonts iText comes with.
So, I made this:
BaseFont CROACIA = BaseFont.createFont("C:\\FreeSans.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font CROATA = new Font(CROACIA, 12);
It works fine and my "Đ" problem is solved, the thing is that I cant set it to be bold
I tried to make a different font with "BOLD" setting like this_
Font CROATABOLD = new Font(CROACIA, 12, BOLD);
The code does not seems erroneous but when I apply it to a paragraph it just not work, the font seems as normal as usual
FreeSans and FreeSansBold are different fonts of the same family. You provide a path to the FreeSans.ttf font program and as a result iText can use the regular font in the FreeSans family. If you want to use the bold font, you need to provide a path to FreeSansBold.ttf which is a different font program for a font in the same family.
This is shown in the FreeSansBold example:
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static final String FONTBOLD = "resources/fonts/FreeSansBold.ttf";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
Paragraph p = new Paragraph("FreeSans regular: \u0110", font);
document.add(p);
BaseFont bfbold = BaseFont.createFont(FONTBOLD, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font bold = new Font(bfbold, 12);
p = new Paragraph("FreeSans bold: \u0110", bold);
document.add(p);
document.close();
}
We have two different fonts FreeSans.ttf and FreeSansBold.ttf of the same family. One is the regular font; the other one is the bold font. If you look at the document properties of the result, free_sans_bold.pdf, you clearly see that two different fonts are at play:
Related
while using itext5 in android to display pdf from XHTML am trying to change the font size but it's not reflecting.
I would like to know the substitutes(or hack) for CSS as itext5 is not supporting CSS.
preparedText = output.toString("UTF-8");
list = XMLWorkerHelper.parseToElementList(preparedText, null);
// URL path =Thread.currentThread().getContextClassLoader().getResource("fontname");
// FontFactory.register(path.toString(), "test_font");
Font titleFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD,7f);
paragraph.setFont(titleFont);
paragraph.addAll(list);
publishProgress(88);
// write to document
document.open();
document.newPage();
Paragraph p= new Paragraph(paragraph);
p.setFont(titleFont);
document.add(p);
document.close();
The font you set in a paragraph applies to all text added to the paragraph afterwards, it does not change the previously added text. To set the font of the text you add to a paragraph in the constructor, there is a constructor that also accepts a font parameter.
Thus, instead of
Paragraph p= new Paragraph(paragraph);
p.setFont(titleFont);
use
Paragraph p = new Paragraph(paragraphText, titleFont);
or
Paragraph p = new Paragraph();
p.setFont(titleFont);
p.add(paragraphText);
In the previous version of itext (5.5.x) I used the BaseFont class as follows:
BaseFont bf = BaseFont.createFont ("Arial.ttf", BaseFont.WINANSI, true);
Then used the method getWidthPoint
bf.getWidthPoint (TEXT_EXAMPLE, fontSize);
But in the version of itext 7 I am not finding the BaseFont class and also some utility that allows me to get the withPoint of a certain text.
Any help is welcome.
To create a similar font in iText7, use:
PdfFont font = PdfFontFactory.createFont("Arial.ttf", PdfEncodings.WINANSI, true);
To get the width of a certain String, use:
float width = font.getWidth(TEXT_EXAMPLE, fontSize);
I am trying to use NOTO fonts (https://www.google.com/get/noto/) to display Chinese characters. Here is my sample code,a modified sample code from iText.
public void createPdf(String filename) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
//This is simple English Font
FontFactory.register("c:/temp/fonts/NotoSerif-Bold.ttf", "my_nato_font");
Font myBoldFont = FontFactory.getFont("my_nato_font");
BaseFont bf = myBoldFont.getBaseFont();
document.add(new Paragraph(bf.getPostscriptFontName(), myBoldFont));
//This is Chinese font
//Option 1 :
Font myAdobeTypekit = FontFactory.getFont("SourceHanSansSC-Regular", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//Option 2 :
/*FontFactory.register("C:/temp/AdobeFonts/source-han-sans-1.001R/OTF/SimplifiedChinese/SourceHanSansSC-Regular.otf", "my_hans_font");
Font myAdobeTypekit = FontFactory.getFont("my_hans_font", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);*/
document.add(Chunk.NEWLINE);
document.add(new Paragraph("高興", myAdobeTypekit));
document.add(Chunk.NEWLINE);
//simplified chinese
document.add(new Paragraph("朝辞白帝彩云间", myAdobeTypekit));
document.add(Chunk.NEWLINE);
document.add(new Paragraph("高兴", myAdobeTypekit));
document.add(new Paragraph("The Source Han Sans Traditional Chinese ", myAdobeTypekit));
document.close();
}
I have downloaded the fonts files on my machine. I am using two approaches
To use the equivalent font family in Adobe
Embed the otf file in pdf
Using approach 1, I would expect the Chinese characters to be displayed in pdf but English text is displayed and it is blank for Chinese characters.
Using approach 2, when I try embedding the fonts with pdf, which is not the path I would like to take, there is error in opening pdf.
Update :
If I look at this example http://itextpdf.com/examples/iia.php?id=214
and in this code
public void createPdf(String filename, boolean appearances, boolean font)
throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
writer.getAcroForm().setNeedAppearances(appearances);
TextField text = new TextField(writer, new Rectangle(36, 806, 559, 780), "description");
text.setOptions(TextField.MULTILINE);
if (font) {
BaseFont unicode =
BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
text.setExtensionFont(BaseFont.createFont());
ArrayList<BaseFont> list = new ArrayList<BaseFont>();
list.add(unicode);
text.setSubstitutionFonts(list);
BaseFont f= (BaseFont)text.getSubstitutionFonts().get(0);
System.out.println(f.getPostscriptFontName());
}
text.setText(TEXT);
writer.addAnnotation(text.getTextField());
// step 5
document.close();
}
I substitute, c:/windows/fonts/arialuni.ttf with C:/temp/fonts/NotoSansCJKtc-Thin.otf , I do not see the Chinese characters. The text to convert now is
public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n"
+ "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), "
+ "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), "
+ "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";
Clearly you are using the wrong font. I have downloaded the fonts from the link you posted. You are using NotoSerif-Bold.ttf, a font that does not support Chinese. However, the ZIP file also contains fonts with CJK in the font name. As described on the site you refer to, CJK stands for Chinese, Japanese and Korean. Use one of those CJK fonts and you'll be able to product Chinese text in your PDF.
Take a look at the NotoExample in which I use one of the fonts from the ZIP file you refer to. It creates a PDF that looks like this:
This is the code I used:
public static final String FONT = "resources/fonts/NotoSansCJKsc-Regular.otf";
public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n"
+ "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), "
+ "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), "
+ "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";
public static final String CHINESE = "\u5341\u950a\u57cb\u4f0f";
public static final String JAPANESE = "\u8ab0\u3082\u77e5\u3089\u306a\u3044";
public static final String KOREAN = "\ube48\uc9d1";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Paragraph p = new Paragraph(TEXT, font);
document.add(p);
document.add(new Paragraph(CHINESE, font));
document.add(new Paragraph(JAPANESE, font));
document.add(new Paragraph(KOREAN, font));
document.close();
}
You claim that Adobe Reader XI doesn't show the Chinese glyphs, but instead shows a "Cannot extract the embedded Font" message. I can not reproduce this [*]. I have even used Preflight in Adobe Acrobat as indicated here, but no errors were found:
[*] Update: this problem can be reproduced if you use iText 4.2.x, a version that was released by somebody unknown to iText Group NV. Please use iText versions higher than 5 only.
I have that part of code:
//Staff
// Title font
BaseFont titleBf = null;
try {
titleBf = BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.CP1252, BaseFont.EMBEDDED);
} catch (IOException e) {
System.out.println(e.getMessage());
}
com.itextpdf.text.Font titleFont = new com.itextpdf.text.Font(titleBf,24);
titleFont.setColor(new BaseColor(0, 0, 204));
/*
* Pdf creation
*/
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(fc.getSelectedFile() + ".pdf"));
document.open();
/*
* Title
*/
Paragraph p = new Paragraph("مرحبا بكم",titleFont);
p.setSpacingAfter(20);
p.setAlignment(1); // Center
document.add(p);
//Staff
document.close();
The output was empty.
Even when I tried to add simple pdfpcell, I got same result, here is the way:
com.itextpdf.text.Font fontNormal = FontFactory.getFont(
("arialuni.ttf"), BaseFont.CP1252, BaseFont.EMBEDDED,
8, com.itextpdf.text.Font.NORMAL);
Chunk chunkArabic = new Chunk("مرحبا العالم", fontNormal);
infoTab.addCell(new PdfPCell(new Phrase(chunkArabic)));
document.add(infoTab);
Different problems may be at play.
Encoding: You are putting Arabic text in your source code. If you save your Java file using the wrong encoding, that text will become corrupt. If you compile your Java file using the wrong encoding, that text will become corrupt. And so on. I hope you get the idea. Store literal text like this: "\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628" to avoid problems like this.
As documented in Chapter 11 of my book, right-to-left writing and Arabic ligatures are only supported in the context of PdfPCell and ColumnText. I see that you're using PdfPCell, but I don't see you using cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL); anywhere. That's wrong.
You are using an object fontNormal, but you're not telling us which font you're actually using. For instance: if you're using the standard Type1 font Helvetica, no text will show up, because Helvetica doesn't know how to render Arabic text.
Amendment: You've now updated your question, showing that you're using "arialuni.ttf". However, you are using the wrong encoding: CP1252 is the encoding for the Latin Alphabet. You should use BaseFont.IDENTITY_H.
Update: looking at the update of your question as well as the extra comment, I see two major errors.
You are assuming that the name of the font is "arialuni.ttf". That's the font file, but not the font name. Also: you are asking the FontFactory for this font, but did you tell the FontFactory where to look for fonts? Are you sure the FontFactory can locate c:/windows/fonts/ or whatever directory the file arialuni.ttf is stored.
You are declaring a font, but you're not using it: Paragraph p = new Paragraph("\u062D\u064A\u0633\u0648"); creates a Paragraph using the default font.
This is code that works:
BaseFont bf = BaseFont.createFont(
"c://windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 8);
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("\u062D\u064A\u0633\u0648", font));
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
table.addCell(cell);
document.add(table);
The result looks like this (zoomed in):
How can I set a font type by giving it's path in iText
I tried this one. But it is not working.
Font ff = new Font(Font.getFamily("C:/Windows/Fonts/Harry Potter Regular.ttf"));
ff.setSize(12);
First create a BaseFont with required font file:
BaseFont bf = BaseFont.createFont(src, myEncoding, BaseFont.EMBEDDED);
Then create the actual font with size and style attributes, for example:
com.itextpdf.text.Font f = new Font(bf, 14.5f, Font.ITALIC | Font.BOLD);
The getFamily method you used, refers to the Acrobat predefined fonts: FontFactory.COURIER, FontFactory.HELVETICA, FontFactory.TIMES_ROMAN, FontFactory.SYMBOL, FontFactory.ZAPFDINGBATS.