I am trying to align dynamic string in page center horizontally, I tried following code but it doesn’t seem to help much, for few string it seems good with below mentioned position however TitleCategory is dynamic string, can be n no of characters i don’t want to set hard coded positioning and HorizontalAlignment attribute is also not working.
Thanks for your inquiry. Please check the following code snippet to center-align text dynamically. Hopefully, it will help you to accomplish the task.
Document doc = new Document();
Page page = doc.Pages.Add();
// create a new text fragment with updated string that contains all necessary newline markers
string multilineText = "Aspose" + Environment.NewLine + "PDF" + Environment.NewLine + "For" + Environment.NewLine + ".NET";
TextFragment multilineFragment = new TextFragment(multilineText);
// set new text fragment properties if necessary
multilineFragment.TextState.Font = FontRepository.FindFont("Arial");
multilineFragment.TextState.FontSize = 14;
// create TextParagraph object
TextParagraph par = new TextParagraph();
// add new TextFragment to paragraph
par.AppendLine(multilineFragment);
// set paragraph Alignment properties
par.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
par.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
// add the TextParagraph using TextBuilder
TextBuilder textBuilder = new TextBuilder(page);
textBuilder.AppendParagraph(par);
// save the document
doc.Save(myDir + "multilinetext.pdf");
Please feel free to contact us for any further assistance.
Thanks for your feedback. If you want to align text horizontally one by one then please add each text instance as a separate paragraph and set its HorizontalAlignment Property. If issue persist then please share you sample code here, so we will look into it and provide you details accordingly.
Please feel free to contact us for any further assistance.
I tried the following code, but it moved the TitleCategory string to the bottom of the page instead of centering it. I need to show 2 more strings one by one in horizontal center on the page.
Example:
DP Biology, category 1
Dubai, United Arab Emirates
3-5 September 2014
byte[] byteArray =
file.OpenBinary();
MemoryStream streamTemplate = new MemoryStream(byteArray);
Aspose.Pdf.Document tempPDF = new Aspose.Pdf.Document(streamTemplate);
newDocument.Pages.Add(tempPDF.Pages);
Aspose.Pdf.Page firstPage = newDocument.Pages[1];
TextFragment textFragWorkShopTitle = new TextFragment(TitleCategory);
//Set text properties
textFragWorkShopTitle.TextState.FontSize = 25;
textFragWorkShopTitle.TextState.Font = FontRepository.FindFont("Helvetica");
textFragWorkShopTitle.TextState.FontStyle = FontStyles.Bold;
textFragWorkShopTitle.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black);
TextParagraph par = new TextParagraph();
par.AppendLine(textFragWorkShopTitle);
//set paragraph Alignment properties
par.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
TextBuilder textBuilder = new TextBuilder(firstPage);
//Append the text fragment to the PDF page
textBuilder.AppendText(par);
Thanks for your inquiry. Please note you need to use the VerticalAlignment property of TextParagraph to add text vertically in the center of the page. Moreover, to add more than one line, you can append as many lines as you need. Please check the following code for this purpose.
....
....
Aspose.Pdf.Page firstPage = newDocument.Pages[1];
TextFragment textFragWorkShopTitle = new TextFragment("DP Biology, category 1");
//Set text properties
textFragWorkShopTitle.TextState.FontSize = 25;
textFragWorkShopTitle.TextState.Font = FontRepository.FindFont("Helvetica");
textFragWorkShopTitle.TextState.FontStyle = FontStyles.Bold;
textFragWorkShopTitle.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black);
TextParagraph par = new TextParagraph();
par.AppendLine(textFragWorkShopTitle);
par.AppendLine("Dubai, United Arab Emirates");
par.AppendLine("3-5 September 2014");
//set paragraph Alignment properties
par.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
par.VeriticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
TextBuilder textBuilder = new TextBuilder(firstPage);
....
....
Please feel free to contact us for any further assistance.