I am creating an android mobile app, which takes data from user and saves to database. Now while generating report I am creating an pdf dynamically using iText pdf creator of version iTextg 5.5.10. I want to show text like this "मासिक दैनंदिनी"but it shows like this "मासकि दैनंदनिी". Also joined words looks like this "कोलहापुर" which should be "कोल्हापुर. We are using "NotoSansDevanagari-Regular.ttf" fontfor marathi font.
We have tried couple of things listed below,
1. We tried pdf box pdf creator but having same issue.
2. Tried different fonts like mangal.ttf, krutidev.ttf.
3. tried unicode fonts using jar files.
I am creating custom font object like this,
BaseFont bf_mar = BaseFont.createFont("assets/NotoSansDevanagari-
Regular.ttf", BaseFont.IDENTITY_H,true);
Font marathi = new Font(bf_mar , 20,Font.NORMAL);
And applying the font to paragraph like this,
Paragraph paragraph = new Paragraph("मासिक दैनंदिनी",marathi);
paragraph.setSpacingAfter(5);
paragraph.setSpacingBefore(5);
document.add(paragraph);
I expect the text should appear like this "मासिक दैनंदिनी". Also joined words looks like this "कोलहापुर" which should be "कोल्हापुर
Try without the use of fonts if it is not compulsory to use the exact font in the report.
Related
I have a problem when adding characters such as "Č" or "Ć" while generating a PDF. I'm mostly using paragraphs for inserting some static text into my PDF report. Here is some sample code I used:
var document = new Document();
document.Open();
Paragraph p1 = new Paragraph("Testing of letters Č,Ć,Š,Ž,Đ", new Font(Font.FontFamily.HELVETICA, 10));
document.Add(p1);
The output I get when the PDF file is generated, looks like this: "Testing of letters ,,Š,Ž,Đ"
For some reason iTextSharp doesn't seem to recognize these letters such as "Č" and "Ć".
THE PROBLEM:
First of all, you don't seem to be talking about Cyrillic characters, but about central and eastern European languages that use Latin script. Take a look at the difference between code page 1250 and code page 1251 to understand what I mean. [NOTE: I have updated the question so that it talks about Czech characters instead of Cyrillic.]
Second observation. You are writing code that contains special characters:
"Testing of letters Č,Ć,Š,Ž,Đ"
That is a bad practice. Code files are stored as plain text and can be saved using different encodings. An accidental switch from encoding (for instance: by uploading it to a versioning system that uses a different encoding), can seriously damage the content of your file.
You should write code that doesn't contain special characters, but that use a different notations. For instance:
"Testing of letters \u010c,\u0106,\u0160,\u017d,\u0110"
This will also make sure that the content doesn't get altered when compiling the code using a compiler that expects a different encoding.
Your third mistake is that you assume that Helvetica is a font that knows how to draw these glyphs. That is a false assumption. You should use a font file such as Arial.ttf (or pick any other font that knows how to draw those glyphs).
Your fourth mistake is that you do not embed the font. Suppose that you use a font you have on your local machine and that is able to draw the special glyphs, then you will be able to read the text on your local machine. However, somebody who receives your file, but doesn't have the font you used on his local machine may not be able to read the document correctly.
Your fifth mistake is that you didn't define an encoding when using the font (this is related to your second mistake, but it's different).
THE SOLUTION:
I have written a small example called CzechExample that results in the following PDF: czech.pdf
I have added the same text twice, but using a different encoding:
public static final String FONT = "resources/fonts/FreeSans.ttf";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
Font f1 = FontFactory.getFont(FONT, "Cp1250", true);
Paragraph p1 = new Paragraph("Testing of letters \u010c,\u0106,\u0160,\u017d,\u0110", f1);
document.add(p1);
Font f2 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, true);
Paragraph p2 = new Paragraph("Testing of letters \u010c,\u0106,\u0160,\u017d,\u0110", f2);
document.add(p2);
document.close();
}
To avoid your third mistake, I used the font FreeSans.ttf instead of Helvetica. You can choose any other font as long as it supports the characters you want to use. To avoid your fourth mistake, I have set the embedded parameter to true.
As for your fifth mistake, I introduced two different approaches.
In the first case, I told iText to use code page 1250.
Font f1 = FontFactory.getFont(FONT, "Cp1250", true);
This will embed the font as a simple font into the PDF, meaning that each character in your String will be represented using a single byte. The advantage of this approach is simplicity; the disadvantage is that you shouldn't start mixing code pages. For instance: this won't work for Cyrillic glyphs.
In the second case, I told iText to use Unicode for horizontal writing:
Font f2 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, true);
This will embed the font as a composite font into the PDF, meaning that each character in your String will be represented using more than one byte. The advantage of this approach is that it is the recommended approach in the newer PDF standards (e.g. PDF/A, PDF/UA), and that you can mix Cyrillic with Latin, Chinese with Japanese, etc... The disadvantage is that you create more bytes, but that effect is limited by the fact that content streams are compressed anyway.
When I decompress the content stream for the text in the sample PDF, I see the following PDF syntax:
As I explained, single bytes are used to store the text of the first line. Double bytes are used to store the text of the second line.
You may be surprised that these characters look OK on the outside (when looking at the text in Adobe Reader), but don't correspond with what you see on the inside (when looking at the second screen shot), but that's how it works.
CONCLUSION:
Many people think that creating PDF is trivial, and that tools for creating PDF should be a commodity. In reality, it's not always that simple ;-)
If you are using the FontProvider, I managed to solve the display of the special characters by setting the registerShippedFreeFonts parameter to true:
FontProvider dfp = new DefaultFontProvider(true, true, false);
See also: https://itextpdf.com/en/resources/books/itext-7-converting-html-pdf-pdfhtml/chapter-6-using-fonts-pdfhtml
Im using Font Awesome in my website and I'm I built a website API that my android application suppose to read.
In some labels there are font awesome along with a text.
I successfully implemented Font Awesome in my application but it works for me only when it's hardcoded or as string inside the XML file.
My problem is that I'm reading the font awesome from an API and I'm setting the text with TextBox.setText() method.
I'm facing really strange problem:
holder.gamesTitleTextView.setText("\uf135 " + gamesTitle); // Hardcoded, works
holder.gamesTitleTextView.setText(R.string.fa_icon_rocket + gamesTitle); //From saved string, does not work (returns some numbers)
Saved string is: <string name="fa_icon_rocket">\uf135</string>
Since I have abbility to change my API, if my API contains the font awesome string, \uf135, it shows it as a text and not as font-awesome icon.
Anyone has any way to solve it and use font awesome with the setText() method from a web API?
Please use like below in java code, its working fine for me:
public static final char FA_ICON_ROCKET= "\uf135";
My project isn't showing any portuguese characters. When I try to type a word like "Não" it returns Não".
The funny thing is that when I get the string from res/string.xml, it shows the word correctly.
Any idea why?
Things I've tried so far and did not work out:
File -> Settings -> Editor -> File Encondings, I've changed everything to UTF-8 and others, rebuild/cleaned the project, and it kept the same.
EDIT:
I can upload a video on youtube showing it, if it helps with the solution!
There goes an image of what is happening:
My file build.gradle had this line:
compileOptions.encoding = 'ISO-8859-1'
Because of that I wasn't able to change anything. Now it's fixed. :)
This is really hard to explain why is it is, I had same with russian characters, but only on SOME devices. I've just checked to do same as you on Genymotion and it displays correctly... From my investigation it is up to each device how to display given characters, but also I assume it could happen because Android knows how to works with Resources, but doesn't with Strings from code. When you create folders for different languages you don't say that default must be English. So system gonna detect and display. I'm not sure 100%, but this is what I understood from doc.
Anyways, for using String object in TextView from code and displaying foreign (from English) languages we have just 2 options:
1) Add .ttf file for particular text/Unicode
2) html format
Example for first option:
String s="(Mouy t'ngai) (១ ថ្ងៃ)";
TextView text_view1 = null;
text_view1 = (TextView) findViewById(R.id.textView2);
Typeface font= Typeface.createFromAsset(getAssets(), "khmerOS.ttf");
text_view1.setTypeface(font);
text_view1.setText(s);
// you can use different type of .ttf like
TAU_BHON.TTF
molten.ttf
arialuni.ttf
Example of Second option:
tv.setText(Html.fromHtml("\\u27A1");
Source.
P.S. If I missed something, please fill free to notice that.
I'm trying to generate PDF file that contains Hebrew words.
When file is created all the Hebrew letters are deleted and only the English one are
in the file.
Can any one help me with this problem?
I'm using iText 5.4.3
This is my code when i add a new paragraph
Paragraph p0 = new Paragraph("טופס קריאת שירות");
BaseFont unicode = BaseFont.createFont("assets/fonts/arial.ttf", BaseFont.IDENTITY_H , BaseFont.EMBEDDED);
Font fontHebrew = new Font(unicode, 12, Font.NORMAL);
p0.setAlignment(Paragraph.ALIGN_CENTER);
p0.setFont(fontHebrew);
//add paragraph to document
doc.add(p0);
Thank's.
The code you added is wrong on many levels.
You're adding Hebrew characters in source code. These characters can get lost if the file is saved using the wrong encoding, if the file is compiled using the wrong encoding, etc... Use the Unicode notation instead of actual characters.
Maybe not wrong, but to be checked: you're using arial.ttf, shouldn't you be using arialuni.ttf? Also: make sure you pack the ttf in your APK (you wouldn't be the first to forget to ship a resource).
I can't read Hebrew, but I know it's written from right to left. RTL isn't supported in the Paragraph class, only in PdfPCell and ColumnText.
See the examples to find out how it's done: say_peace.pdf is done using a table; ligatures_2.pdf is done using a column (the second example is in Arabic, but it's the same principle as Hebrew).
I created a sample application using Android TextView with shared preferences. In my application i have a sample textview which contains some text (Eg. "Android Font Text"), and also a had set of fonts in my assets folder. Here my requirement is, when i touch the textview, a sample dialog will popup in this pop up i shows the list of font's in assets folder. When i select any one of the font it was changed successfully, it persist only in that particular activity. When i back to next activity the text view remains in default style. Here how can i achieve shared preferences for fonts. I can't pass the type face object through the editor. I used like this,
Typeface font = Typeface.createFromAsset(getAssets(),"digit_fonts/AKEI____.TTF");
font.isBold();
mTextView.setTypeface(font);
font_editor = font_pref.edit();
font_editor.putString("font_style", font.toString());
font_editor.commit();
Unfortunately it won't works, how can i acheive, if any one solve my problem. Thanks in Advance.
So, you're trying to load a font from sharedpreferences whenever an activity is started?
If I'm understanding you correctly, what you need to do is
1) Save the file name of the font the user has chosen (i.e., "digit_fonts/AKEI__.TTF")
2) Load the font using the file name saved in shared preferences, for example:
Typeface font = Typeface.createFromAsset(getAssets(),font_pref.getString("font_style", default_font_filename));
Then apply the font as you normally would.
EDIT:
To load:
font_style = font_pref.getString("font_style", null);
if (font_style!=null){
Typeface font = Typeface.createFromAsset(getAssets(),font_style);
font.isBold();
mTextView.setTypeface(font);
}
To save:
font_editor = font_pref.edit();
font_editor.putString("font_style", font_style);
font_editor.commit();
As you can see, font_style is actually the file location of your font (the way I've coded it above). When the user chooses a font, you alter font_style then save it to preferences.