Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JSpreadsheet
>
Tutorial
> Insert Text Box
The following example shows you how to insert a Text Box.
import com.independentsoft.office.Unit; import com.independentsoft.office.UnitType; import com.independentsoft.office.drawing.PresetGeometry; import com.independentsoft.office.drawing.SchemeColor; import com.independentsoft.office.drawing.SchemeColorValue; import com.independentsoft.office.drawing.ShapeType; import com.independentsoft.office.drawing.SolidFill; import com.independentsoft.office.drawing.TextParagraph; import com.independentsoft.office.drawing.TextRun; import com.independentsoft.office.spreadsheet.Workbook; import com.independentsoft.office.spreadsheet.Worksheet; import com.independentsoft.office.spreadsheet.drawing.ClientData; import com.independentsoft.office.spreadsheet.drawing.DrawingObjects; import com.independentsoft.office.spreadsheet.drawing.EndAnchorPoint; import com.independentsoft.office.spreadsheet.drawing.Shape; import com.independentsoft.office.spreadsheet.drawing.ShapeTextBody; import com.independentsoft.office.spreadsheet.drawing.StartAnchorPoint; import com.independentsoft.office.spreadsheet.drawing.TwoCellAnchor; public class Example { public static void main(String[] args) { try { Workbook book = new Workbook(); Worksheet sheet1 = new Worksheet(); TwoCellAnchor anchor = new TwoCellAnchor(); StartAnchorPoint start = new StartAnchorPoint(); start.setColumn(1); start.setColumnOffset(new Unit(0, UnitType.ENGLISH_METRIC_UNIT)); start.setRow(2); start.setRowOffset(new Unit(0, UnitType.ENGLISH_METRIC_UNIT)); EndAnchorPoint end = new EndAnchorPoint(); end.setColumn(5); end.setColumnOffset(new Unit(0, UnitType.ENGLISH_METRIC_UNIT)); end.setRow(7); end.setRowOffset(new Unit(0, UnitType.ENGLISH_METRIC_UNIT)); anchor.setStart(start); anchor.setEnd(end); Shape shape = new Shape(); shape.setID("1"); shape.setName("TextBox1"); shape.setTextBox(true); PresetGeometry presetGeometry = new PresetGeometry(ShapeType.RECTANGLE); shape.getShapeProperties().setPresetGeometry(presetGeometry); SolidFill solidFill = new SolidFill(); solidFill.setColorChoice(new SchemeColor(SchemeColorValue.ACCENT_6)); shape.getShapeProperties().setSolidFill(solidFill); TextRun run1 = new TextRun("TextBox body text."); TextParagraph paragraph1 = new TextParagraph(); paragraph1.getContent().add(run1); ShapeTextBody shapeTextBody = new ShapeTextBody(); shapeTextBody.getParagraphs().add(paragraph1); shape.setTextBody(shapeTextBody); anchor.setElement(shape); anchor.setClientData(new ClientData()); DrawingObjects drawingObjects = new DrawingObjects(); drawingObjects.getAnchors().add(anchor); sheet1.setDrawingObjects(drawingObjects); book.getSheets().add(sheet1); book.save("c:\\test\\output.xlsx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*