Replacing text in link annotations in a table in an existing pdf is cropping the underline after update and leaving unmatched background color

I have replaced the link annotations present in a table in an existing pdf using Aspose.pdf. However, after the update, sometimes the links are missing the underline in alternate rows. The background color seems to be cropped as well. Please help with this issue.
image.png (45.1 KB)

@rdahal

Can you please share your sample PDF document along with complete sample code snippet so that we can test the scenario in our environment and address it accordingly.

Hi Asad,

I have attached my sample pdf and the sample code with this response. My pdf has bookmarks for several locations and a table with bogus links to them in the beginning.
I read all the bookmarks, save their destinations, and update all the bogus links in the tables with the destination from bookmarks whose ids match with the id of the links.
So, most of the times those links text get updated fine but sometimes we see the issue where the background is unmatched and the underlines are missing in some rows(see the pdf I attached with the original post for the issue).
Not sure what is causing that. Any help would be appreciated.

static void UpdatePageLinks(Document pdfDocument)
{

Regex tgtRgx = new Regex("([a-z0-9]*)_(.*)");
Dictionary<string, XYZExplicitDestination> extractedbookmarks = new Dictionary<string, XYZExplicitDestination>();

// Create PdfBookmarkEditor
PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor();

// Open PDF file
bookmarkEditor.BindPdf(pdfDocument);

// Extract bookmarks
Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks();

foreach (Aspose.Pdf.Facades.Bookmark bookmark in bookmarks)
{
var match = tgtRgx.Match(bookmark.Title);
var id = match.Groups[1].Value;
var dest = new XYZExplicitDestination(bookmark.PageNumber, bookmark.PageDisplay_Left, bookmark.PageDisplay_Top, 0);
extractedbookmarks.Add(id, dest);
}

Regex srcRgx = new Regex("[https://www](https://www)." + "([a-z0-9]*)");
Dictionary<string, Annotation> extractedStripIndexLinks = new Dictionary<string, Annotation>();

try
{
// Extract annotations
foreach (var page in pdfDocument.Pages)
{
var linkAnnotations = page.Annotations.Where(a => a.AnnotationType == AnnotationType.Link);

if (linkAnnotations != null)
{
foreach (LinkAnnotation annot in linkAnnotations)
{
// Print the URL of each Link Annotation
var linkUri = ((annot as LinkAnnotation).Action as GoToURIAction).URI;

var match = srcRgx.Match(linkUri);
var id = match.Groups[1].Value;
extractedStripIndexLinks.Add(id, annot);
}
}
}
}
catch (Exception ex)
{
throw;
}

foreach (var link in extractedStripIndexLinks)
{

var linkAnnot = extractedStripIndexLinks[link.Key];
if (linkAnnot is LinkAnnotation && extractedbookmarks.ContainsKey(link.Key))
{

var page = pdfDocument.Pages[linkAnnot.PageIndex];
var rect = linkAnnot.Rect;
rect.LLX -= 10;

rect.LLY -= 10;

rect.URX += 10;

rect.URY += 10;
// Grab the text as fragments from the link location from the doc
TextFragmentAbsorber ta = new TextFragmentAbsorber();
ta.TextSearchOptions.Rectangle = rect;
page.Accept(ta);

foreach (TextFragment textFragment in ta.TextFragments)
{

textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;
var UriToSearch = textFragment.Text + "/"; //the URI returned by linkAnnot has / at the end
if (UriToSearch.Equals(((linkAnnot as LinkAnnotation).Action as GoToURIAction).URI))
{
var dest = extractedbookmarks[link.Key];
textFragment.Text = "page " + dest.PageNumber;
textFragment.TextState.Font = FontRepository.FindFont("Arial Narrow");
textFragment.TextState.FontSize = 7;
textFragment.HorizontalAlignment = HorizontalAlignment.Center;
textFragment.VerticalAlignment = VerticalAlignment.Center;
textFragment.TextState.Underline = true;

var linkAnnotation = new LinkAnnotation(page, rect)
{
Action = new GoToAction(dest) //Update the destination
};
page.Annotations.Delete(linkAnnot); //Delete the existing link
page.Annotations.Add(linkAnnotation); //Add a new one
}

}

}
}
pdfDocument.ProcessParagraphs();
Stream newStream = new MemoryStream();
pdfDocument.Save(newStream, SaveFormat.Pdf);

}

stripped_hello.pdf (2.46 MB)

@rdahal

Have you tried the same code snippet in your environment with the same PDF document that you have shared? Could you please share the .NET Framework version you are using? We tried to use this code at our side and faced an exception like in the image:

DictionaryError.png (14.2 KB)

@asad.ali
Thanks for the response.
Weird, I wasn’t getting an exception in my environment. But, I am attaching an updated pdf, please try with this one. And, I am using .NET Framework 4.7.2
hello-updated-test.pdf (2.5 MB)

@rdahal

We again faced the similar exception. It was because of the regular expression. We changed it to Regex srcRgx = new Regex("[https://www]*.([a-z0-9]*)"); and it worked correctly.

Please check the output PDF generated at our end while using Aspose.PDF for .NET 22.1. The links are fine in it except the alignment of the text. Please let us know about your feedback after testing the case with 22.1 version. We will further proceed accordingly. stripped_out.pdf (2.5 MB)

Hi Asad,
Oh that is my mistake. I was using a variable there and didn’t check after I modified the code in this thread before posting.
Glad you figured that out.
Also, the problem doesn’t happen often. The links always work. The issue is sometimes they appear to not have the underlines and the background looks cropped. I am attaching the screenshot of the image here again for your reference. It might be something about how the links are placed in the rectangle that holds the links in the table. I am not able to exactly pinpoint the problem. Please let me know if you have any further questions.

@rdahal

It is difficult to determine the issue cause without replicating it in our environment. However, can you please confirm if you have tested using the latest version i.e. Aspose.PDF for .NET 22.1?

@asad.ali
I am using Aspose.PDF.21.12.0 version of Aspose.PDF

@rdahal

Please try to use 22.2 version as well which is the latest one and has just been released. Please let us know in case you face issues with this version as well.

@asad.ali
In one of the earlier posts, you mentioned that the alignment of the text is not right. I have noticed that too. I am using textFragment.HorizontalAlignment = HorizontalAlignment.Center; textFragment.VerticalAlignment = VerticalAlignment.Center; to fix the alignment of the text within the link annotation rectangle for the new links. But, why is this not doing anything? No matter what I set the values of these properties, the text is always in the same position within the rectangle. My issue might be related to this. Can you please help me with this problem?

@rdahal

The text fragment is not being added inside the rectangle of LinkAnnotation. In fact, you are drawing LinkAnnotation around a text. They both are different objects which is why TextFragment is not honoring the alignment settings. A better way to achieve clean results is to draw a new LinkAnnotation with the same rectangle on which new text is being added like below:

if (UriToSearch.Equals(((linkAnnot as LinkAnnotation).Action as GoToURIAction).URI))
{
 var dest = extractedbookmarks[link.Key];
 textFragment.Text = "page " + dest.PageNumber;
 textFragment.TextState.Font = FontRepository.FindFont("Arial Narrow");
 textFragment.TextState.FontSize = 7;
 textFragment.TextState.Underline = true;
 var linkAnnotation = new LinkAnnotation(page, textFragment.Rectangle)
 {
  Action = new GoToAction(dest), //Update the destination
 };

 linkAnnotation.Border = new Border(linkAnnotation) { Width = 0 };
 page.Annotations.Delete(linkAnnot); //Delete the existing link
 page.Annotations.Add(linkAnnotation); //Add a new one
}

stripped_out.pdf (2.5 MB)

@asad.ali
I have replaced my code with your snippet from above. I am still struggling with matching the background color exactly with the original background color.
My code now looks like this. What am I missing? Please help.

                        {
                            var UriToSearch = textFragment.Text + "/"; //the URI returned by linkAnnot has / at the end
                            if (UriToSearch.Equals(((linkAnnot as LinkAnnotation).Action as GoToURIAction).URI))
                            {
                                var dest = extractedbookmarks[link.Key];
                                var textrect = textFragment.Rectangle;
                                textFragment.Text = "page " + dest.PageNumber;
                                textFragment.TextState.Font = FontRepository.FindFont("Arial Narrow");
                                textFragment.TextState.FontSize = 8;
                                textFragment.HorizontalAlignment = HorizontalAlignment.Center;
                                textFragment.VerticalAlignment = VerticalAlignment.Center;
                                textFragment.TextState.Underline = true;
                                textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;

                                var linkAnnotation = new LinkAnnotation(page, textrect)
                                {
                                    Action = new GoToAction(dest) //Update the destination
                                };

                                linkAnnotation.Border = new Border(linkAnnotation);
                                linkAnnotation.Border.Width = 0; //remove the border around link annotation
                                page.Annotations.Delete(linkAnnot);  //Delete the existing link
                                page.Annotations.Add(linkAnnotation); //Add a new one
                            }

                        }

Please use thesebefore_updating_the_links.pdf (1.8 MB)
after_updating_the_links.pdf (1.8 MB)
new pdfs for your testing now as my original pdf has also changed slightly. I am also attaching the updated pdf from my testing.

@rdahal

We tested the scenario with 22.2 version now (as it is the latest one) and noticed that some replaced text in the output PDF was invisible. Also, the background color was also disturbed as you mentioned. stripped_out.pdf (1.8 MB)

The issue of invisible text was not happening with 22.1 so we will consider it as regression and investigate it along with the problem of background color. A ticket with the ID PDFNET-51412 has been logged in our issue management system for the purpose. We will 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 , just so we are clear, I am not at the latest version of Aspose.PDF yet. I am now on version 22.1.0.0. The screenshots of the mismatched background that I shared earlier is from this version only. As of right now, we don’t have the license to upgrade to the latest version.
Are you saying this issue is also present in 22.1 version of Aspose.PDF?

@rdahal

With 22.1 version, only the issue of mismatched background is occurring which will also be addressed during ticket investigation. The text got vanished/lost while using 22.2 version which is not happening in the version that you are using i.e. 22.1.

@asad.ali I wanted to let you know that I have found a workaround this issue. If my original link test is small enough to be close to the same length and height of the replacement text than the background cropping doesn’t happen. You can close this issue if you like. I will reopen if I run into other issues in future.

@rdahal

Thanks for sharing your feedback. It is good to know that the issue has been resolved at your end. We will consider the provided information while investigating the ticket and will do what is needed. Please feel free to share in case you encounter any other issues.