1 50 51 package com.lowagie.text.rtf.text; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 57 import com.lowagie.text.Chunk; 58 import com.lowagie.text.DocumentException; 59 import com.lowagie.text.Element; 60 import com.lowagie.text.Paragraph; 61 import com.lowagie.text.rtf.RtfBasicElement; 62 import com.lowagie.text.rtf.RtfElement; 63 import com.lowagie.text.rtf.document.RtfDocument; 64 import com.lowagie.text.rtf.graphic.RtfImage; 65 import com.lowagie.text.rtf.style.RtfFont; 66 import com.lowagie.text.rtf.style.RtfParagraphStyle; 67 68 69 77 public class RtfParagraph extends RtfPhrase { 78 79 82 public static final byte[] PARAGRAPH = "\\par".getBytes(); 83 84 87 protected RtfParagraphStyle paragraphStyle = null; 88 89 95 public RtfParagraph(RtfDocument doc, Paragraph paragraph) { 96 super(doc); 97 98 RtfFont baseFont = null; 99 if(paragraph.getFont() instanceof RtfParagraphStyle) { 100 this.paragraphStyle = this.document.getDocumentHeader().getRtfParagraphStyle(((RtfParagraphStyle) paragraph.getFont()).getStyleName()); 101 baseFont = this.paragraphStyle; 102 } else { 103 baseFont = new RtfFont(this.document, paragraph.getFont()); 104 this.paragraphStyle = new RtfParagraphStyle(this.document, this.document.getDocumentHeader().getRtfParagraphStyle("Normal")); 105 this.paragraphStyle.setAlignment(paragraph.getAlignment()); 106 this.paragraphStyle.setFirstLineIndent((int) (paragraph.getFirstLineIndent() * RtfElement.TWIPS_FACTOR)); 107 this.paragraphStyle.setIndentLeft((int) (paragraph.getIndentationLeft() * RtfElement.TWIPS_FACTOR)); 108 this.paragraphStyle.setIndentRight((int) (paragraph.getIndentationRight() * RtfElement.TWIPS_FACTOR)); 109 this.paragraphStyle.setSpacingBefore((int) (paragraph.spacingBefore() * RtfElement.TWIPS_FACTOR)); 110 this.paragraphStyle.setSpacingAfter((int) (paragraph.spacingAfter() * RtfElement.TWIPS_FACTOR)); 111 if(paragraph.hasLeading()) { 112 this.paragraphStyle.setLineLeading((int) (paragraph.getLeading() * RtfElement.TWIPS_FACTOR)); 113 } 114 this.paragraphStyle.setKeepTogether(paragraph.getKeepTogether()); 115 } 116 117 for(int i = 0; i < paragraph.size(); i++) { 118 Element chunk = (Element) paragraph.get(i); 119 if(chunk instanceof Chunk) { 120 ((Chunk) chunk).setFont(baseFont.difference(((Chunk) chunk).getFont())); 121 } else if(chunk instanceof RtfImage) { 122 ((RtfImage) chunks.get(i)).setAlignment(this.paragraphStyle.getAlignment()); 123 } 124 try { 125 chunks.add(doc.getMapper().mapElement(chunk)); 126 } catch(DocumentException de) { 127 } 128 } 129 } 130 131 136 public void setKeepTogetherWithNext(boolean keepTogetherWithNext) { 137 this.paragraphStyle.setKeepTogetherWithNext(keepTogetherWithNext); 138 } 139 140 147 public byte[] write() { 148 ByteArrayOutputStream result = new ByteArrayOutputStream (); 149 try { 150 writeContent(result); 151 } catch(IOException ioe) { 152 ioe.printStackTrace(); 153 } 154 return result.toByteArray(); 155 } 156 160 public void writeContent(final OutputStream result) throws IOException 161 { 162 result.write(PARAGRAPH_DEFAULTS); 163 result.write(PLAIN); 164 165 if(inTable) { 166 result.write(IN_TABLE); 167 } 168 169 if(this.paragraphStyle != null) { 170 result.write(this.paragraphStyle.writeBegin()); 171 } 172 result.write("\\plain".getBytes()); 173 174 for(int i = 0; i < chunks.size(); i++) { 175 RtfBasicElement rbe = (RtfBasicElement)chunks.get(i); 176 rbe.writeContent(result); 178 } 179 180 if(this.paragraphStyle != null) { 181 result.write(this.paragraphStyle.writeEnd()); 182 } 183 184 if(!inTable) { 185 result.write(PARAGRAPH); 186 } 187 if(this.document.getDocumentSettings().isOutputDebugLineBreaks()) { 188 result.write('\n'); 189 } 190 } 191 192 197 public int getIndentLeft() { 198 return this.paragraphStyle.getIndentLeft(); 199 } 200 201 206 public void setIndentLeft(int indentLeft) { 207 this.paragraphStyle.setIndentLeft(indentLeft); 208 } 209 210 215 public int getIndentRight() { 216 return this.paragraphStyle.getIndentRight(); 217 } 218 219 224 public void setIndentRight(int indentRight) { 225 this.paragraphStyle.setIndentRight(indentRight); 226 } 227 } 228 | Popular Tags |