We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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.