I need write PDF File, and I use this sample(http://www.vogella.com/tutorials/JavaPDF/article.html) with this version "itextpdf-5.4.1.jar".
This create the PDF file, but when the word has "você" write this "você".
I find this code but has not work:
Document document;
...
...
document.addLanguage("pt-BR");
How set encoding or language to Brasil?
Thanks!
Take a look at my answer to Divide page in 2 parts so we can fill each with different source (this is year another question answered in The Best iText Questions on StackOverflow). In this example, we read a series of text files that are stored in UTF-8. To achieve this, we use this method:
public Phrase createPhrase(String path) throws IOException {
Phrase p = new Phrase();
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(path), "UTF8"));
String str;
while ((str = in.readLine()) != null) {
p.add(str);
}
in.close();
return p;
}
If you remove the "UTF8" and if you read that text as if it were ASCII, then you'd get the same behavior you are describing in your question: each byte would be treated as a single character whereas you have characters that require two bytes.
This is not really an iText question. This is a pure encoding question.
Related
This question already has answers here:
How to get FileInputStream to File in assets folder
(2 answers)
Closed 7 years ago.
I know that I can read files from assets using AssetManager, like here
AssetManager assetManager = getAssets();
InputStream is = assetManager.open(filename)
but open() method returns InputStream. So what I should do when I need to work with FileInputStream not with it superclass. Is there a way to get FileInputStream instance by InputStream instance?
No, there is not a way to convert an arbitrary InputStream back to a FileInputStream, as the data source may be of a different nature - something other than a literal File.
Assets are not files on the device, but rather particular chunks of the zip file which is your .apk. The Asset APIs give you access that is file-like in many ways (particularly with regard to input streams), but does not ultimately wrap an individual java.io.File, but rather an engine for extracting data directly from the .apk
In that case you can create a StringBuilder and read whole content of text file in it something like this
StringBuilder buf=new StringBuilder();
InputStream is=getAssets().open(filename);
BufferedReader in= new BufferedReader(new InputStreamReader(is, "UTF-8"));
String str;
while ((str=in.readLine()) != null) {
buf.append(str);
}
in.close();
Note: Things differ based on the file type you try to access; as Chris Stratton wrote as the data source may be of a different nature.
I'm encountering an odd situation whereby strings that I load from my resource XML file that have Spanish characters in them display correctly in my TextViews, but strings that I'm fetching from a JSON file that I load via HTTP at runtime display the missing char [] boxes
ESPAÑOL for example, when embedded in my XML strings works fine, but when pulled from my JSON is rendered as SPAÃ[]OL, so the Ñ is transformed into a à and a missing char!
I'm not sure at what point I need to intercept these strings and set the correct encoding on them. The JSON text file itself is generated on the server via Node, so, I'm not entirely sure if that's the point at which I should be encoding it, or if I should be encoding the fileReader on the Android side, or perhaps setting the TextView itself to be of some special encoding type (I'm unaware that this is an option, just sort of throwing my hands in the air, really).
[EDIT]
As per ianhanniballake's suggestion I am logging and seeing that the screwy characters are actually showing up in the log as well. However, when I look at the JSON file with a text viewer on the Android file system (it's sitting on the SDCARD) it appears correct.
So, it turned out that the text file was, indeed, encoded correctly and the issue was that I wasn't setting UTF-8 as my encoding on the FileInputStream...
The solution is to read the file thusly:
static String readInput() {
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream("myfile.json");
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char) ch);
}
in.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Hello I am trying to read a UTF-8 encoded txt files with Hebrew chars on my android application, and now after managing doing for some reason the 'a' char is always appended at the beginning of the String i read.. and I wonder why
Here is my code:
void Read(){
try {
File fileDir = new File("/sdcard/test.txt");
BufferedReader in = new BufferedReader( new InputStreamReader(
new FileInputStream(fileDir), "UTF8"));
String str;
while ((str = in.readLine()) != null) {
Log.i("TEST",str);
}
in.close();
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
this is the result i get
05-15 01:53:25.269: INFO/TEST(16236): אבגדהוזחטיכלמנסעפצקרשתa
In order to get a better answer, I need two questions answered:
What is the exact code point of the character in question (your "a")?
What is the exact byte sequence in your file, around the questionable area?
I'm going to take a guess here: You say the character is the first thing in the file ("appended at the beginning of the String") and that you got back it's in the Arabic Presentation Forms B block. The last character of Arabic Presentation Forms B, which oddly has nothing to do with Arabic, is U+FFEF, or the byte order mark (BOM). It usually appears at the beginning of UTF-16 or UTF-32 encoded files, and identifies the "endianess" of the encoding (whether the file is UTF-16LE or UTF-16BE encoded, likewise for UTF-32). It typically does not appear, however, in UTF-8 data, as UTF-8 has no notion of "byte order". That said, some brain-dead Windows programs will stick it there, and then have an additional option of "UTF-8 without BOM". (The BOM is used then to identify a file as likely being encoded in UTF-8.) My guess is you have a BOM in your data, and your program is reading it and passing it on to you.
IF this is your problem, and your file is genuinely encoded in UTF-8, you should be able to find the following byte sequence near the beginning of the file: EF BB BF — this is the UTF-8 representation of U+FFEF.
i have the following text in my strings.xml file
\n\nSVG Service Verlags GmbH & Co. KG \n
Schwertfegerstra?e 1-3\n
D-23556 L?beck\n
this is german text.
i need to decode this using utf-8 and then set it as text of a textview.
how do i go about this
thank you in advance.
EDIT:
i have tried the following
String decodedstring = URLDecoder.decode(nodevalue, "UTF-8");
this also does not work. why does this not work?
Some things to check.
Make sure your xml is tagged with the right encoding.
Make sure your xml file is SAVED with the right encoding. Looking at the text you pasted (from a browser?) it looks like the file is already mangled. Schwertfegerstra?e should be Schwertfegerstraße.
When you open the file you need to use an InputStreamReader with the encoding set.
See this page for an example:
http://www.mkyong.com/java/how-to-read-utf-8-encoded-data-from-a-file-java/
The key bit is:
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(fileDir), "UTF8"));
Trying to read an utf-8 encoded file in android...
InputStreamReader reader = new InputStreamReader(assets.open("data.txt"), "UTF-8");
BufferedReader br = new BufferedReader(reader);
String line;
//The line below throws an IOException!!
line = br.readLine();
What's wrong with this code?
It looks like you file is too big, you have to split it onto several files (1048576 bytes maximum for each) or find another way to reduce file size. Here is an article about similar problem http://androidgps.blogspot.com/2008/10/dealing-with-large-resources.html