Horizontally aligning dynamically generated paragraph text

Hi,

I am trying to dynamically generate a horizontally centered paragraph but the TextState.HorizontalAlignment property fails. Could you help me with whats going wrong here.

Attaching below the code snippet and concerned pdf for the same.

Thanks

Certificate1d.pdf (428.5 KB)

        TextFragmentAbsorber bodyFragmentAbsorber = new TextFragmentAbsorber("{this place is reserved for the body fields}");
        doc.Pages.Accept(bodyFragmentAbsorber);
        TextFragmentCollection bodyFragmentCollection = bodyFragmentAbsorber.TextFragments;
        TextFragment bodyFragment = bodyFragmentCollection[1];

        String value = "Some string that is dynamically generated with \n as a new line char and many \n in the content" ;
        Char delimiter = '\n';
        String[] substrings = value.Split(delimiter);
        TextParagraph tempFragment = new TextParagraph();

        foreach (var substring in substrings)
        {
            TextFragment updatedBodyFragment = new TextFragment(substring);
            updatedBodyFragment.TextState.LineSpacing = 15;
            updatedBodyFragment.TextState.FontSize = bodyFragment.TextState.FontSize;
            updatedBodyFragment.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
            updatedBodyFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Segoe UI");
            updatedBodyFragment.TextState.ForegroundColor = bodyFragment.TextState.ForegroundColor;
            tempFragment.AppendLine(updatedBodyFragment);
        }

        bodyFragment.Text = String.Empty;

        tempFragment.Position = new Position(bodyFragment.Position.XIndent, bodyFragment.Position.YIndent - bodyFragment.TextState.FontSize - bodyFragment.TextState.LineSpacing); 
   
        TextBuilder bodyTextBuilder = new TextBuilder(bodyFragment.Page);

        bodyTextBuilder.AppendParagraph(tempFragment);

@Heuristix,

Please send us your output PDF and let us know which Aspose.PDF for .NET API version you are using. Kindly highlight the expected position of the text with the help of a snapshot. Your response is awaited.

Hi,
I managed to find a workaround for this. Thank you for the quick reply and support although. Am sharing the working code for the same.

    TextFragmentAbsorber bodyFragmentAbsorber = new TextFragmentAbsorber("{this place is reserved for the body fields}");
    doc.Pages.Accept(bodyFragmentAbsorber);
    TextFragmentCollection bodyFragmentCollection = bodyFragmentAbsorber.TextFragments;
    TextFragment bodyFragment = bodyFragmentCollection[1];

    String value = "Some string that is dynamically generated with \n as a new line char and many \n in the content" ;
    Char delimiter = '\n';
    String[] substrings = value.Split(delimiter);
    TextParagraph tempFragment = new TextParagraph();
    tempFragment.Rectangle = new Aspose.Pdf.Rectangle(0 , 0 , 820 , 1000); 
    //820 being my page width and 1000 approx height pf paragraph container
    tempFragment.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

    foreach (var substring in substrings)
    {
        TextFragment updatedBodyFragment = new TextFragment(substring);
        updatedBodyFragment.TextState.LineSpacing = 15;
        updatedBodyFragment.TextState.FontSize = bodyFragment.TextState.FontSize;
        updatedBodyFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Segoe UI");
        updatedBodyFragment.TextState.ForegroundColor = bodyFragment.TextState.ForegroundColor;
        tempFragment.AppendLine(updatedBodyFragment);
    }

    bodyFragment.Text = String.Empty;

    tempFragment.Position = new Position(0 , bodyFragment.Position.YIndent - bodyFragment.TextState.FontSize - bodyFragment.TextState.LineSpacing); 

    TextBuilder bodyTextBuilder = new TextBuilder(bodyFragment.Page);

    bodyTextBuilder.AppendParagraph(tempFragment);

@Heuristix,

It is nice to hear from you that the problem has been resolved. Please feel free to let us know in case of any further assistance or queries.