Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Create text box and set color
The example shows you how to create text box and set color.
import com.independentsoft.office.vml.ConnectType; import com.independentsoft.office.vml.HorizontalPosition; import com.independentsoft.office.vml.Position; import com.independentsoft.office.vml.RelativeHorizontalPosition; import com.independentsoft.office.vml.RelativeVerticalPosition; import com.independentsoft.office.vml.ShapePath; import com.independentsoft.office.vml.ShapeTemplate; import com.independentsoft.office.vml.Stroke; import com.independentsoft.office.vml.StrokeJoinStyle; import com.independentsoft.office.vml.TextBox; import com.independentsoft.office.vml.VerticalPosition; import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.VmlObject; import com.independentsoft.office.word.WordDocument; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); Run textBoxRun = new Run(); textBoxRun.addText("Simple text inside a textbox"); Paragraph textBoxParagraph = new Paragraph(); textBoxParagraph.add(textBoxRun); TextBox textBox = new TextBox(); textBox.getContent().add(textBoxParagraph); ShapeTemplate shapeTemplate = new ShapeTemplate(); shapeTemplate.setCoordinateSpaceSize("21600,21600"); shapeTemplate.setEdgePath("m,l,21600r21600,l21600,xe"); shapeTemplate.setPreferRelativeResize(true); Stroke stroke = new Stroke(); stroke.setJoinStyle(StrokeJoinStyle.MITER); ShapePath path = new ShapePath(); path.setEnableGradient(true); path.setConnectionPointType(ConnectType.FOUR); shapeTemplate.getContent().add(stroke); shapeTemplate.getContent().add(path); com.independentsoft.office.vml.Shape shape = new com.independentsoft.office.vml.Shape(); shape.setStyle(new com.independentsoft.office.vml.ShapeStyle()); shape.getStyle().setPosition(Position.ABSOLUTE); shape.getStyle().setLeftMargin("0"); shape.getStyle().setTopMargin("0"); shape.getStyle().setWidth("200pt"); shape.getStyle().setHeight("195pt"); shape.getStyle().setHorizontalPosition(HorizontalPosition.CENTER); shape.getStyle().setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN); shape.getStyle().setVerticalPosition(VerticalPosition.CENTER); shape.getStyle().setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN); shape.setFillColor("yellow"); shape.getContent().add(textBox); VmlObject vmlObject = new VmlObject(); vmlObject.getContent().add(shapeTemplate); vmlObject.getContent().add(shape); Run run1 = new Run(); run1.add(vmlObject); Paragraph paragraph1 = new Paragraph(); paragraph1.add(run1); doc.getBody().add(paragraph1); doc.save("c:\\test\\output.docx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*