TOC content mix of two styles (sentence with few bold text and other normal) not working

Hi Team,

I am working on a requirement where merging multiple PDF document and creating table of contents page to navigate on click to respective page. TOC text contains mix of normal and bold text as below:-

Chapter 1: Marseilles—The Arrival …………………………………. 1

Chapter 2: Father and Son ……………………………………………… 8

Can you please help me in order to achieve this?

Thanks in advance!
Ajay

@ajayEY

Can you please share the code snippet that you have tried so far along with an expected output PDF document that you want to obtain? We will investigate and share our feedback with you.

Below is my code snippet:–
foreach (PDFMapper pMap in pdfMapper)
{
// Create Heading object
Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
heading2.StartNumber = Page_Start_cnt;
TextSegment segment2 = new TextSegment();
heading2.TocPage = tocPage;
heading2.Segments.Add(segment2);

                    // Specify the destination page for heading object
                    heading2.DestinationPage = doc.Pages[pMap.PageOffset + 1];

                    // Destination page
                    heading2.Top = doc.Pages[pMap.PageOffset + 1].Rect.Height;

                    string input = pMap.FundName

                    // Handling <b> tag
                    string pattern = @"<b>(.*?)</b>";
                    System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(input, pattern);

                    if (matches.Count > 0)
                    {
                        int currentIndex = 0;

                        foreach (System.Text.RegularExpressions.Match match in matches)
                        {
                            int startIndex = match.Index;
                            int length = match.Length;
                            string extractedText = match.Groups[1].Value;

                            // Add plain text before the current bold tag
                            if (startIndex > currentIndex)
                            {
                                string plainText = input.Substring(currentIndex, startIndex - currentIndex);
                                TextSegment plainSegment = new TextSegment(plainText);
                                plainSegment.TextState.Font = FontRepository.FindFont("Arial Nova Light");
                                plainSegment.TextState.FontSize = 9;
                                plainSegment.TextState.ForegroundColor = Color.FromArgb(65, 73, 86);
                                heading2.Segments.Add(plainSegment);
                            }

                            // Add the bold segment
                            TextSegment boldSegment = new TextSegment(extractedText);
                            boldSegment.TextState.FontStyle = FontStyles.Bold;
                            heading2.Segments.Add(boldSegment);

                            // Update the current index
                            currentIndex = startIndex + length;
                        }

                        // Add plain text after the last bold tag
                        if (currentIndex < input.Length)
                        {
                            string remainingText = input.Substring(currentIndex);
                            TextSegment remainingSegment = new TextSegment(remainingText);
                            remainingSegment.TextState.Font = FontRepository.FindFont("Arial Nova Light");
                            remainingSegment.TextState.FontSize = 9;
                            remainingSegment.TextState.ForegroundColor = Color.FromArgb(65, 73, 86);                                
                            heading2.Segments.Add(remainingSegment);
                        }
                    }
                    else
                    {
                        // If the pattern doesn't match, handle it here (e.g., add the entire input as plain text)
                        TextSegment plainSegment = new TextSegment(input);
                        plainSegment.TextState.Font = FontRepository.FindFont("Arial Nova Light");
                        plainSegment.TextState.FontSize = 9;
                        plainSegment.TextState.ForegroundColor = Color.FromArgb(65, 73, 86);
                        heading2.Segments.Add(plainSegment);
                    }

                    // Add page number to the TOC
                    heading2.TocPage.Paragraphs.Add(heading2);

                    // Increment the page number
                    Page_Start_cnt++;
                }

doc.Save(Merge_Output_fullPath);

Output :-
image.png (16.4 KB)

Problem 1 : Before bold text in text is faded out .
Problem 2: The page number is not visible when mixing normal and bold text together

@ajayEY

Thanks for sharing the sample code snippet. However, we are afraid that we could not execute it as it contained some missing classes. Is it possible for you to share complete code snippet and sample PDF for our reference? This would help us in investigating the issues and address them accordingly.

Below is the code and attached the PDF file where I am trying to generate TOC (mix if normal and bold text):-

using System.IO;
using Aspose.Pdf;
using System.Collections.Generic;
using System.Linq;
using Aspose.Pdf.Text;
public class PDFMapper
{
    public int PageOffset { get; set; }
    public string FundName { get; set; }
}

class Program
{
    static void Main()
    {
		int Page_Start_cnt;
       
        PDFMapper pdfMapper1 = new PDFMapper
        {
            PageOffset = 1,
            FundName = "Research and Publication Ethicsl"
        };

        PDFMapper pdfMapper2 = new PDFMapper
        {
            PageOffset = 8,
            FundName = "Philosophy and <b>Ethics</b>"
        };
		PDFMapper pdfMapper3 = new PDFMapper
        {
            PageOffset = 11,
            FundName = " <b>Research</b> Philosophy"
        };
		 List<PDFMapper> pdfMappers = new List<PDFMapper> { pdfMapper1, pdfMapper2,pdfMapper3 };
        Aspose.Pdf.Document doc = new Aspose.Pdf.Document("C:\PDF\testfile.pdf);

                    // Get access to first page of PDF file
                    Aspose.Pdf.Page tocPage = doc.Pages.Insert(1);

                    // Create object to represent TOC information
                    TocInfo tocInfo = new TocInfo();

                    //toc title
                    TextFragment title = new TextFragment("Research Methedologies");
                    title.TextState.FontSize = 12;
                    title.TextState.FontStyle = FontStyles.Bold;
                    title.TextState.Font = FontRepository.FindFont("Arial Nova Light");
                    title.TextState.ForegroundColor = Color.FromArgb(187, 55, 50);

                    //toc header
                    TextFragment tocHeader = new TextFragment("Table of Contents");
                    tocHeader.TextState.FontSize = 14;
                    tocHeader.TextState.FontStyle = FontStyles.Bold;
                    tocHeader.TextState.Underline = true;
                    tocHeader.TextState.Font = FontRepository.FindFont("Arial Nova Light");
                    tocHeader.TextState.ForegroundColor = Color.FromArgb(0, 97, 147);
                    tocHeader.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

                    tocPage.Paragraphs.Add(tocHeader);
                    TextFragment text = new TextFragment(" ");
                    tocPage.Paragraphs.Add(text);
                    tocPage.Paragraphs.Add(title);
                    tocPage.TocInfo = tocInfo;

                    TextFragment text2 = new TextFragment(" ");
                    tocPage.Paragraphs.Add(text2);

		

       

         foreach (PDFMapper pMap in pdfMapper)
                    {
                        // Create Heading object
                        Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
                        heading2.StartNumber = Page_Start_cnt;
                        TextSegment segment2 = new TextSegment();
                        heading2.TocPage = tocPage;
                        heading2.Segments.Add(segment2);

                        // Specify the destination page for heading object
                        heading2.DestinationPage = doc.Pages[pMap.PageOffset + 1];

                        // Destination page
                        heading2.Top = doc.Pages[pMap.PageOffset + 1].Rect.Height;

                        string input = pMap.FundName

                        // Handling <b> tag
                        string pattern = @"<b>(.*?)</b>";
                        System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(input, pattern);

                        if (matches.Count > 0)
                        {
                            int currentIndex = 0;

                            foreach (System.Text.RegularExpressions.Match match in matches)
                            {
                                int startIndex = match.Index;
                                int length = match.Length;
                                string extractedText = match.Groups[1].Value;

                                // Add plain text before the current bold tag
                                if (startIndex > currentIndex)
                                {
                                    string plainText = input.Substring(currentIndex, startIndex - currentIndex);
                                    TextSegment plainSegment = new TextSegment(plainText);
                                    plainSegment.TextState.Font = FontRepository.FindFont("Arial Nova Light");
                                    plainSegment.TextState.FontSize = 9;
                                    plainSegment.TextState.ForegroundColor = Color.FromArgb(65, 73, 86);
                                    heading2.Segments.Add(plainSegment);
                                }

                                // Add the bold segment
                                TextSegment boldSegment = new TextSegment(extractedText);
                                boldSegment.TextState.FontStyle = FontStyles.Bold;
                                heading2.Segments.Add(boldSegment);

                                // Update the current index
                                currentIndex = startIndex + length;
                            }

                            // Add plain text after the last bold tag
                            if (currentIndex < input.Length)
                            {
                                string remainingText = input.Substring(currentIndex);
                                TextSegment remainingSegment = new TextSegment(remainingText);
                                remainingSegment.TextState.Font = FontRepository.FindFont("Arial Nova Light");
                                remainingSegment.TextState.FontSize = 9;
                                remainingSegment.TextState.ForegroundColor = Color.FromArgb(65, 73, 86);
                                heading2.Segments.Add(remainingSegment);
                            }
                        }
                        else
                        {
                            // If the pattern doesn't match, handle it here (e.g., add the entire input as plain text)
                            TextSegment plainSegment = new TextSegment(input);
                            plainSegment.TextState.Font = FontRepository.FindFont("Arial Nova Light");
                            plainSegment.TextState.FontSize = 9;
                            plainSegment.TextState.ForegroundColor = Color.FromArgb(65, 73, 86);
                            heading2.Segments.Add(plainSegment);
                        }

                        // Add page number to the TOC
                        heading2.TocPage.Paragraphs.Add(heading2);

                        // Increment the page number
                        Page_Start_cnt++;
                    }
					 doc.Save(C:\PDF\Merged.pdf);

                    Aspose.Pdf.Document doc1 = new Aspose.Pdf.Document(C:\PDF\Merged.pdf);

                    PageNumberStamp pageNumberStamp = new PageNumberStamp();
                    // Whether the stamp is background
                    pageNumberStamp.Background = false;
                    int total_Page_count = Convert.ToInt32(doc1.Pages.Count);
                    pageNumberStamp.Format = "# of " + (total_Page_count);
                    pageNumberStamp.BottomMargin = 10;
                    pageNumberStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
                    pageNumberStamp.StartingNumber = 1;
                    // Set text properties
                    pageNumberStamp.TextState.Font = FontRepository.FindFont("Arial Nova Light");
                    pageNumberStamp.TextState.FontSize = 9.0F;
                    pageNumberStamp.TextState.FontStyle = FontStyles.Regular;
                    pageNumberStamp.TextState.ForegroundColor = Color.Black;

                    foreach (Aspose.Pdf.Page page in doc1.Pages)
                    {
                        pageNumberStamp.Value = $"{ Page_Start_cnt} of {total_Page_count}";
                        page.AddStamp(pageNumberStamp);
                    }
                     doc1.Save(C:\PDF\Merged.pdf);
    }
}

testfile.pdf (7.9 MB)

@ajayEY

We have tested the scenario in our environment using your code snippet and were able to replicate the issues that you mentioned.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56098

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hello Asad,

Can you please help us with the solution here please? We need to have mixed types Bold with regular text fonts in the TOC.

Regards,
Manoj

@manojk92

The earlier logged ticket has not been yet resolved sadly due to other pending issues in the queue. However, we have recorded your concerns and as soon as we have some updates about issue fix, we will update you. Please be patient and spare us some time.