Double segment link annotation issue

Hi Aspose Team,

Using below source code we are creating hyperlink on attached PDF file (TransferDocument.pdf).

We are trying to create hyperlink on “Deeds, Leases; official Records and Contracts on page 219” text in the attached PDF file. The below code is considering from “Deeds, Leases; official Records and Contract” is one segment and remaining text “s on page 219” are another segment. Hence, this case 2 link annotation is created for single line text. Refer the Attached image file for more details.

Please let me know how to create single link annotation for entire text (“Deeds, Leases; official Records and Contracts on page 219”) using Aspose in this case.

NOTE: it’s very high priority task for me. Please let know your feedback ASAP.

foreach (Aspose.Pdf.Page pdfPage in PDFDocument.Pages)
{
//create TextAbsorber object to find all the phrases matching the regular expression
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("<>", new TextSearchOptions(true));

pdfPage.Accept(textFragmentAbsorber);

//get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

if (textFragmentCollection.Count > 0)
{
foreach (TextFragment textFragment in textFragmentCollection)
{
// Use the text segments when the text fragment has a newline char.
if (textFragment.Text.Contains(Environment.NewLine))
{
foreach (TextSegment s in textFragment.Segments)
{
s.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;

// Create the annotation for each segment.
LinkAnnotation link = new LinkAnnotation(pdfPage, rect);
Aspose.Pdf.Point start = new Aspose.Pdf.Point(rect.LLX, rect.LLY);
Aspose.Pdf.Point end = new Aspose.Pdf.Point(rect.LLX + rect.Width, rect.LLY);
LineAnnotation line = new LineAnnotation(pdfPage, rect, start, end);

line.Color = Aspose.Pdf.Color.Blue;
line.Border = new Border(link) { Width = 1 };
link.Border = new Border(link) { Width = 0 };
link.Action = new GoToURIAction(url);
pdfPage.Annotations.Add(link);
pdfPage.Annotations.Add(line);
}
}
else // Use the whole text fragment.
{
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;
LinkAnnotation link = new LinkAnnotation(pdfPage, rect);
Aspose.Pdf.Point start = new Aspose.Pdf.Point(rect.LLX, rect.LLY);
Aspose.Pdf.Point end = new Aspose.Pdf.Point(rect.LLX + rect.Width, rect.LLY);
LineAnnotation line = new LineAnnotation(pdfPage, rect, start, end);

line.Color = Aspose.Pdf.Color.Blue;
line.Border = new Border(link) { Width = 1 };
link.Border = new Border(link) { Width = 0 };
link.Action = new GoToURIAction(url);
pdfPage.Annotations.Add(link);
pdfPage.Annotations.Add(line);
}
}
}
}

Regards,

Ganesan. B

Hi Ganesan,


Thanks for contacting support.

In your earlier post, the values for rect object are not defined. So in order to test the scenario, I have used the following code snippet (which I have made slight changes to code) and I am unable to notice any problem because as per my observations, the LinkAnnotation is not being drawn. For your reference, I have attached the resultant PDF generated over my end.

[C#]

// create Document object <o:p></o:p>

Aspose.Pdf.Document PDFDocument = new Aspose.Pdf.Document(@"C:\pdftest\TransferDocument.pdf");

foreach (Aspose.Pdf.Page pdfPage in PDFDocument.Pages)

{

//create TextAbsorber object to find all the phrases matching the regular expression

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("<>", new TextSearchOptions(true));

pdfPage.Accept(textFragmentAbsorber);

//get the extracted text fragments

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

if (textFragmentCollection.Count > 0)

{

foreach (TextFragment textFragment in textFragmentCollection)

{

// Use the text segments when the text fragment has a newline char.

if (textFragment.Text.Contains(Environment.NewLine))

{

foreach (TextSegment s in textFragment.Segments)

{

s.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;

// Create the annotation for each segment.

LinkAnnotation link = new LinkAnnotation(pdfPage,s.Rectangle);

Aspose.Pdf.Point start = new Aspose.Pdf.Point(s.Rectangle.LLX, s.Rectangle.LLY);

Aspose.Pdf.Point end = new Aspose.Pdf.Point(s.Rectangle.LLX + s.Rectangle.Width, s.Rectangle.LLY);

LineAnnotation line = new LineAnnotation(pdfPage, s.Rectangle, start, end);

line.Color = Aspose.Pdf.Color.Blue;

line.Border = new Aspose.Pdf.InteractiveFeatures.Annotations.Border(link) { Width = 1 };

link.Border = new Aspose.Pdf.InteractiveFeatures.Annotations.Border(link) { Width = 0 };

link.Action = new GoToURIAction("www.aspose.com");

pdfPage.Annotations.Add(link);

pdfPage.Annotations.Add(line);

}

}

else // Use the whole text fragment.

{

textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;

LinkAnnotation link = new LinkAnnotation(pdfPage, textFragment.Rectangle);

Aspose.Pdf.Point start = new Aspose.Pdf.Point(textFragment.Rectangle.LLX, textFragment.Rectangle.LLY);

Aspose.Pdf.Point end = new Aspose.Pdf.Point(textFragment.Rectangle.LLX + textFragment.Rectangle.Width, textFragment.Rectangle.LLY);

LineAnnotation line = new LineAnnotation(pdfPage, textFragment.Rectangle, start, end);

line.Color = Aspose.Pdf.Color.Blue;

line.Border = new Aspose.Pdf.InteractiveFeatures.Annotations.Border(link) { Width = 1 };

link.Border = new Aspose.Pdf.InteractiveFeatures.Annotations.Border(link) { Width = 0 };

link.Action = new GoToURIAction("www.aspose.com");

pdfPage.Annotations.Add(link);

pdfPage.Annotations.Add(line);

}

}

}

}

PDFDocument.Save(“c:/pdftest/AnnotationAdded.pdf”);