The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

RTF Editor / Writer / Reader

Posted by Rod on February 22, 2002 at 4:36 AM

Hi Hiran,

Try this code, it's pretty dirty (no comments, but I did knock it together pretty quickly).

It allows you to read in RTF files (Wordpad generated), slight problem with the last couple of characters (seems to append then if I remember correctly).

Edit a file / new file (font, size, bold/italic/underline , justify right/left/center, colour)

Load and save as RTF

Hope it helps .


import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.StyleConstants.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.text.html.*;
import java.util.Map;
import java.io.*;
import javax.swing.filechooser.FileFilter;


/**
* Word.java
*


*
* RTF File reader/editor and writer
*
* @author Rod Higgins
*/


public class Word extends JPanel {

private int selectedFont = 0;
private JTextPane jtp;
private Font[] fonts;
private JComboBox jc;
private JComboBox size;
private JButton centre = new JButton("C");
private JButton left = new JButton("L");
private JButton right = new JButton("R");
private JButton justified = new JButton("J");
private JButton color = new JButton("Co");
private JButton rtfOut = new JButton("Save");
private JButton load = new JButton("Load");
private JButton italics = new JButton("I");
private JButton underlines = new JButton("U");
private JButton bolds = new JButton("B");

private StyleContext sc = new StyleContext();
private Style s = sc.addStyle( null , null );
private Style sl = sc.addStyle( "left" , null );
private Style sr = sc.addStyle( "right" , null );
private Style sce = sc.addStyle( "center" , null );
private Style sj = sc.addStyle( "justified" , null );
private DefaultStyledDocument dsd = new DefaultStyledDocument( sc );
private Integer fontSpec[] = new Integer[2];
private Vector fontLocation = new Vector();
private boolean newFont = true;
private boolean newSize = true;
private boolean newCol = true;
private boolean newBold = true;
private boolean newItal = true;
private boolean newUnder = true;
private Color textCol = Color.black;
private StringBuffer outFileRtf = new StringBuffer();
private HashMap colH;
private HashMap h;
private boolean italic,bold, underline;
private boolean listenersOn = true;
private JPanel j;
private MouseListener ml;
private KeyListener kl;
private JScrollPane jsp;

/**
* Constructor
*/
public Word() throws Exception{

setSize(600,400);
setLayout(new BorderLayout());
StyleConstants.setForeground(s,textCol);
StyleConstants.setAlignment(sl,StyleConstants.ALIGN_LEFT);
StyleConstants.setItalic(s,false);
StyleConstants.setBold(s,false);
StyleConstants.setUnderline(s,false);

jtp = new JTextPane(dsd);
kl = new KeyAdapter(){
public void keyPressed(KeyEvent k){
if(k.getKeyCode()==KeyEvent.VK_LEFT||k.getKeyCode()==KeyEvent.VK_RIGHT||
k.getKeyCode()==KeyEvent.VK_UP||k.getKeyCode()==KeyEvent.VK_DOWN){
Element e = dsd.getCharacterElement(jtp.getCaretPosition());
AttributeSet a = e.getAttributes();
StyleConstants.setFontFamily(s,StyleConstants.getFontFamily(a));
StyleConstants.setFontSize(s,StyleConstants.getFontSize(a));
StyleConstants.setForeground(s,StyleConstants.getForeground(a));
StyleConstants.setItalic(s,StyleConstants.isItalic(a));
StyleConstants.setBold(s,StyleConstants.isBold(a));
StyleConstants.setUnderline(s,StyleConstants.isUnderline(a));
size.setSelectedIndex((StyleConstants.getFontSize(a)/2)-2);
jc.setSelectedItem(StyleConstants.getFontFamily(a));
newSize = false;
newFont = false;
newCol=false;
newBold=false;
newItal=false;
newUnder=false;
if(StyleConstants.isItalic(a)){
italics.setBackground(Color.darkGray);
}else{
italics.setBackground(Color.lightGray);
}
if(StyleConstants.isBold(a)){
bolds.setBackground(Color.darkGray);
}else{
bolds.setBackground(Color.lightGray);
}
if(StyleConstants.isUnderline(a)){
underlines.setBackground(Color.darkGray);
}else{
underlines.setBackground(Color.lightGray);
}
};
if(k.getKeyCode()==KeyEvent.VK_CONTROL){
System.out.println("Control");
}
}


public void keyReleased(KeyEvent k){

}

public void keyTyped(KeyEvent k){
Character c = new Character(k.getKeyChar());
char keyHit = k.getKeyChar();
int keyCode = k.getKeyCode();

// do not attempt to validate control characters
if (!Character.isISOControl(keyHit)){
k.consume();
Font f = fonts[jc.getSelectedIndex()];

if(jtp.getCaretPosition()==dsd.getLength()){
StyleConstants.setFontFamily(s,f.getFamily());
StyleConstants.setFontSize(s, ((Integer)size.getSelectedItem()).intValue());
StyleConstants.setForeground(s, textCol);
StyleConstants.setItalic(s,italic);
StyleConstants.setBold(s,bold);
StyleConstants.setUnderline(s,underline);

}
else{
Element e = dsd.getCharacterElement(jtp.getCaretPosition()-1);
AttributeSet a = e.getAttributes();
if(newSize){
StyleConstants.setFontSize(s,((Integer)size.getSelectedItem()).intValue());
}
else{
StyleConstants.setFontSize(s,StyleConstants.getFontSize(a));
size.setSelectedIndex((StyleConstants.getFontSize(a)/2)-2);
}
if(newFont){
StyleConstants.setFontFamily(s,f.getFamily());
}
else {
StyleConstants.setFontFamily(s,StyleConstants.getFontFamily(a));
jc.setSelectedItem(StyleConstants.getFontFamily(a));
}
if(newCol){
StyleConstants.setForeground(s,textCol);
}
else {
StyleConstants.setForeground(s,StyleConstants.getForeground(a));

}
if(newBold){
StyleConstants.setBold(s,bold);
}
else {

StyleConstants.setBold(s,StyleConstants.isBold(a));

}
if(newItal){
StyleConstants.setItalic(s,italic);
}
else {
StyleConstants.setItalic(s,StyleConstants.isItalic(a));

}
if(newUnder){
StyleConstants.setUnderline(s,underline);
}
else {
StyleConstants.setUnderline(s,StyleConstants.isUnderline(a));

}

}
try{
dsd.insertString(jtp.getCaretPosition(),c.toString(), s);
dsd.setCharacterAttributes(jtp.getCaretPosition()-1,1,s,true);
newSize = false;
newFont = false;
newCol=false;
newBold = false;
newItal = false;
newUnder = false;
}catch(Exception e){

}
}
}
};
jtp.addKeyListener(kl);
jtp.setSelectedTextColor(Color.black);
jtp.setSelectionColor(Color.blue);

ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent ae){
Style sty = null;
if(ae.getActionCommand().equalsIgnoreCase("C")){
StyleConstants.setAlignment(sce,StyleConstants.ALIGN_CENTER);
centre.setBackground( Color.darkGray );
left.setBackground( Color.lightGray );
right.setBackground( Color.lightGray );
justified.setBackground( Color.lightGray );
Element el = dsd.getParagraphElement(jtp.getCaretPosition());
dsd.setParagraphAttributes(el.getStartOffset(),el.getEndOffset()-el.getStartOffset(),sce,false);
//sty=sce;
}else if(ae.getActionCommand().equalsIgnoreCase("R")){
StyleConstants.setAlignment(sr,StyleConstants.ALIGN_RIGHT);
centre.setBackground( Color.lightGray );
left.setBackground( Color.lightGray );
right.setBackground( Color.darkGray );
justified.setBackground( Color.lightGray );
Element el = dsd.getParagraphElement(jtp.getCaretPosition());
dsd.setParagraphAttributes(el.getStartOffset(),el.getEndOffset()-el.getStartOffset(),sr,false);
//sty=sr;
}else if(ae.getActionCommand().equalsIgnoreCase("L")){
StyleConstants.setAlignment(sl,StyleConstants.ALIGN_LEFT);
centre.setBackground( Color.lightGray );
left.setBackground( Color.darkGray );
right.setBackground( Color.lightGray );
justified.setBackground( Color.lightGray );
Element el = dsd.getParagraphElement(jtp.getCaretPosition());
dsd.setParagraphAttributes(el.getStartOffset(),el.getEndOffset()-el.getStartOffset(),sl,true);
// sty=sl;
}else if(ae.getActionCommand().equalsIgnoreCase("j")){
StyleConstants.setAlignment(sj,StyleConstants.ALIGN_JUSTIFIED);
centre.setBackground( Color.lightGray );
left.setBackground( Color.lightGray );
right.setBackground( Color.lightGray );
justified.setBackground( Color.darkGray );
Element el = dsd.getParagraphElement(jtp.getCaretPosition());
dsd.setParagraphAttributes(el.getStartOffset(),el.getEndOffset()-el.getStartOffset(),sj,true);
//sty=sj;
}
jtp.grabFocus();
}
};

centre.addActionListener(al);
right.addActionListener(al);
left.addActionListener(al);
justified.addActionListener(al);
centre.setBackground( Color.lightGray );
left.setBackground( Color.darkGray );
right.setBackground( Color.lightGray );
justified.setBackground( Color.lightGray );

italics.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(italic) italic = false;
else italic = true;
StyleConstants.setItalic( s, italic);
if(jtp.getSelectedText()!=null){

for(int i = jtp.getSelectionStart(); i Element e = dsd.getCharacterElement(i);
AttributeSet a = e.getAttributes();
StyleConstants.setFontSize( s,StyleConstants.getFontSize(a) );
StyleConstants.setFontFamily(s,StyleConstants.getFontFamily(a));
textCol=StyleConstants.getForeground(a);
StyleConstants.setForeground(s,textCol);
StyleConstants.setItalic(s,italic);
StyleConstants.setBold(s,StyleConstants.isBold(a));
StyleConstants.setUnderline(s,StyleConstants.isUnderline(a));
dsd.setCharacterAttributes(i,1, s , false);
}

}
jtp.grabFocus();

newItal=true;
if(italic) italics.setBackground(Color.darkGray);
else italics.setBackground(Color.lightGray);
}
});

bolds.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(bold) bold = false;
else bold = true;
StyleConstants.setBold( s, bold );
if(jtp.getSelectedText()!=null){
for(int i = jtp.getSelectionStart(); i Element e = dsd.getCharacterElement(i);
AttributeSet a = e.getAttributes();
StyleConstants.setFontSize( s,StyleConstants.getFontSize(a) );
StyleConstants.setFontFamily(s,StyleConstants.getFontFamily(a));
textCol=StyleConstants.getForeground(a);
StyleConstants.setForeground(s,textCol);
StyleConstants.setItalic(s,StyleConstants.isItalic(a));
StyleConstants.setBold(s,bold);
StyleConstants.setUnderline(s,StyleConstants.isUnderline(a));
dsd.setCharacterAttributes(i,1, s , false);
}

}
jtp.grabFocus();
jtp.setVisible(true);
if(bold) bolds.setBackground(Color.darkGray);
else bolds.setBackground(Color.lightGray);
newBold=true;
}
});

underlines.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(underline) underline=false;
else underline = true;
StyleConstants.setUnderline( s, underline );
if(jtp.getSelectedText()!=null){
for(int i = jtp.getSelectionStart(); i Element e = dsd.getCharacterElement(i);
AttributeSet a = e.getAttributes();
StyleConstants.setFontSize( s,StyleConstants.getFontSize(a) );
StyleConstants.setFontFamily(s,StyleConstants.getFontFamily(a));
textCol=StyleConstants.getForeground(a);
StyleConstants.setForeground(s,textCol);
StyleConstants.setItalic(s,StyleConstants.isItalic(a));
StyleConstants.setBold(s,StyleConstants.isBold(a));
StyleConstants.setUnderline(s,underline);
dsd.setCharacterAttributes(i,1, s , false);
}
}
if(underline) underlines.setBackground(Color.darkGray);
else underlines.setBackground(Color.lightGray);
newUnder=true;
jtp.grabFocus();
}
});
color.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textCol = JColorChooser.showDialog(jtp, "FontColor", Color.black);
newCol = true;
try{
for(int i = jtp.getSelectionStart(); i Element e = dsd.getCharacterElement(i);
AttributeSet a = e.getAttributes();
StyleConstants.setFontSize( s,StyleConstants.getFontSize(a) );
StyleConstants.setFontFamily(s,StyleConstants.getFontFamily(a));
StyleConstants.setForeground(s,textCol);
StyleConstants.setItalic(s,StyleConstants.isItalic(a));
StyleConstants.setBold(s,StyleConstants.isBold(a));
StyleConstants.setUnderline(s,StyleConstants.isUnderline(a));
dsd.setCharacterAttributes(i,1, s , false);
}
jtp.grabFocus();
newFont=true;
}catch(Exception e){
e.printStackTrace();
}
}
});
load.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
LoadRtf lrtf = new LoadRtf();
int option = lrtf.showOpenDialog(null);
if(option==JFileChooser.APPROVE_OPTION){
j.remove(jsp);
jtp = lrtf.getFormattedDocument(lrtf.getSelectedFile());
jtp.setVisible(true);
jtp.getText();
jsp = new JScrollPane(jtp);
j.add(jsp, BorderLayout.CENTER);
validate();
dsd = (DefaultStyledDocument)jtp.getDocument();
Style styleArray[] = lrtf.getStyles();
s = styleArray[0];
sl = styleArray[1];
sr = styleArray[2];
sce = styleArray[3];
sj = styleArray[4];
jtp.addMouseListener(ml);
jtp.addKeyListener(kl);
jsp.getVerticalScrollBar().setValue(100);
}
}
});

fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
Vector fontVector = new Vector();
for(int i = 0;i fontVector.addElement(fonts[i].getFamily());
//Style ss = sc.addStyle( fonts[i].getFamily(), null );

if(fonts[i].getFontName().equalsIgnoreCase("Arial")){
selectedFont=i;
TabStop[] ts = new TabStop[1];
ts[0] = new TabStop(0.5f,TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
StyleConstants.setTabSet(s, new TabSet(ts));
StyleConstants.setFontFamily(s, fonts[i].getFamily());
ColorConstants.setForeground(s, textCol);
dsd.setLogicalStyle(0, s);
}
}

jc = new JComboBox(fontVector);
jc.setSelectedIndex(selectedFont);
jc.setBackground(Color.white);
jc.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
try{
if(listenersOn){
if(jc.getSelectedIndex()<0){
jc.setSelectedItem("Arial");
}
Font f = fonts[jc.getSelectedIndex()];
StyleConstants.setFontFamily( s,f.getFontName() );
dsd.setCharacterAttributes(jtp.getSelectionStart(),
jtp.getSelectionEnd()-jtp.getSelectionStart(), s , true);
jtp.grabFocus();
newFont=true;
}
}catch(Exception e){

}
}
});
Vector sizes = new Vector();
for(int i = 4; i<78; i+=2){
sizes.addElement(new Integer(i));
}
size = new JComboBox(sizes);
size.setBackground( Color.white );
size.setSelectedItem(sizes.elementAt(4));
size.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try{
if(listenersOn){
for(int i = jtp.getSelectionStart(); i Element e = dsd.getCharacterElement(i);
AttributeSet a = e.getAttributes();
Integer in = (Integer)size.getSelectedItem();
StyleConstants.setFontSize( s,in.intValue() );
StyleConstants.setFontFamily(s,StyleConstants.getFontFamily(a));
textCol=StyleConstants.getForeground(a);
StyleConstants.setForeground(s,textCol);
StyleConstants.setItalic(s,StyleConstants.isItalic(a));
StyleConstants.setBold(s,StyleConstants.isBold(a));
StyleConstants.setUnderline(s,StyleConstants.isUnderline(a));
dsd.setCharacterAttributes(i,1, s , false);
jtp.grabFocus();
newSize = true;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
});

ml = new MouseAdapter(){
public void mouseReleased(MouseEvent me){
if(jtp.getSelectedText()!=null){
int start = jtp.getSelectionStart();
int end = jtp.getSelectionEnd();
boolean boldOn = false,boldOff = false,underOn=false,underOff=false,italOn=false,italOff=false;
for(int i = start;i Element e = dsd.getCharacterElement(jtp.getCaretPosition());
AttributeSet a = e.getAttributes();

listenersOn=false;
size.setSelectedIndex((StyleConstants.getFontSize(a)/2)-2);
jc.setSelectedItem(StyleConstants.getFontFamily(a));
listenersOn=true;

newSize = false;
newFont = false;
newCol=false;
newBold=false;
newItal=false;
newUnder=false;
}
}
}

public void mouseClicked(MouseEvent me){
int textLoc = jtp.getCaretPosition();
if(jtp.getCaretPosition()==dsd.getLength()){
textLoc -= 1;
}
Element e = dsd.getCharacterElement(textLoc);
AttributeSet a = e.getAttributes();
StyleConstants.setFontFamily(s,StyleConstants.getFontFamily(a));
StyleConstants.setFontSize(s,StyleConstants.getFontSize(a));
textCol=StyleConstants.getForeground(a);
StyleConstants.setForeground(s,textCol);
StyleConstants.setItalic(s,StyleConstants.isItalic(a));
StyleConstants.setBold(s,StyleConstants.isBold(a));
StyleConstants.setUnderline(s,StyleConstants.isUnderline(a));
size.setSelectedIndex((StyleConstants.getFontSize(a)/2)-2);
jc.setSelectedItem(StyleConstants.getFontFamily(a));
newSize = false;
newFont = false;
newCol=false;
newBold=false;
newItal=false;
newUnder=false;
if(StyleConstants.isItalic(a)){
italics.setBackground(Color.darkGray);
}else{
italics.setBackground(Color.lightGray);
}
if(StyleConstants.isBold(a)){
bolds.setBackground(Color.darkGray);
}else{
bolds.setBackground(Color.lightGray);
}
if(StyleConstants.isUnderline(a)){
underlines.setBackground(Color.darkGray);
}else{
underlines.setBackground(Color.lightGray);
}
jtp.grabFocus();
}
};

jtp.addMouseListener(ml);
rtfOut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
outFileRtf = new StringBuffer();
outFileRtf.append("{\\rtf1\\ansi\\ansicpg1252\\deff0\\deftab720{\\fonttbl");
addFontTable();
addColorTable();
addBody();
saveText();
jtp.grabFocus();

}
});
JPanel attPanel = new JPanel();
attPanel.add(jc);
attPanel.add(size);
attPanel.add(centre);
attPanel.add(right);
attPanel.add(left);
attPanel.add(justified);
attPanel.add(color);
attPanel.add(rtfOut);
attPanel.add(load);
attPanel.add(italics);
attPanel.add(bolds);
attPanel.add(underlines);
jsp = new JScrollPane(jtp);
j = new JPanel(new BorderLayout());
jtp.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0,15,0,15),
BorderFactory.createLineBorder(Color.black,2)));
add(attPanel, BorderLayout.NORTH);
add(j, BorderLayout.CENTER);
j.add(jsp, BorderLayout.CENTER);
setVisible( true );
}


public void saveText(){
JFileChooser jfc = new JFileChooser();
int option = jfc.showSaveDialog(this);
if(option==JFileChooser.APPROVE_OPTION){
OutputStream out;
try{
File file = new File(jfc.getSelectedFile().getAbsolutePath());
byte buffer[] = outFileRtf.toString().getBytes();
out = new FileOutputStream( file );
out.write( buffer );
out.close();
}catch (IOException ioe){

}
}

}

public static void main(String argv[]){
try{
JFrame jf = new JFrame("JWord");
jf.setSize(600,400);
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(new Word(), BorderLayout.CENTER);
jf.setVisible( true );
}catch(Exception e){
e.printStackTrace();
}
}


public void addBody(){
outFileRtf.append("\\deflang2057\\horzdoc{\\*\\fchars }{\\*\\lchars }");
int startPos = 0;

String currentFont = "Arial";
int currentSize = 12;
Color currentCol = Color.black;
boolean currentBold = false;
boolean currentItalic = false;
boolean currentUnderline = false;

boolean addStyle = false;

char c[] = jtp.getText().toCharArray();
int enter = dsd.getLength();
int alignment = StyleConstants.ALIGN_LEFT;
for(int i = startPos;i // code to extract formatting letter by letter
Element e = dsd.getCharacterElement(i+enter);
AttributeSet a = e.getAttributes();
if(c[i]=='\n')
{
enter++;
}
if(c[i]=='\n'||i==0){
if(i!=0) outFileRtf.append("\\par ");
if(i!=i+enter){
if(i!=dsd.getLength()+enter||c[i+1]!='\n'){
Element ee = dsd.getParagraphElement(i+1);
AttributeSet aa = ee.getAttributes();
if(StyleConstants.getAlignment(aa)!= alignment||
i==0){
alignment = StyleConstants.getAlignment(aa);
outFileRtf.append("\\pard");

if(StyleConstants.getAlignment(aa)==StyleConstants.ALIGN_RIGHT){
outFileRtf.append("\\qr");
}
else if(StyleConstants.getAlignment(aa)==StyleConstants.ALIGN_CENTER){
outFileRtf.append("\\qc");
}
else if(StyleConstants.getAlignment(aa)==StyleConstants.ALIGN_JUSTIFIED){
outFileRtf.append("\\qj");
}
else if(StyleConstants.getAlignment(aa)==StyleConstants.ALIGN_LEFT){
outFileRtf.append("\\ql");
}

addStyle=true;
}
}
}

}
if(i==0||c[i]!='\n'){
Element ep = dsd.getCharacterElement(i-(enter-dsd.getLength()));
AttributeSet ap = ep.getAttributes();
if(!currentFont.equalsIgnoreCase(StyleConstants.getFontFamily(ap))||
currentSize!=StyleConstants.getFontSize(ap)||
currentCol.getRGB()!=StyleConstants.getForeground(ap).getRGB()||
!currentUnderline==StyleConstants.isUnderline(ap)||
!currentBold==StyleConstants.isBold(ap)||
!currentItalic==StyleConstants.isItalic(ap)||addStyle){

currentCol = StyleConstants.getForeground(ap);
currentFont = StyleConstants.getFontFamily(ap);
currentSize = StyleConstants.getFontSize(ap);
currentBold = StyleConstants.isBold(ap);
currentItalic = StyleConstants.isItalic(ap);
currentUnderline = StyleConstants.isUnderline(ap);

boolean noNext = false;
Set es = h.entrySet();
Iterator it = es.iterator();
while(it.hasNext()){
Map.Entry me = (Map.Entry)it.next();
if(((String)me.getValue()).equalsIgnoreCase(StyleConstants.getFontFamily(ap))){
if(addStyle) outFileRtf.append("\\plain");
else outFileRtf.append("\\plain");
outFileRtf.append("\\f"+me.getKey());
noNext =true;
break;
}
}
if(noNext) outFileRtf.append("\\fs"+currentSize*2);
es = colH.entrySet();
it = es.iterator();
while(it.hasNext()){
Map.Entry me = (Map.Entry)it.next();
if(((Color)me.getValue()).getRGB()==currentCol.getRGB()){
if(currentCol.getRGB()!=Color.black.getRGB()){
outFileRtf.append("\\cf"+(String)me.getKey());
}
break;
}
}

if(currentBold){
outFileRtf.append("\\b");
}
if(currentItalic){
outFileRtf.append("\\i");
}
if(currentUnderline){
outFileRtf.append("\\u");
}

if(noNext) outFileRtf.append(" ");
addStyle=false;
}
if(c[i]=='\\'){
outFileRtf.append('\\');
}
if(c[i]=='}'||c[i]=='{'){
outFileRtf.append('\\');
}
outFileRtf.append(c[i]);
}

}


outFileRtf.append(" }");
}


public int[] getRedGreen(Color changeCol){
int i[] = new int[3];
i[0] = changeCol.getRed();
i[1] = changeCol.getGreen();
i[2] = changeCol.getBlue();
return i;
}

public void addColorTable(){
int colId = 0;
outFileRtf.append("{\\colortbl");
colH = new HashMap();
for(int i = 0;i Element e = dsd.getCharacterElement(i);
AttributeSet a = e.getAttributes();
Color col = StyleConstants.getForeground(a);

if(!colH.containsValue(StyleConstants.getForeground(a))){
colH.put(new String(""+colId),StyleConstants.getForeground(a));
colId++;
}


}

Set s = colH.entrySet();
String colString[] = new String[s.size()];
Iterator it = s.iterator();
int i = 0;
while(it.hasNext()){
Map.Entry me = (Map.Entry)it.next();
Color thisCol = (Color)me.getValue();
colString[i] = new String("\\red"+thisCol.getRed()+"\\green"+thisCol.getGreen()+"\\blue"+
thisCol.getBlue()+";");
i++;
}

for(i = colString.length-1;i>-1;i--){
outFileRtf.append(colString[i]);
}
outFileRtf.append("}\n");


}

public void addFontTable(){
int fontId = 0;
h = new HashMap();
for(int i = 0;i Element e = dsd.getCharacterElement(i);
AttributeSet a = e.getAttributes();

if(!h.containsValue(StyleConstants.getFontFamily(a))){
h.put(new String(""+fontId),StyleConstants.getFontFamily(a));
fontId++;
}


}

Set s = h.entrySet();
String fontString[] = new String[s.size()];
Iterator it = s.iterator();
int i = 0;
while(it.hasNext()){
Map.Entry me = (Map.Entry)it.next();
fontString[i] = new String("{\\f"+Integer.parseInt((String)me.getKey())+
"\\"+(String)me.getValue()+" "+(String)me.getValue()+";}");
i++;
}

for(i = fontString.length-1;i>-1;i--){
outFileRtf.append(fontString[i]);
}
outFileRtf.append("}\n");

}

}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us