Add PDF link color or border color in scanned PDF documents using Aspose.PDF for .NET

Our objective is to apply color for links in PDF document. For scanned documents, which are converted to searchable PDF documents, text will be invisible. So, for links in these documents, we apply visible border instead of color for link text. For links in other documents we apply text color. We have written following code to achieve the same. This code worked well for most of documents except for few documents like attached. For links in this document, neither border color is applied nor text color is visible.
Please explain reason for this behavior. Is there a problem in document or written code?

   public static void LinkBorderColorTextColor(String sourcePath, String OutputPath)
    {
        Document document = new Document(sourcePath);
        foreach (Aspose.Pdf.Page page in document.Pages)
        {
            AnnotationSelector selector = new AnnotationSelector(new Aspose.Pdf.Annotations.LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));
            page.Accept(selector);
            IList<Annotation> list = selector.Selected;
            foreach (LinkAnnotation a in list)
            {
                string URL2 = string.Empty;
                IAppointment dest = a.Destination;
                if (a.Action != null || dest != null)
                {
                    if (a.Action.GetType().FullName == "Aspose.Pdf.Annotations.GoToAction")
                    {
                        URL2 = ((Aspose.Pdf.Annotations.GoToAction)a.Action).ToString();
                        if (URL2 != "")
                        {
                            TextFragmentAbsorber ta = new TextFragmentAbsorber();
                            ta.TextSearchOptions.Rectangle = a.Rect;
                            ta.Visit(page);
                            bool bordercolor = false;
                            foreach (TextFragment tf in ta.TextFragments)
                            {
                                if (tf.TextState.Invisible)
                                {
                                    bordercolor = true;
                                    Border b = new Border(a);
                                    b.Width = 2;
                                    b.Style = BorderStyle.Solid;
                                    a.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#0000FF"));
                                    break;
                                }
                            }
                            if (!bordercolor)
                            {
                                foreach (TextFragment tf in ta.TextFragments)
                                {
                                    if (tf.Text.Trim() != "")
                                    {
                                        tf.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#0000FF"));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            page.FreeMemory();
        }
        document.Save(OutputPath);
     }

linkcolor.zip (561 KB)

@crshekharam

Could you please share the document with which the above code snippet is working and generating expected results. We will further test the scenario in our environment and address it accordingly.

@asad.ali

I have modified code a little. Please take updated code. I am attaching two set of files link border.zip (536.1 KB) and link color.zip (49.0 KB).

There is no problem in link color documents as it normal file. Link color is changed successfully for this document.

Document in link border file is scanned document and is converted to searchable PDF document. In this document out of 8 links only 2 links are getting border color. For remaining links, neither link text color is applied nor border color is applied.

    public static void LinkBorderColorTextColor(String sourcePath, String OutputPath)
    {
        Document document = new Document(sourcePath);
        foreach (Aspose.Pdf.Page page in document.Pages)
        {
            AnnotationSelector selector = new AnnotationSelector(new Aspose.Pdf.Annotations.LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));
            page.Accept(selector);
            IList<Annotation> list = selector.Selected;
            int i = 0;
            foreach (LinkAnnotation a in list)
            {
                i++;
                string URL2 = string.Empty;
                IAppointment dest = a.Destination;
                if (a.Action != null || dest != null)
                {
                    if (a.Action.GetType().FullName == "Aspose.Pdf.Annotations.GoToAction")
                    {
                        URL2 = ((Aspose.Pdf.Annotations.GoToAction)a.Action).ToString();
                        if (URL2 != "")
                        {
                            TextFragmentAbsorber ta = new TextFragmentAbsorber();
                            ta.TextSearchOptions.Rectangle = a.Rect;
                            ta.Visit(page);
                            bool bordercolor = false;
                            if (ta.TextFragments.Count == 0)
                                bordercolor = true;
                            else
                            {
                                foreach (TextFragment tf in ta.TextFragments)
                                {
                                    if (tf.TextState.Invisible)
                                    {
                                        bordercolor = true;
                                        break;
                                    }
                                }
                            }
                            if (bordercolor)
                            {
                                Border b = new Border(a);
                                b.Width = 2;
                                b.Style = BorderStyle.Solid;
                                a.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#0000FF"));
                           }
                            else
                            {
                                foreach (TextFragment tf in ta.TextFragments)
                                {
                                    if (tf.Text.Trim() != "")
                                    {
                                        tf.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#0000FF"));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            page.FreeMemory();
        }
        document.Save(OutputPath);
     }

@crshekharam

We were able to notice the issue in our environment while testing the scenario with Aspose.PDF for .NET 21.2. Therefore, an issue under the ticket ID PDFNET-49477 has been logged in our issue management system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

@asad.ali, Is there any update on this issue?

@crshekharam

We are afraid that the earlier logged issue is not yet resolved. We will surely investigate and fix it on first come first serve basis and let you know as soon as we make significant progress towards its resolution. Please give us some time.

We are sorry for the inconvenience.