Knowledgebase (2311)
Children categories
This article demonstrates how to set text horizontal and vertical alignment within a shape using Spire.Presentation for Java.
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class SetTextAlignment {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//Add a shape
IAutoShape textShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50, 50, 400, 200));
textShape.getShapeStyle().getLineColor().setColor(Color.DARK_GRAY);
textShape.getFill().setFillType(FillFormatType.NONE);
//Remove the default paragraphs
textShape.getTextFrame().getParagraphs().clear();
//Add a paragraph and append some text to it
textShape.getTextFrame().getParagraphs().append(new ParagraphEx());
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().append(new PortionEx("Text Alignment"));
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setFontHeight(20f);
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Arial"));
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().getSolidColor().setColor(Color.BLACK);
//Set text horizontal alignment to right
textShape.getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
//Set text vertical alignment to bottom
textShape.getTextFrame().setAnchoringType(TextAnchorType.BOTTOM);
//Save to file
presentation.saveToFile("output/TextAlignment.pptx",FileFormat.PPTX_2013);
}
}

Published in
Paragraph and Text
Tagged under
This article demonstrates how to get the position of text in PowerPoint in Java using Spire.Presentation for Java.
import com.spire.presentation.IAutoShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import java.awt.geom.Point2D;
public class GetPositionOfText {
public static void main(String []args) throws Exception {
//Create a Presentation instance
Presentation ppt = new Presentation();
//Load a PowerPoint document
ppt.loadFromFile("Sample.pptx");
//Get the first slide
ISlide slide = ppt.getSlides().get(0);
//Get the first shape
IAutoShape shape = (IAutoShape)slide.getShapes().get(0);
//Get location of text in the shape
Point2D location =shape.getTextFrame().getTextLocation();
//Print out the x and y coordinates of the location relative to slide
String point1="Text's position relative to Slide: x= "+location.getX()+" y = "+location.getY();
System.out.println(point1);
//Print out the x and y coordinates of the location relative to shape
String point2 = "Text's position relative to shape: x= " + (location.getX() - shape.getLeft()) + " y = " + (location.getY() - shape.getTop());
System.out.println(point2);
}
}
Output:

Published in
Paragraph and Text
Tagged under
In the earlier tutorial, we have given a brief introduction on how to insert Textbox in Word and this article will demonstrate how to position the text vertically in a text box using Spire.Doc for .NET.
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
namespace WordTextbox
{
class Program
{
static void Main(string[] args)
{
// Instantiate document object
Document document = new Document();
//Add a section
Section section = document.AddSection();
//Set the margin
section.PageSetup.Margins.Left = 90;
section.PageSetup.Margins.Right = 90;
Paragraph paragraph = section.AddParagraph();
//Add texbox 1
TextBox textBox1 = paragraph.AppendTextBox(section.PageSetup.Margins.Left - 20, section.PageSetup.PageSize.Height + 20);
//Fix the position of textbox
textBox1.Format.HorizontalOrigin = HorizontalOrigin.Page;
textBox1.Format.HorizontalPosition = 0;
textBox1.Format.VerticalPosition = -10f;
textBox1.Format.VerticalOrigin = VerticalOrigin.Page;
//Set the text vertically
textBox1.Format.TextAnchor = ShapeVerticalAlignment.Center;
textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRight;
//Add text and set the font
Paragraph textboxPara1 = textBox1.Body.AddParagraph();
TextRange txtrg = textboxPara1.AppendText("Name_______Number_________Class__________");
txtrg.CharacterFormat.FontName = "Arial";
txtrg.CharacterFormat.FontSize = 10;
txtrg.CharacterFormat.TextColor = System.Drawing.Color.Black;
textboxPara1.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Save the document
document.SaveToFile("Result.docx");
}
}
}
VB.NET
Namespace WordTextbox
Class Program
Private Shared Sub Main(ByVal args() As String)
Dim document As Document = New Document
Dim section As Section = document.AddSection
section.PageSetup.Margins.Left = 90
section.PageSetup.Margins.Right = 90
Dim paragraph As Paragraph = section.AddParagraph
Dim textBox1 As TextBox = paragraph.AppendTextBox((section.PageSetup.Margins.Left - 20), (section.PageSetup.PageSize.Height + 20))
textBox1.Format.HorizontalOrigin = HorizontalOrigin.Page
textBox1.Format.HorizontalPosition = 0
textBox1.Format.VerticalPosition = -10!
textBox1.Format.VerticalOrigin = VerticalOrigin.Page
textBox1.Format.TextAnchor = ShapeVerticalAlignment.Center
textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRight
Dim textboxPara1 As Paragraph = textBox1.Body.AddParagraph
Dim txtrg As TextRange = textboxPara1.AppendText("Name_______Number_________Class__________")
txtrg.CharacterFormat.FontName= "Arial"
txtrg.CharacterFormat.FontSize = 10
txtrg.CharacterFormat.TextColor = System.Drawing.Color.Black
textboxPara1.Format.HorizontalAlignment = HorizontalAlignment.Center
document.SaveToFile("Result.docx")
End Sub
End Class
End Namespace
Output

Published in
Textbox
Tagged under