RIGHT TO LEFT when creating PDF in android - android

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).

Related

Marathi font not rendered correctly?

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.

Android iTextPdf utf-8 not working [duplicate]

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

Android Studio messes up brazilian-portuguese characters

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.

How can I put utf-16 characters in Android string resource?

I want to use Emojis in my app's strings. All strings reside, of course, in strings.xml
The problem is that not all Emojis are 16 bit friendly. Some Emojis can be represented as "normal" 16 bit hex: '\u26FF' but some are 32 bit hexes (UTF-16), usually represented as: '\x1F600'. I have no problem dealing with those inside the app, in code. But the strings.xml resource file is UTF8 encoded, and does not deal properly with non 16 bit escape chars.
I tried using '\x1F600' - because I saw that '\u26FF' works just fine. But it seems not to devour the 'x' escape char. Nor did it like the regexp notation '\x{1F600}'
So I ended up using a string placeholder '%1$s' and filling in the Emoji in code like this:
// greeting_3 is defined as: "hello there %1$s!"
String s = context.getString(R.string.greeting_3, "😜");
// OR:
String s = context.getString(R.string.greeting_3, new String(Character.toChars(0x1F61C)));
This is not a very elegant solution... is there a proper way to put 32 bit UTF-8 chars in strings.xml ?
But the strings.xml resource file is UTF8
If it's UTF-8 encoded, you can put your emojis directly. But then you risk that your editor or another piece of software destroys them.
If you are putting them in XML, you can try using XML entities: 😀, I'm not sure how well Android supports them though.
You can also use surrogate pairs: convert the emoji to UTF-16 and use standard \u escape. You can for example check out this page, it even tells you how to create a string litaral in Java: http://www.fileformat.info/info/unicode/char/1F600/index.htm
😜 → U+1F600 → "\uD83D\uDE00"
The easiest way it just copying and pasting the emoji, it works from Android Studio 3.0 and newer
Add the resource like follows:
<string name="string_title">This is a emoji example <U+1F642></string>
In Android Studio 3.0 you can copy and paste an emoji:
And here how it looks:

Facebook Text Sharing With Custom Fonts

My Application require is share text with facebook.I'm able to share text using this code.
Here I'm giving parameters like this:
Bundle myParames = new Bundle();
myParames.putString("message", message);asyncFacebook.request("me/feed", myParames,"POST", new FacebookPostListener(), null);
But I want share the text every letter with corresponding font like,
if myString = "stack"; then,
String newString = null;
newString.replace("s", "Ⓢ");
newString.replace("t", "Ⓣ");
newString.replace("a", "Ⓐ");
newString.replace("c", "Ⓒ");
newString.replace("k", "Ⓚ");
Then I'm able to share my text with replaced fonts.
But I want apply font from .ttf file and then share.
I tried SpannableString, But it can't convert into .string().
So I need to change library?
Those characters you are doing your replacements with are no font, but characters. Unicode characters, to be precise.
Enclosed C or circled Latin C (Ⓒ or ⓒ) is a typographical symbol. As one of many enclosed alphanumerics, the symbol is a "C" within a circle.
The capitalized symbol (Ⓒ) can be generated with the unicode encoding "U+24B8" and the UTF-8 (hex.) encoding "e2 92 b8".
Source: http://en.wikipedia.org/wiki/%E2%92%B8
So, you are replacing one character with another, and that is all you can do.
You can't pass a custom font embedded in your text to another application through something like an intent. This is not possible.

Categories

Resources