Add Text to PDF using Aspose.PDF - Aligning the data(rates) right for data in pdf dynamically

Hi team,

I am putting data(rates) in a pdf dynamically. But they need to aligned from the right.
The data are like below
13.00%
120.00%
70.00%
160.00%
5.00%

But the requirement is that the data should be “aligned from right” like below (‘%’ sign should be on same straight line)
13.00%
120.00%
70.00%
160.00%
5.00%

I am using the below code to align the data
textStamp.XIndent=500;
textStamp.YIndent = fragment.Rectangle.LLY;

I have attached snip shot of the pdf.
DataAligning.JPG (43.4 KB)

Below is the google drive link for the pdf. The rates in the pdf comes dynamically from database.

Could you please help me asap. Thank you.

@santosh.g2047

Thank you for contacting support.

Would you please also share the source code that you are using to add this data so that we may replicate the scenario and help you out.

I have copied all the code. Please only look for “growthSuite Life”. You can ignore other cases

public byte[] FillForm(string IPAddress, string SessionID, string UserName, string pdfPath, IDictionary<string, string> values)
{
var document = GetPdfDocument(_fileSystem.GetFullPath(pdfPath));
// setting date and rates
foreach (var value in values)
{

            bool rateMapper = false;
            bool dateMapper = false;
            if (!value.Value.Contains(";"))
                continue;

            var rateMapperSplittedValue = value.Value.Split(';');
            if (rateMapperSplittedValue.Count() < 4)
                continue;

            if (value.Value.Contains("ProductRateMapper"))
                rateMapper = true;

            if (value.Value.Contains("EffectiveDateMapper"))
                dateMapper = true;

            if (rateMapper)
            {
                if (rateMapperSplittedValue[0].Contains("ChoiceFour"))
                {
                    try
                    {
                        Page pdfPage = (Page)document.Pages[1];
                        // Create text fragment
                        TextFragment textFragment = new TextFragment(rateMapperSplittedValue[3]);
                        if (rateMapperSplittedValue[2] == "ChoiceFour (Recruit)")
                        {
                            textFragment.Position = new Position(100, 420);
                        }
                        if (rateMapperSplittedValue[2] == "ChoiceFour Flyer")
                        {
                            textFragment.Position = new Position(100, 420);
                        }
                        if (rateMapperSplittedValue[2] == "ChoiceFour Print Ad")
                        {
                            textFragment.Position = new Position(100, 220);
                        }
                        if (rateMapperSplittedValue[2] == "ChoiceFour Mailer")
                        {
                            textFragment.Position = new Position(100, 210);
                        }
                        // Set text properties
                        textFragment.TextState.FontSize = 20;
                        //textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");
                        textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black);

                        // Create TextBuilder object
                        TextBuilder textBuilder = new TextBuilder(pdfPage);

                        // Append the text fragment to the PDF page
                        textBuilder.AppendText(textFragment);

                    }
                    catch (Exception e)
                    {
                        string CallParameters() =>$"FillForm";
                        Logger.Error(CallParameters(), e);
                    }
                }
                else
                {
                    var rateYearString = Regex.Match(rateMapperSplittedValue[0], @"\d+").Value;
                    int rateYear = 0;
                    int.TryParse(rateYearString, out rateYear);
                    if (rateYear > 0)
                    {
                        string searchText = "";
                        bool findSecondOccurance = false;
                        bool insertSpace = false;
                        bool growthSuiteX = false;
                        bool secureSuiteLifeXindex = false;
                        if (rateMapperSplittedValue[0].Contains("Confidence Income"))
                            insertSpace = true;
                        if (rateMapperSplittedValue[0] == "Confidence Income Life Only - Male 65")
                        {
                            searchText = "M 65";
                        }
                        else if (rateMapperSplittedValue[0] == "Confidence Income Life Only - Female 65")
                        {
                            searchText = "F 65";
                        }
                        else if (rateMapperSplittedValue[0] == "Confidence Income Joint Life - M 65/F 65")
                        {
                            searchText = "M 65 / F 65";
                        }
                        else if (rateMapperSplittedValue[0] == "Growth SuiteLife™ 1-Year Interest Account")
                        {
                            searchText = "1-Year Interest Account";
                            growthSuiteX = true;
                        }
                        else if (rateMapperSplittedValue[0] == "Growth SuiteLife™ Dynamo® 1 Year Point to Point Participation Index")
                        {
                            searchText = "Multi-Asset Index";
                            growthSuiteX = true;
                        }
                        else if (rateMapperSplittedValue[0] == "Growth SuiteLife™ GSL 1YR POINT-POINT PART")
                        {
                            searchText = "1-Year Point-to-Point Participation Index Account";
                            growthSuiteX = true;
                        }
                        else if (rateMapperSplittedValue[0] == "Growth SuiteLife™ S&P 500® 1 Year Average Participation")
                        {
                            searchText = "1-Year Monthly Average Participation Index Account";
                            growthSuiteX = true;
                        }
                        else if (rateMapperSplittedValue[0] == "Growth SuiteLife™ S&P 500® 1 Year Point to Point Cap Index Account")
                        {
                            searchText = "1-Year Point-to-Point Cap Index Account";
                            growthSuiteX = true;
                        }

                        else if (rateMapperSplittedValue[0] == "Secure SuiteLife™ S&P 500® 1 Year Point to Point Cap Index Account")
                        {
                            searchText = "1-Year Point—to-Point Cap Index Account";
                            secureSuiteLifeXindex = true;
                        }
                        else if (rateMapperSplittedValue[0] == "Secure SuiteLife™ S&P 500® 1 Year Monthly Cap")
                        {
                            searchText = "1-Year Monthly Cap Index Account";
                            secureSuiteLifeXindex = true;
                        }
                        else if (rateMapperSplittedValue[0] == "Secure SuiteLife™ 1-Year Monthly Average Participation Index Account")
                        {
                            searchText = "1-Year Monthly Average Participation Index Account";
                            secureSuiteLifeXindex = true;
                        }
                        else if (rateMapperSplittedValue[0] == "Secure SuiteLife™ Dynamo® 1 Year Point to Point Participation Index")
                        {
                            searchText = "1-Year Point—to-Point Participation Index Account";
                            secureSuiteLifeXindex = true;
                        }
                        else if (rateMapperSplittedValue[0] == "Secure SuiteLife™ 1-Year Interest Account")
                        {
                            searchText = "1-Year Interest Account";
                            secureSuiteLifeXindex = true;
                        }
                        if (rateMapperSplittedValue[0] == "Confidence Income 20-Years Fixed Period")
                        {
                            findSecondOccurance = true;
                        }
                        bool firstFormat = false;
                        try
                        {
                            TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(rateYear + "-Year:");
                            if (!string.IsNullOrEmpty(searchText))
                                textFragmentAbsorber = new TextFragmentAbsorber(searchText);
                            TextSearchOptions textSearchOptions = new TextSearchOptions(true);
                            textFragmentAbsorber.TextSearchOptions = textSearchOptions;
                            int count = 1;
                            foreach (Page page in document.Pages)
                            {
                                if (count == 2)
                                    continue;
                                count++;
                                page.Accept(textFragmentAbsorber);
                                TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
                                if (textFragmentCollection.Count > 0)
                                {
                                    firstFormat = true;
                                    TextFragment fragment = textFragmentCollection[1];

                                    // Create text stamp
                                    TextStamp textStamp = new TextStamp(rateMapperSplittedValue[3]);

                                    // Set origin
                                    textStamp.XIndent = fragment.Rectangle.LLX + fragment.Rectangle.Width + 5;

                                    textStamp.YIndent = fragment.Rectangle.LLY;

                                    // Set text properties
                                    textStamp.TextState.FontSize = fragment.TextState.FontSize;
                                    if (growthSuiteX || secureSuiteLifeXindex)
                                    {
                                        textStamp.XIndent = 500;
                                        textStamp.TextAlignment = HorizontalAlignment.Right;
                                        textStamp.TextState.FontSize = 16;
                                    }
                                    textStamp.TextState.ForegroundColor = fragment.TextState.ForegroundColor;
                                    // Add stamp to particular page
                                    document.Pages[fragment.Page.Number].AddStamp(textStamp);
                                }

                            }
                        }
                        catch (Exception e)
                        {
                            string CallParameters() => $"FillForm";
                            Logger.Error(CallParameters(), e);
                        }

                        bool secondFormat = false;
                        if (!firstFormat)
                        {
                            try
                            {
                                TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(rateYear + " Year");
                                TextSearchOptions textSearchOptions = new TextSearchOptions(true);
                                textFragmentAbsorber.TextSearchOptions = textSearchOptions;
                                int count = 1;
                                foreach (Page page in document.Pages)
                                {
                                    if (count == 2)
                                        continue;
                                    count++;
                                    page.Accept(textFragmentAbsorber);
                                    TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
                                    if (textFragmentCollection.Count > 0)
                                        secondFormat = true;
                                    TextFragment fragment = textFragmentCollection[1];
                                    if (findSecondOccurance && textFragmentCollection.Count > 2)
                                        fragment = textFragmentCollection[3];
                                    // Create text stamp
                                    string textValue = "";
                                    if (insertSpace)
                                        textValue = " " + rateMapperSplittedValue[3];
                                    else
                                        textValue = rateMapperSplittedValue[3];
                                    TextStamp textStamp = new TextStamp(textValue);

                                    // Set origin
                                    textStamp.XIndent = fragment.Rectangle.LLX + fragment.Rectangle.Width + 5;
                                    textStamp.YIndent = fragment.Rectangle.LLY;

                                    // Set text properties
                                    textStamp.TextState.FontSize = fragment.TextState.FontSize;
                                    textStamp.TextState.ForegroundColor = fragment.TextState.ForegroundColor;
                                    // Add stamp to particular page
                                    document.Pages[fragment.Page.Number].AddStamp(textStamp);
                                }
                            }
                            catch (Exception e)
                            {
                                string CallParameters() => $"FillForm";
                                Logger.Error(CallParameters(), e);
                            }
                        }

                    }
                }

            }

            if (dateMapper)
            {
                bool firstDateFormat = false;
                try
                {
                    TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Rates are based on the Company’s");
                    TextSearchOptions textSearchOptions = new TextSearchOptions(true);
                    textFragmentAbsorber.TextSearchOptions = textSearchOptions;

                    foreach (Page page in document.Pages)
                    {
                        page.Accept(textFragmentAbsorber);
                        TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
                        TextFragment fragment = textFragmentCollection[1];
                        if (textFragmentCollection.Count > 0)
                            firstDateFormat = true;
                        // Create text stamp
                        TextStamp textStamp = new TextStamp(rateMapperSplittedValue[3]);

                        // Set origin
                        textStamp.XIndent = fragment.Rectangle.LLX + fragment.Rectangle.Width + 2;
                        textStamp.YIndent = fragment.Rectangle.LLY;

                        // Set text properties
                        textStamp.TextState.FontSize = fragment.TextState.FontSize - 1;
                        textStamp.TextState.ForegroundColor = fragment.TextState.ForegroundColor;
                        // Add stamp to particular page
                        document.Pages[fragment.Page.Number].AddStamp(textStamp);
                    }
                }
                catch (Exception e)
                {
                    string CallParameters() => $"FillForm";
                    Logger.Error(CallParameters(), e);
                }

                bool secondDateFormat = false;
                if (!firstDateFormat)
                {
                    try
                    {
                        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Rates current as of");
                        TextSearchOptions textSearchOptions = new TextSearchOptions(true);
                        textFragmentAbsorber.TextSearchOptions = textSearchOptions;

                        foreach (Page page in document.Pages)
                        {
                            page.Accept(textFragmentAbsorber);
                            TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
                            TextFragment fragment = textFragmentCollection[1];
                            if (textFragmentCollection.Count > 0)
                                secondDateFormat = true;
                            // Create text stamp
                            TextStamp textStamp = new TextStamp(rateMapperSplittedValue[3]);

                            // Set origin
                            textStamp.XIndent = fragment.Rectangle.LLX + fragment.Rectangle.Width + 2;
                            textStamp.YIndent = fragment.Rectangle.LLY;

                            // Set text properties
                            textStamp.TextState.FontSize = fragment.TextState.FontSize - 1;
                            textStamp.TextState.ForegroundColor = fragment.TextState.ForegroundColor;
                            // Add stamp to particular page
                            document.Pages[fragment.Page.Number].AddStamp(textStamp);
                        }
                    }
                    catch (Exception e)
                    {
                        string CallParameters() => $"FillForm";
                        Logger.Error(CallParameters(), e);
                    }
                }

                if (!firstDateFormat && !secondDateFormat)
                {
                    try
                    {
                        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Rates as of");
                        TextSearchOptions textSearchOptions = new TextSearchOptions(true);
                        textFragmentAbsorber.TextSearchOptions = textSearchOptions;

                        foreach (Page page in document.Pages)
                        {
                            page.Accept(textFragmentAbsorber);
                            TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
                            TextFragment fragment = textFragmentCollection[1];
                            //if (textFragmentCollection.Count > 0)
                            //    thirdDateFormat = true;
                            // Create text stamp
                            TextStamp textStamp = new TextStamp(rateMapperSplittedValue[3]);

                            // Set origin
                            textStamp.XIndent = fragment.Rectangle.LLX + fragment.Rectangle.Width + 2;
                            textStamp.YIndent = fragment.Rectangle.LLY;

                            // Set text properties
                            textStamp.TextState.FontSize = fragment.TextState.FontSize;
                            textStamp.TextState.ForegroundColor = fragment.TextState.ForegroundColor;
                            // Add stamp to particular page
                            document.Pages[fragment.Page.Number].AddStamp(textStamp);
                        }
                    }
                    catch (Exception e)
                    {
                        string CallParameters() => $"FillForm";
                        Logger.Error(CallParameters(), e);
                    }
                }
            }
        }
        var ms = new MemoryStream();
        document.Save(ms);
        return ms.GetBuffer();

@santosh.g2047

Thank you for sharing the code.

We are afraid that TextStamp class does not expose any such property that can be used to align the text with respect to last symbol in your text so you might try adding spaces based on your scenario if that may work.