2014-07-10 4 views
1

내 프로그램으로 RTF 문서를 생성하고 싶습니다. 나는 RTFEditor을 사용하여 rtfformat에서 편집 된 텍스트를 제공하지만, 역시 이 있고 TextFields에서 RTF Documeent로 문자열을 가져와야합니다. rtf 문서 텍스트를 편집하려고했지만 ANSI와 UTF-8로 인해이 성가신 인코딩 문제가 발생했습니다. 내 TextFields에 "äöüß"라는 문자열이있는 경우 ... "¶"과 같은 인코딩을 얻습니다. 내 목표는 인코딩 문제가없는 멋진 RTF 문서를 얻는 것입니다.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(); 
    } 
} 
+1

중요하지만 관련이 있습니다. 코드의 식별자와 설명에 영어를 사용해야합니다. 코드를 세계와 언제 공유해야하는지 알 수 없습니다. 영어는 의사 소통과 협업을 훨씬 쉽게 만듭니다. 또한 영어를 쓸 때 더 능숙해질 것입니다. – Palec

+0

내 첫 번째 프로젝트 중 하나이며 내 요점은 내 다음 프로젝트에서 수행 할 첫 번째 작업 중 하나입니다. – Rubinum

답변

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); 
} 
+0

링크 및 예제가 좋지만 코드에서 "x"가 무엇인지 이해하지 못합니다. 신고가 누락되었습니다. – Rubinum

+0

나는 아마 s라고 생각한다. –

+0

x를 s로 바꿨지 만 지금은 "매개 변수"가 무엇인지 알지 못한다. 어쨌든 언급하지 않았다. 죄송합니다 묻지 만 더 많은 정보없이이 코드는 작동하지 않으며 자동 완성을 할 수 없습니다. – Rubinum

2

ISO-8859-1 인코딩을 사용하십시오. 이 인코딩 형식은 해당 문자도 지원합니다.