我想用我的程序生成一个RTF文档。我使用RTFEditor
给出了rtfformat中的编辑文本,但我也有TextFields
,我应该将TextFields中的Strings带入RTF Documeent。我试图编辑rtf文档文本,但我认为这有恼人的ANSI和UTF-8编码问题。
如果我的TextFields获得了带有“äöüß”的字符串...依此类推,我会得到像“¶”这样的编码内容
我的目标是获得一个没有编码问题的好看的RTF文档。
处理此问题的最佳方法是什么?
这是我输出rtf文档的类: 注意:请关注此代码中的重要事项。我希望我的代码中的德语术语不会让你感到困惑:)。
/**
* 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();
}
}
答案 0 :(得分:2)
使用ISO-8859-1
编码。这种编码格式也支持这些字符。
答案 1 :(得分:0)
我讨厌角色编码......我相信以下博客可能会帮助你here
我会在这里发布代码,以防该网站将来出现问题,但请注意,我没有写这个,也不是我采取信用。
// 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);
}