Question

Ok so I'm trying to create an .rtf file using Java. I didn't know the syntax so what I did was I created an rtf file using TextEdit on a Mac then opening it with plaintext to see the code. I used this to create my output method which is as followed:

/* Creates RTF */
public String createRTF(){

    String l1 = "{\\rtf1\\ansi\\ansicpg1252\\cocoartf1265\\cocoasubrtf190 \n\n";
    String l2 = "{\\fonttbl\\f0\\fmodern\\fcharset0 Courier;} \n\n";
    String l3 = "{\\colortbl;\\red255\\green255\\blue255;} \n\n";
    String l4 = "\\margl1440\\margr1440\\vieww10800\\viewh8400\\viewkind0 \n\n";
    String l5 = "\\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\tx5040\\tx5760\\tx6480\\tx7200\\tx7920\\tx8640\\pardirnatural \n\n";
    String l6 = "\n\n";
    String l7 = "\\f0\\fs24 \\cf0 Original Sequences:\\ \n\n";
    String l8 = "\\ \n\n";
    String l9 =  sequence1 + "\\ \n\n";
    String l10 = sequence2 + "\\ \n\n";
    String l11 = "\\ \n";
    String l12 = "Alignment:\\ \n\n";
    String l13 = "\\ \n\n";
    String l14 = s1Align + "\\ \n\n";
    String l15 = aligned + "\\ \n\n";
    String l16 = s2Align + "\\ \n\n";
    String l17 = "\\ \n\n";
    String l18 = "Score: " + score + "}";



    String rtfString = l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8 + l9 + l10 + l11 + l12
            + l13 + l14 + l15 + l16 + l17 + l18;

    return rtfString;
}

I know it's a little sloppy but I was trying to be as specific as possible and to separate the lines because when I manually made the file in TextEdit the rtf code was:

{\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf190
{\fonttbl\f0\fmodern\fcharset0 Courier;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0 \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural

\f0\fs24 \cf0 Original Sequences:\
\
ACGTTGTACCA\
AGTTAAACCGTAAA\
\
Alignment:\
\
ACGTTGTACC--A--\
| |||  |||  |  \
A-GTTAAACCGTAAA\
\
Score: 3}  

My problem is this is exactly how my rtf code looks when I save a file using my program, but when I open the formatted file using TextEdit it's one continuos string not separated by lines like I want it. Funny thing is when I add the line breaks myself in TextEdit then save it the rtf code looks exactly the same. I tried adding two /n/n at the end of my strings but that didn't work. Is there a way to accomplish this output?

Was it helpful?

Solution 4

I'd hate to answer my own question but I did find the solution. Since what I wanted to display was in a par, I needed to add //line to the end of my Strings to properly put a new line in the middle of a paragraph. Thank you everyone for your help!

OTHER TIPS

See Rich Text Format (RTF) Version 1.5 Specification

"\\\r\n"

So a backslash followed by CR + LF will act as \par - a paragraph.

A carriage return (character value 13) or linefeed (character value 10) will be treated as a \par control if the character is preceded by a backslash. You must include the backslash; otherwise, RTF ignores the control word. (You may also want to insert a carriage-return/linefeed pair without backslashes at least every 255 characters for better text transmission over communication lines.)

Use \r\n instead of \n. That should fix the problem.

Use a string builder object to build your rtfString , its append method to build the string and the system's line separator for new lines , such as :

StringBuilder.append(System.getProperty("line.separator"));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top