Reputation: 557
I want to generate a RTF-Document with my program. I use an RTFEditor
which gives me the edited text in a rtfformat, but I also have TextFields
and I should take Strings from the TextFields into the RTF Documeent. I have tried to edit the rtf document text, but i got this annoying encoding problem with ANSI and UTF-8 i think.
If my TextFields got strings with an "äöüß" ... and so on i get this encoding stuff like "ö"
My target is to get a good looking RTF-Document without encoding-issues.
What is the best way to handle this issue?
Here is my class which outputs the rtf document: NOTE: Please focus into the important things in this code. I hope the german terms in my code dont confuse you :).
/**
* Handles the Saving of a Protokoll to a .rtf file.
* The .executeSaving() method executes the process of saving.
* @author me
*/
public class WordExport {
// other methods...
/**
* Executes the saving-progress by the given file.
* Structure of the rtf-File:
* -------------------------
* HeadString |
* -------------------------
* Content |
* .. |
* .. |
* .. |
* .. |
* -------------------------
* @param file points to the target where the .rtf should be saved.
* @param protokoll The protokoll which you want to save.
* @throws IOException Thrown when something is not okay with the IO.
*/
public void executeSaving(File file, Protokoll protokoll) throws IOException
{
//I want to insert my HeaderString into a specific position so i have to split etc...
String content = new String();
content = protokoll.getInhalt();
String[] split = content.split("}", 3);
for(int i = 0; i < split.length ; i++)
{
if(!split[i].contains("}"))
{
split[i] += "}";
}
}
String teilnehmer = new String();
for (Profil p : protokoll.getTeilnehmer())
{
teilnehmer += p.toString() + "\\par\n ";
}
String headString = new String();
//HERE IS THE IMPORTANT CODE --> here i put my strings bare into the rtf-format and i dont know how to handle the encoding.
headString = "\\f1\\fs44\\i0\\b0\\ul\\cf1\\ "+ protokoll.getTitel() +" \\par\n" +
"\\par\n" +
"\\fs24\\ul0 Raum: "+ protokoll.getRaum() +"\\par\n" +
"Zeitraum: "+ protokoll.getZeitraum() +"\\par\n" +
"Datum: "+ protokoll.getErstellungsdatum().toString() +"\\par\n" +
"\\par\n" +
"Teilnehmer: \\par\n" +
teilnehmer +
"\\ul0\\par ";
split[2] = headString+ split[2];
//Converts the String[] from above to the string, which you need to write into the .rtf file.
StringBuilder builder = new StringBuilder();
for(String s : split)
{
builder.append(s);
}
content = builder.toString();
if(!file.exists())
{
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
}
}
Upvotes: 1
Views: 1771
Reputation: 5809
I hate character encodings... I believe the following blog might help you here
i will post the code here just in case that site has issues in the future though, NOTE I DID NOT WRITE THIS NOR DO I TAKE CREDIT.
// Create a hashtable to hold the characters to convert
Hashtable<String, String> replace = new Hashtable<String, String>();
// The String to convert
String str = "The Māori Macron";
// Values we'll use in the loop
int value;
String bit;
for (int i = 0; i < str.length(); i++) {
// Get the character value
bit = str.substring(i, i + 1);
value = str.codePointAt(i);
// If the character value is above the
// 7-bit range of RTF ASCII
if (value > 127) {
replace.put(bit, "\\\\u" + value + "\\\\' ");
}
}
// Now replace all the characters we found
Enumeration e = parameters.keys();
String key, value;
while (e.hasMoreElements()) {
// Get the key
key = (String)e.nextElement();
// Get the value
value = (String)parameters.get(key);
// Make the substitution
str = str.replaceAll(key, value);
}
Upvotes: 0
Reputation: 10069
Use ISO-8859-1
encode. This encode format will support that characters too.
Upvotes: 2