FileDocCategorySizeDatePackage
TextBox.javaAPI DocApache Poi 3.0.117475Thu May 31 18:45:28 BST 2007org.apache.poi.hslf.model

TextBox

public class TextBox extends SimpleShape
Represents a TextFrame shape in PowerPoint.

Contains the text in a text frame as well as the properties and methods that control alignment and anchoring of the text.

author
Yegor Kozlov

Fields Summary
public static final int
AnchorTop
How to anchor the text
public static final int
AnchorMiddle
public static final int
AnchorBottom
public static final int
AnchorTopCentered
public static final int
AnchorMiddleCentered
public static final int
AnchorBottomCentered
public static final int
AnchorTopBaseline
public static final int
AnchorBottomBaseline
public static final int
AnchorTopCenteredBaseline
public static final int
AnchorBottomCenteredBaseline
public static final int
WrapSquare
How to wrap the text
public static final int
WrapByPoints
public static final int
WrapNone
public static final int
WrapTopBottom
public static final int
WrapThrough
public static final int
AlignLeft
How to align the text
public static final int
AlignCenter
public static final int
AlignRight
public static final int
AlignJustify
protected TextRun
_txtrun
Low-level object which holds actual text and format data
protected EscherTextboxWrapper
_txtbox
Escher container which holds text attributes such as TextHeaderAtom, TextBytesAtom ot TextCharsAtom, StyleTextPropAtom etc.
private boolean
_missingTextRecords
Is the TextBox missing the text records which actually store the text?
Constructors Summary
protected TextBox(EscherContainerRecord escherRecord, Shape parent)
Create a TextBox object and initialize it from the supplied Record container.

param
escherRecord EscherSpContainer container which holds information about this shape
param
parent the parent of the shape


                                                
       
        super(escherRecord, parent);

        EscherTextboxRecord textbox = (EscherTextboxRecord)Shape.getEscherChild(_escherContainer, EscherTextboxRecord.RECORD_ID);
        _txtbox = new EscherTextboxWrapper(textbox);
    
public TextBox(Shape parent)
Create a new TextBox. This constructor is used when a new shape is created.

param
parent the parent of this Shape. For example, if this text box is a cell in a table then the parent is Table.

        super(null, parent);
        _escherContainer = createSpContainer(parent instanceof ShapeGroup);
    
public TextBox()
Create a new TextBox. This constructor is used when a new shape is created.

        this(null);
    
Methods Summary
protected voidafterInsert(org.apache.poi.hslf.model.Sheet sh)
When a textbox is added to a sheet we need to tell upper-level PPDrawing about it.

param
sh the sheet we are adding to

        PPDrawing ppdrawing = sh.getPPDrawing();
        ppdrawing.addTextboxWrapper(_txtbox);
        // Ensure the escher layer knows about the added records 
        try {
            _txtbox.writeOut(null);
        } catch (IOException e){
            throw new HSLFException(e);
        }
        if(getAnchor().equals(new java.awt.Rectangle())) resizeToFitText();
    
protected org.apache.poi.ddf.EscherContainerRecordcreateSpContainer(boolean isChild)
Create a new textBox and initialize internal structures

return
the created EscherContainerRecord which holds shape data

        EscherContainerRecord spcont = super.createSpContainer(isChild);

        EscherSpRecord spRecord = spcont.getChildById(EscherSpRecord.RECORD_ID);
        short type = (ShapeTypes.TextBox << 4) | 0x2;
        spRecord.setOptions(type);

        //set default properties for a textbox
        EscherOptRecord opt = (EscherOptRecord)getEscherChild(spcont, EscherOptRecord.RECORD_ID);
        setEscherProperty(opt, EscherProperties.TEXT__TEXTID, 0);

        setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, 0x8000004);
        setEscherProperty(opt, EscherProperties.FILL__FILLBACKCOLOR, 0x8000000);
        setEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST, 0x100000);
        setEscherProperty(opt, EscherProperties.LINESTYLE__COLOR, 0x8000001);
        setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x80000);
        setEscherProperty(opt, EscherProperties.SHADOWSTYLE__COLOR, 0x8000002);

        //create EscherTextboxWrapper
        _txtbox = new EscherTextboxWrapper();

        TextHeaderAtom tha = new TextHeaderAtom();
        tha.setParentRecord(_txtbox); // TextHeaderAtom is parent aware
        _txtbox.appendChildRecord(tha);

        TextCharsAtom tca = new TextCharsAtom();
        _txtbox.appendChildRecord(tca);

        StyleTextPropAtom sta = new StyleTextPropAtom(0);
        _txtbox.appendChildRecord(sta);

        _txtrun = new TextRun(tha,tca,sta);
        _txtrun.setText("");
        spcont.addChildRecord(_txtbox.getEscherRecord());

        return spcont;
    
public intgetMarginBottom()
Returns the distance (in points) between the bottom of the text frame and the bottom of the inscribed rectangle of the shape that contains the text. Default value is 1/20 inch.

return
the botom margin

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.TEXT__TEXTBOTTOM);
        int val = prop == null ? EMU_PER_INCH/20 : prop.getPropertyValue();
        return val/EMU_PER_POINT;
    
public intgetMarginLeft()
Returns the distance (in EMUs) between the left edge of the text frame and the left edge of the inscribed rectangle of the shape that contains the text. Default value is 1/10 inch.

return
the left margin

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.TEXT__TEXTBOTTOM);
        int val = prop == null ? EMU_PER_INCH/10 : prop.getPropertyValue();
        return val/EMU_PER_POINT;
    
public intgetMarginRight()
Returns the distance (in EMUs) between the right edge of the text frame and the right edge of the inscribed rectangle of the shape that contains the text. Default value is 1/10 inch.

return
the right margin

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.TEXT__TEXTRIGHT);
        int val = prop == null ? EMU_PER_INCH/10 : prop.getPropertyValue();
        return val/EMU_PER_POINT;
    
public intgetMarginTop()
Returns the distance (in EMUs) between the top of the text frame and the top of the inscribed rectangle of the shape that contains the text. Default value is 1/20 inch.

return
the top margin

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.TEXT__TEXTTOP);
        int val = prop == null ? EMU_PER_INCH/20 : prop.getPropertyValue();
        return val/EMU_PER_POINT;
    
public java.lang.StringgetText()
Returns the text contained in this text frame.

return
the text string for this textbox.

        return _txtrun == null ? null : _txtrun.getText();
    
public intgetTextId()

return
id for the text.

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.TEXT__TEXTID);
        return prop == null ? 0 : prop.getPropertyValue();
    
public org.apache.poi.hslf.model.TextRungetTextRun()

return
the TextRun object for this text box

         return _txtrun;
     
public intgetVerticalAlignment()
Returns the type of vertical alignment for the text. One of the Anchor* constants defined in this class.

return
the type of alignment

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.TEXT__ANCHORTEXT);
        int valign;
        if (prop == null){
            int type = getTextRun().getRunType();
            switch (type){
                case TextHeaderAtom.TITLE_TYPE:
                case TextHeaderAtom.CENTER_TITLE_TYPE:
                    valign = TextBox.AnchorMiddle;
                    break;
                default:
                    valign = TextBox.AnchorTop;
                    break;
            }
        } else {
            valign = prop.getPropertyValue();
        }
        return valign;
    
public intgetWordWrap()
Returns the value indicating word wrap. One of the Wrap* constants defined in this class.

return
the value indicating word wrap

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.TEXT__WRAPTEXT);
        return prop == null ? WrapSquare : prop.getPropertyValue();
    
private voidinitTextRun()

        OutlineTextRefAtom ota = null;
        
        // Find the interesting child records 
        Record[] child = _txtbox.getChildRecords();
        for (int i = 0; i < child.length; i++) {
            if (child[i] instanceof OutlineTextRefAtom) {
                ota = (OutlineTextRefAtom)child[i];
                break;
            }
        }

        Sheet sheet = getSheet();
        TextRun[] runs = sheet.getTextRuns();
        if (ota != null) {
            int idx = ota.getTextIndex();
            if(idx < runs.length) _txtrun = runs[idx];
            if(_txtrun == null) {
                logger.log(POILogger.WARN, "text run not found for OutlineTextRefAtom.TextIndex=" + idx);
            }
        } else {
            int shapeId = _escherContainer.getChildById(EscherSpRecord.RECORD_ID).getShapeId();
            if(runs != null) for (int i = 0; i < runs.length; i++) {
                if(runs[i].getShapeId() == shapeId){
                    _txtrun = runs[i];
                    break;
                }
            }
            if(_txtrun == null) {
                logger.log(POILogger.WARN, "text run not found for shapeId=" + shapeId);
            }
        }

    
public voidresizeToFitText()
Adjust the size of the TextBox so it encompasses the text inside it.

        try{
        FontRenderContext frc = new FontRenderContext(null, true, true);
        RichTextRun rt = _txtrun.getRichTextRuns()[0];
        int size = rt.getFontSize();
        int style = 0;
        if (rt.isBold()) style |= Font.BOLD;
        if (rt.isItalic()) style |= Font.ITALIC;
        String fntname = rt.getFontName();
        Font font = new Font(fntname, style, size);

        TextLayout layout = new TextLayout(getText(), font, frc);
        int width = Math.round(layout.getAdvance());
        int height = Math.round(layout.getAscent());

        Dimension txsize = new Dimension(width, height);
        java.awt.Rectangle anchor = getAnchor();
        anchor.setSize(txsize);
        setAnchor(anchor);
        } catch (Exception e){
            e.printStackTrace();

        }
    
public voidsetBackgroundColor(java.awt.Color color)
The color used to fill this shape.

param
color the background color

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB();
        setEscherProperty(opt, EscherProperties.FILL__FILLBACKCOLOR, rgb);
    
public voidsetMarginBottom(int margin)
Sets the botom margin.

see
#getMarginBottom()
param
margin the bottom margin

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        setEscherProperty(opt, EscherProperties.TEXT__TEXTBOTTOM, margin*EMU_PER_POINT);
    
public voidsetMarginLeft(int margin)
Sets the left margin.

see
#getMarginLeft()
param
margin the left margin

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        setEscherProperty(opt, EscherProperties.TEXT__TEXTLEFT, margin*EMU_PER_POINT);
    
public voidsetMarginRight(int margin)
Sets the right margin.

see
#getMarginRight()
param
margin the right margin

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        setEscherProperty(opt, EscherProperties.TEXT__TEXTRIGHT, margin*EMU_PER_POINT);
    
public voidsetMarginTop(int margin)
Sets the top margin.

see
#getMarginTop()
param
margin the top margin

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        setEscherProperty(opt, EscherProperties.TEXT__TEXTTOP, margin*EMU_PER_POINT);
    
public voidsetSheet(org.apache.poi.hslf.model.Sheet sheet)

        _sheet = sheet;

        // Initialize _txtrun object.
        // (We can't do it in the constructor because the sheet
        //  is not assigned then, it's only built once we have
        //  all the records)
        if(_txtrun == null) initTextRun();
        if(_txtrun == null) {
        	// No text records found, skip
        	_missingTextRecords = true;
        	return;
        } else {
        	_missingTextRecords = false;
        }
        
        // Supply the sheet to our child RichTextRuns
        _txtrun.setSheet(sheet);
        RichTextRun[] rt = _txtrun.getRichTextRuns();
        for (int i = 0; i < rt.length; i++) {
            rt[i].supplySlideShow(_sheet.getSlideShow());
        }
    
public voidsetText(java.lang.String text)
Sets the text contained in this text frame.

param
text the text string used by this object.

        _txtrun.setText(text);
    
public voidsetTextId(int id)
Sets text ID

param
id of the text

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        setEscherProperty(opt, EscherProperties.TEXT__TEXTID, id);
    
public voidsetVerticalAlignment(int align)
Sets the type of vertical alignment for the text. One of the Anchor* constants defined in this class.

param
align - the type of alignment

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        setEscherProperty(opt, EscherProperties.TEXT__ANCHORTEXT, align);
    
public voidsetWordWrap(int wrap)
Specifies how the text should be wrapped

param
wrap the value indicating how the text should be wrapped

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        setEscherProperty(opt, EscherProperties.TEXT__WRAPTEXT, wrap);