The application I am working needs to have functionality to place text that is wrapped to a given width and placed at different coordinates on a page.
Some of the text needs to centered horizontally.
I am able to place the text correctly and get the text to wrap to multiple lines.
I am not sure how to obtain a pdf where all of the wrapped lines are centered.
The following code will create a pdf using a long string that is wrapped to two lines (segments). The first segment appears to be centered. The second segment is positioned to start on the left (left alignment).
I have a couple of quesitons regarding the following code:
I am using the TextParagraph class. I set both the Horizontal Alignment and Vertical Alignment of the object. Does this setting override all of Text Fragments that are added to the Text Paragraph?
I created a TextFragment. I also set the Horizontal and Vertical Alignment of the Text Fragment. Does this setting override the setting of the TextParagraph that it is added to?
Is the Vertical Alignment value of the TextFragment ignored when it is added to a TextParagraph?
Is there a way to parse thru the segments of the TextParagraph at any point within this code prior to writing the pdf?
I have added an attachment that results from this code. Note the difference in Horizontal Alignment between the two lines.
public static void CheckHorizontalAlignment()
{
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Aspose.Pdf.Page p = doc.Pages.Add();
p.PageInfo.Height = 11.0f * 72.0f;
p.PageInfo.Width = 8.5f * 72.0f;
p.PageInfo.Margin.Bottom = 0.0f;
p.PageInfo.Margin.Top = 0.0f;
p.PageInfo.Margin.Left = 0.0f;
p.PageInfo.Margin.Right = 0.0f;
Aspose.Pdf.Rectangle rect = new Rectangle(114.235046386719, 772.11953830719, 509.260620117188,
787.40048122406);
var txtBldr = new TextBuilder(p);
var txtPara = new TextParagraph
{
FirstLineIndent = 0.0f,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
SubsequentLinesIndent = 0.0f,
FormattingOptions = { WrapMode = TextFormattingOptions.WordWrapMode.ByWords },
Rectangle = rect,
Rotation = 0.0f
};
var tf = new TextFragment("REVISION AND APPROVAL OF THIS STANDARD NOTE IS CONTROLLED BY A DATA MANAGEMENT SYSTEM. COPIES OF THIS RECORD ARE NOT CONTROLLED. INFORMATION HEREON MAY NOT REFLECT THE LATEST APPROVED REVISION / VERSION OF THIS STANDARD NOTE. ") { ZIndex = 1000 };
tf.TextState.Font = FontRepository.FindFont("Arial");
tf.TextState.FontSize = 6.34047174f;
tf.TextState.ForegroundColor = Color.Black;
tf.TextState.FontStyle= FontStyles.Regular;
tf.TextState.HorizontalAlignment = HorizontalAlignment.Center;
tf.VerticalAlignment = VerticalAlignment.Center;
tf.TextState.Rotation = 0.0f;
tf.TextState.HorizontalScaling = 90.5f;
tf.TextState.LineSpacing = 1.30f;
txtPara.BeginEdit();
txtPara.AppendLine(tf);
txtPara.EndEdit();
txtBldr.AppendParagraph(txtPara);
doc.Save("C:\\temp\\HorzontalAlignment.pdf");
}
HorzontalAlignment.pdf (47.2 KB)