Use Android PDF Writer but can't support chinese - android

I use the source code from this website to create a new PDF file, and it works.
The problem is that when I write Chinese in PDFWriteDemo.java : mPDFWriter.addText(70, 50, 12, "各位好"); , it can't display the characters in the PDF file normally.
Instead, it shows ???.

Try to change this line:
outputToFile("helloworld.pdf",pdfcontent,"ISO-8859-1");
to:
outputToFile("helloworld.pdf",pdfcontent,"UTF-8");
UPDATE
Considering that it might be the encoding issue, you'll need to follow these sequence to change the Font and Encoding:
First we found that in PDFWriterDemo.java, this line defines the Font and encoding:
mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING);
In which the mPDFWriter is a class that defined in PDFWriter.java.
As you can see in PDFWriter.java there's an method:
public void setFont(String subType, String baseFont, String encoding) {
mCurrentPage.setFont(subType, baseFont, encoding);
}
Where mCurrentPage is a class defined in Page.java
Here in Page.java
public void setFont(String subType, String baseFont, String encoding) {
IndirectObject lFont = mDocument.newIndirectObject();
mDocument.includeIndirectObject(lFont);
lFont.setDictionaryContent(" /Type /Font\n /Subtype /" + subType + "\n /BaseFont /" + baseFont + "\n /Encoding /" + encoding + "\n");
mPageFonts.add(lFont);
}
Then you'll find that actually they didn't embed the font into the package, they just provide a font's name and with correct encoding the PDF reader will load them automatically.
Thus the solution of what I think, is to edit the StandardFonts.java to add the PDF standard fonts for rendering Chinese characters and a correct encoding.

Related

Can I remove the symbol new line from base 64 encoded key file?

I have a base 64 encoded key file. If I open it by Text Editor, I see 4 lines like this:
Then I copy the text and paste to Android Studio, I see the symbol "\n" is generated as below:
This pubic key doesn't work. So I tried :
Remove all "\n" symbol. Still doesn't work.
Replace the "\n" symbol with the space " ". Again doesn't work.
Could you please show me where I am wrong?
Rather than pasting the contents of the file into a string, why not just copy the file itself into your assets folder. For example:
public String readPublicKeyFromFile() {
String publicKeyString; = "";
try {
InputStream is = getAssets().open("public_key.txt");
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
return new String(buffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
Its android studio console character limitation that it shows long string in multiple lines.
Best way is to copy that string in any text editor(notepad) and make it single line string and then paste it to studio.
Another way is just delete that '\n' character from your string it will be single line string.
e.g.
private static final String = "abcdefgh" +
"ijklmnop" +
"qrstuvwxyz";
just remove '\n' character from your string.
If you creating the "publickey.txt" (base64) file, just use "Base64.NO_WRAP" flag for creating the file. This flag not allow the "\n" character.
By default it takes the "Base64.DEFAULT" flag, so every 64 characters after "\n" will be added automatically.
// for encoding the String with out \n
String base64Str=Base64.encode(your_string,Base64.NO_WRAP);
// for decoding
byte[] resByte=Base64.decode(base64Str,Base64.NO_WRAP);
// convert into String
String resStr=new String(resByte,"UTF-8");

Google Translate API with android application

I created an android application and I'm using Google Translate Rest service.
I used this URL to send a request in order to translate a source text.
"https://www.googleapis.com/language/translate/v2?key=" + apiKey + "&target=en&q=" + text"
As you can see the source language was not set in order to enable the service to detect
the language of the given text.
When I put something in Hebrew the result I get is question mark.
For example if I enter "שלום עולם" which need to be translated into "hello world" in English,
I get the result "???? ????".
I tried with other languages like Russian or Spanish. It worked fine with Spanish, but
with the Russian language it worked like Hebrew.
Is there any bug with the API or am I doing something wrong?
Update:
encoded = URLEncoder.encode(textToTranslate,"UTF-8");
url = new URL("https://www.googleapis.com/language/translate/v2?key=" + apiKey + "&target=en&q=" + encoded);
Thanks,
Elior
You need to encode the text before pass as q parameter.
For example for the string שלום עולם:
String original = "שלום עולם";
String encoded = null;
try {
encoded = URLEncoder.encode(original, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
The value of the encoded variable will be %D7%A9%D7%9C%D7%95%D7%9D+%D7%A2%D7%95%D7%9C%D7%9D and the URL became:
"https://www.googleapis.com/language/translate/v2?key=" + apiKey + "&target=en&q=%D7%A9%D7%9C%D7%95%D7%9D+%D7%A2%D7%95%D7%9C%D7%9D"

Can't read lines from file by using StreamReader on Unity3d (Android)

I need to read a text stream by using StreamReader from file on android platform. File is about 100k lines, so even editor is getting stuck if i try to load it all to TextAsset or if i use WWW.
I simply need to read that file line by line without loading it all to a string. Then i'll do a tree generation from the lines that i got from the file. (But probably that part doesn't matter, i just need help on file reading part.)
I'm giving the code that i wrote down below. It works perfectly on editor, but fails on android.
I would be glad if anyone tell me, what am i missing.
(ps. english is not my native and this is my first question on the site. so sorry for the any mistakes that i may have done.)
private bool Load(string fileName)
{
try
{
string line;
string path = Application.streamingAssetsPath +"/";
StreamReader theReader = new StreamReader(path + fileName +".txt", Encoding.UTF8);
using (theReader)
{
{
line = theReader.ReadLine();
linesRead++;
if (line != null)
{
tree.AddWord(line);
}
}
while (line != null);
theReader.Close();
return true;
}
}
catch (IOException e)
{
Debug.Log("{0}\n" + e.Message);
exception = e.Message;
return false;
}
}
You can't use Application.streamingAssetsPath as a path on Android because streaming assets are stored within the JAR file with the application.
From http://docs.unity3d.com/Manual/StreamingAssets.html:
Note that on Android, the files are contained within a compressed .jar
file (which is essentially the same format as standard zip-compressed
files). This means that if you do not use Unity’s WWW class to
retrieve the file then you will need to use additional software to see
inside the .jar archive and obtain the file.
Use WWW like this in a coroutine:
WWW data = new WWW(Application.streamingAssetsPath + "/" + fileName);
yield return data;
if(string.IsNullOrEmpty(data.error))
{
content = data.text;
}
Or, if you really want to keep it simple (and your file is only a few 100k, stick it in a resource folder:
TextAsset txt = (TextAsset)Resources.Load(fileName, typeof(TextAsset));
string content = txt.text;

How to load different html file in different language Android environment?

I have some different language html file for different language environment, and all of those html files contain images. Now I want to show suitable html files with WebView adapt to current language of Android. How can I do that? Thanks.
You can give special names for non-English files like file-de.html for German and then use the following code:
private static String getFileName() {
Configuration config = getResources().getConfiguration();
InputStream stream = null;
try {
String name = "file-" + config.locale.getLanguage() + ".html";
stream = getAssets().open(name);
return name;
} catch (IOException exception) {
return "file.html";
} finally {
if (stream != null) {
stream.close();
}
}
}
You might use "string values" to get Android language dependent HTML file names without using any additional java code and just using getString(). Here's an example for 2 languages:
In file values/strings.xml:
<string name="html_help_basepage">en_help.html</string>
In file de/strings.xml:
<string name="html_help_basepage">some_german_help.html</string>
And so on...
For your Java code (loading from local file in asset folder as in question)
helpView = (WebView) findViewById(R.id.wv_help);
String adaptedToLanguage = getString(R.string.html_help_basepage)
helpView.loadUrl("file:///android_asset/" + adaptedToLanguage);
No additional Java code is needed. I hope this is helpful and answers the question.
Added a new answer, which should be more precise by providing an example.

Why are foreign characters not read using inputStream?

I have a text file which contains data I need to preload into a SQLite database. I saved in in res/raw.
I read the whole file using readTxtFromRaw(), then I use the StringTokenizer class to process the file line by line.
However the String returned by readTxtFromRaw does not show foreign characters that are in the file. I need these as some of the text is Spanish or French. Am I missing something?
Code:
String fileCont = new String(readTxtFromRaw(R.raw.wordstext));
StringTokenizer myToken = new StringTokenizer(fileCont , "\t\n\r\f");
The readTxtFromRaw method is:
private String readTxtFromRaw(Integer rawResource) throws IOException
{
InputStream inputStream = mCtx.getResources().openRawResource(rawResource);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
return byteArrayOutputStream.toString();
}
The file was created using Eclipse, and all characters appear fine in Eclipse.
Could this have something to do with Eclipse itself? I set a breakpoint and checked out myToken in the Watch window. I tried to manually replace the weird character for the correct one (for example í, or é), and it would not let me.
Have you checked the several encodings?
what's the encoding of your source file?
what's the encoding of your output stream?
the byteArrayOutputStream.toString() converts according to the platform's default character encoding. So I guess it will strip the foreign characters or convert them in a way that they are not displayed in your output.
Have you already tried to use byteArrayOutputStream.toString(String enc)? Try "UTF-8" or "iso-8859-1" or "UTF-16" for the encoding.

Categories

Resources