Replace Text in PDF not working correctly

Hey,

We are using the Aspose.PDF product library to find and replace hyperlinks in PDF’s. When all of the hyperlink labels (TextFragments) are the same string, then the output isn’t correct. Only the first label is changed and the rest look to be some kind of unrecognised characters. I am unsure as to what the issue is as I can’t see any errors in our code, as I’ve done exactly as the Documentation suggests.

We are using Aspose.PDF 22.10.0. I have attached the input and output PDF’s, and I have copied in our code below.

Here we loop through the pages to find the LinkAnnotations, and then get the TextFragment of the Hyperlink by using the rectangle of the LinkAnnotation

foreach (Aspose.Pdf.Page page in doc.Pages)
{
    // Get all Link Annotations and associated Text Fragments from PDF and add to a list
    foreach (Aspose.Pdf.Annotations.Annotation annot in page.Annotations)
    {
        if (annot is LinkAnnotation)
        {
            // Get Text fragments from inside URLs
            TextFragmentAbsorber absorber = new TextFragmentAbsorber();
            absorber.TextSearchOptions.LimitToPageBounds = true;
            absorber.TextSearchOptions.Rectangle = annot.Rect;
            page.Accept(absorber);
            var nonEmptyList = absorber.TextFragments.Where(w => !string.IsNullOrEmpty(w.Text)).ToList();
            nonEmptyList = absorber.TextFragments.Where(w => !string.IsNullOrWhiteSpace(w.Text)).ToList();
            documentHyperlinks.Add(new PdfHyperlink(){ Label = nonEmptyList.FirstOrDefault(), Url = (LinkAnnotation) annot });
        }
    }
}

Once we have our filtered list of Hyperlinks, we update the Label based on the users input and update the size of the URL rectangle (to account for changes in text length).

if (fullReplacement)
{
    hyperlink.Label.Text = searchHyperlink.HyperlinkLabelValueNew;
    // Set Rectangle bounds to accomodate new Text Annotation size
    hyperlink.Url.Rect = hyperlink.Label.Rectangle;
}
else
{
    hyperlink.Label.Text = hyperlink.Label.Text.Replace(searchHyperlink.HyperlinkLabelValueCurrent, searchHyperlink.HyperlinkLabelValueNew);
    // Set Rectangle bounds to accomodate new Text Annotation size
    hyperlink.Url.Rect = hyperlink.Label.Rectangle;
}

pdf-input.pdf (36.2 KB)
pdf-output.pdf (299.8 KB)

Thanks,
Charlie

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-53528

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.

Hey Andrey,

Are there any updates on this issue?

We are afraid that the issue has not been yet resolved. It was logged under free support model and will be resolved on a first come first serve basis. Once we make some progress towards its resolution, we will definitely update you within this forum thread. Please spare us some time.

Hey Guys,

Is there any update on this issue yet?

@charlie.lancaster

Sadly, no news yet because of the pending issues logged prior to this ticket. Nevertheless, your concerns were already recorded and we will inform you as soon as we have some definite updates about ticket resolution. We highly appreciate your patience in this regard.