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)