Paragraph properties with RTF

R

Ross

I can't seem to modify paragraph properties when creating RTF in java.
I can change text to bold, italic, etc. But, if I try to set a
paragraph attribute, e.g. line indent, nothing happens. It's not only
when I display the document in a JEditorPane, the .rtf file I save to
disk doesn't have any line indent either.

What am I doing wrong?

import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.rtf.RTFEditorKit;
import java.io.FileOutputStream;
import javax.swing.*;
import java.awt.*;

public class RTFTest extends JFrame
{
private JEditorPane jep;

public RTFTest()
{
setBounds( 50, 50, 500, 500 );
setLayout( new BorderLayout() );
add( jep = new JEditorPane(), BorderLayout.CENTER );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jep.setEditable( false );
jep.setContentType( "text/rtf" );
}

public void setDocument( Document d )
{
jep.setDocument( d );
}

public static void main( String args[] ) throws Exception
{
RTFEditorKit aKit = new RTFEditorKit();
RTFTest r2 = new RTFTest();

Document d = aKit.createDefaultDocument();
d.insertString( 0, "Hello world\n", SimpleAttributeSet.EMPTY );
MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setBold( mas, true );
d.insertString( d.getLength(), ".... and this in bold\n", mas );
StyleConstants.setItalic( mas, true );
StyleConstants.setBold( mas, false );

// The following line seems to have no effect. Why?

StyleConstants.setLeftIndent( mas, 150.0F );

d.insertString( d.getLength(), "\nThis is another paragraph which
should be in italic and should be indented a bit", mas );

FileOutputStream out = new FileOutputStream( "test.rtf" );
aKit.write( out, d, 0, d.getLength() );
out.close();
r2.setDocument( d );
r2.setVisible( true );
}
}
 
R

Roedy Green

I can't seem to modify paragraph properties when creating RTF in java.

RTF is human readable. You can look at the documents you generate
with Java using text editor, and at RTF documents created with a word
processor and compare. That may be a tool to help you through your
problem.
--
Roedy Green Canadian Mind Products
http://mindprod.com

There is one brain organ that is optimised for understanding and articulating logical processes, and that is the outer layer of the brain, called the cerebral cortex. Unlike the rest of the brain, this relatively recent evolutionary development is rather flat, only about 0.32 cm (0.12 in) thick, and includes a mere 6 million neurons. This elaborately folded organ provides us with what little competence we do possess for understanding what we do and who we do it.
~ Ray Kurzweil (born: 1948-02-12 age: 61)
 
R

Ross

RTF is human readable. You can look at the documents you generate
with Java using text editor, and at RTF documents created with a word
processor and compare. That may be a tool to help you through your
problem.

Yes, I've looked at the output file. It has no sign of any line
indents.

{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;}

Hello world\par
\b .... and this in bold\par
\i\b0\par
This is another paragraph which should be in italic and should be
indented a bit\i0\b0\par
}

So, it hasn't helped me find the problem, other than confirm that it's
not just a problem with the JEditorPane object not displaying the
indent properly.

Hence, I'm still stuck.
 
R

Ross

The program in this java forum uses the StyledDocument class and seems
to be able to do everything needed. Perhaps using these classes will
solve my problem.

http://www.java-forums.org/awt-swing/2260-how-insert-tables-into-jtextpane.html

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;

public class AddingTables {
JTextPane textPane;

public AddingTables() {
String[] text = {
"This component models paragraphs that are composed of " +
"runs of character level attributes.\n", // 0 -
89
" \n", // 90 -
91
"Each paragraph may have a logical style attached to it
which " +
"contains the default attributes to use if not overridden
by " +
"attributes set on the paragraph or character run.
Components " +
"and images may be embedded in the flow of text." // 92 -
321
};
textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
createStyles(doc);
setContent(doc, text);
styleContent(doc);
}

private void createStyles(StyledDocument doc) {
Style baseStyle = doc.addStyle("base", null);
StyleConstants.setFontFamily(baseStyle, "Lucida Sans
Unicode");
StyleConstants.setFontSize(baseStyle, 18);
StyleConstants.setFirstLineIndent(baseStyle, 20f);
StyleConstants.setLeftIndent(baseStyle, 10f);

Style style = doc.addStyle("bold", baseStyle);
StyleConstants.setBold(style, true);

style = doc.addStyle("italic", baseStyle);
StyleConstants.setItalic(style, true);

style = doc.addStyle("blue", baseStyle);
StyleConstants.setForeground(style, Color.blue);

style = doc.addStyle("underline", baseStyle);
StyleConstants.setUnderline(style, true);

style = doc.addStyle("green", baseStyle);
StyleConstants.setForeground(style, Color.green.darker());
StyleConstants.setUnderline(style, true);

style = doc.addStyle("highlight", baseStyle);
StyleConstants.setForeground(style, Color.yellow);
StyleConstants.setBackground(style, Color.black);

style = doc.addStyle("table", null);
StyleConstants.setComponent(style, getTableComponent());

style = doc.addStyle("tableParagraph", null);
StyleConstants.setLeftIndent(style, 35f);
StyleConstants.setRightIndent(style, 35f);
StyleConstants.setSpaceAbove(style, 15f);
StyleConstants.setSpaceBelow(style, 15f);
}

private void setContent(StyledDocument doc, String[] text) {
try {
doc.insertString(0, text[0], doc.getStyle
("base"));
doc.insertString(doc.getLength(), text[1], doc.getStyle
("table"));
doc.insertString(doc.getLength(), text[2], doc.getStyle
("base"));
} catch(BadLocationException e) {
System.out.printf("Bad location error: %s%n", e.getMessage
());
}
}

private void styleContent(StyledDocument doc) {
Style style = doc.getStyle("base");
doc.setLogicalStyle(0, style);
style = doc.getStyle("underline");
doc.setCharacterAttributes(22, 10, style, false);
style = doc.getStyle("highlight");
doc.setCharacterAttributes(62, 26, style, false);

Style logicalStyle = doc.getLogicalStyle(0);
style = doc.getStyle("tableParagraph");
doc.setParagraphAttributes(90, 1, style, false);
style = doc.getStyle("table");
doc.setCharacterAttributes(90, 1, style, false);
doc.setLogicalStyle(92, logicalStyle);

style = doc.getStyle("blue");
doc.setCharacterAttributes(118, 13, style, false);
style = doc.getStyle("italic");
doc.setCharacterAttributes(166, 18, style, false);
style = doc.getStyle("green");
doc.setCharacterAttributes(235, 9, style, false);
doc.setCharacterAttributes(248, 9, style, false);
style = doc.getStyle("bold");
doc.setCharacterAttributes(263, 10, style, false);
doc.setCharacterAttributes(278, 6, style, false);
}

private JScrollPane getTableComponent() {
JTable table = new JTable(getModel());
Dimension d = table.getPreferredSize();
d.width = 300;
table.setPreferredScrollableViewportSize(d);
return new JScrollPane(table);
}

private AbstractTableModel getModel() {
return new AbstractTableModel() {
public int getColumnCount() { return 3; }
public int getRowCount() { return 3; }
public Object getValueAt(int row, int col) {
return String.valueOf(row + 1) + (col + 1);
}
};
}

private JScrollPane getContent() {
return new JScrollPane(textPane);
}

public static void main(String[] args) {
System.setProperty("swing.aatext", "true");
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new AddingTables().getContent());
f.setSize(500,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
 
R

Ross

It seems that I have to use objects describe by the StyledDocument
interface rather than Document. As I need to setLogicalAttributes()
for the paragraphs to have their line indents set.

It seems a bit of a clunky way of doing things, but it that's what I
have to do......
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top