Disable hyperlinks while converting html to PDF

Hi Team,

we would like to disable Hyperlinks (URLs) when converting HTML file to PDF.

we have used following code to do but not working, please suggest.

Expectation: hyperlinks should be captured but not to redirect to link.

public static bool DisableLinkAndConvertToPdf(string filePath, string targetPath)
{
var options = new HtmlLoadOptions();
using (var document = new Document(filePath, options))
{
for (var eachPage = 1; eachPage <= document.Pages.Count; eachPage++)
{
var page = document.Pages[eachPage];
var totalAnnotationsCount = page.Annotations.Count;
for (var annotation = 1; annotation <= totalAnnotationsCount; annotation++)
{
if (page.Annotations[annotation].AnnotationType != AnnotationType.Link) continue;
var linkAnnot = (LinkAnnotation)page.Annotations[annotation];
var uriAction = (GoToURIAction)linkAnnot.Action;
if (uriAction == null) continue;
var urlString = uriAction.URI;
linkAnnot.Hyperlink = null;
uriAction.URI.Remove(0);

                }
                document.Save();
                           var isConversionSuccessful = ConvertToArchivalPdf(document, targetPath);
            return isConversionSuccessful;

}
}
sample.zip (205 Bytes)

@ITCUser

Thank you for contacting support.

Would you please share a sample PDF file with us as expected output, so that we may investigate further to help you out.

Hi Farhan,

Please find the attachment of input and output files.

sampleIO.zip (23.9 KB)
Expectation is when click on the PDF link it should not redirect to the hyper link.

@ITCUser

We are afraid that a hyperlink may not be disabled in a PDF document. It can be removed but not disabled. In case you are able to create such a PDF file using Microsoft Word, Adobe Acrobat or any other application, then please share that PDF file with us so that we may investigate to help you out.

hi team,

can you please share the code to remove the hyperlink.

@ITCUser

You can Flatten an annotation to remove the hyperlink, as in the code snippet below:

        Document doc = new Document(dataDir + "Sample.html", new HtmlLoadOptions());
        foreach (Page page in doc.Pages)
        {
            foreach (Annotation annotation in page.Annotations)
            {
                if (annotation.AnnotationType == AnnotationType.Link)
                {
                    LinkAnnotation linkannotation = (LinkAnnotation)annotation;
                    linkannotation.Flatten();
                }
            }
        }
        doc.Save(dataDir + @"Test_18.5.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Hi Team we tried to remove the link by using above mentioned code. but we are getting an error like

Invalid index: index should be in the range [1…n] where n equals to the annotations count.

code used to remove link:

public static bool RemoveLinkAndConvertToPdf(string filePath, string targetPath)
        {
            var options = new HtmlLoadOptions();
            using (var document = new Document(filePath, options))
            {
                for (var eachPage = 1; eachPage <= document.Pages.Count; eachPage++)
                {
                    var page = document.Pages[eachPage];
                    var totalAnnotationsCount = page.Annotations.Count;
                    for (var annotation = 1; annotation <= totalAnnotationsCount; annotation++)
                    {
                        if (page.Annotations[annotation].AnnotationType != AnnotationType.Link) continue;
                        var linkAnnot = (LinkAnnotation)page.Annotations[annotation];
                        var uriAction = (GoToURIAction)linkAnnot.Action;
                        if (uriAction == null) continue;
                        var urlString = uriAction.URI;
                        if (!urlString.Contains(Constants.AttachmentUrl)) continue;
                        linkAnnot.Hyperlink = null;
                        linkAnnot.Flatten();
                        //var annot = new RedactionAnnotation(document.Pages[eachPage], linkAnnot.Rect)
                        //{
                        //    FillColor = Color.White,
                        //    Color = Color.Black
                        //};
                        //annot.Redact();
                    }
                    document.Save();
                }
                              return isConversionSuccessful;
            }

@ITCUser

The code snippet shared by me, as well as the one shared by you, are not reproducing this issue in our environment. We have made following changes to make it work in our environment:

  • if (!urlString.Contains("Constants.AttachmentUrl")) continue; 
    
  • document.Save(targetPath);
    

Please ensure using Aspose.PDF for .NET 18.5, and in case the problem persists, please share a narrowed down sample application reproducing this issue so that we may proceed further to help you out.

Above code throwing exception "Unable to cast object of type ‘Aspose.Pdf.Annotations.GoToAction’ to type ‘Aspose.Pdf.Annotations.GoToURIAction’ " at code line “var uriAction = (GoToURIAction)linkAnnot.Action;” .

We are using Aspose.PDF for .NET 18.5

Please refer the attached code snippet-image.png (38.6 KB)

Please suggest.

@ITCUser

Thank you for getting back to us.

We have tested below code snippet with sample.html file but the issue is not reproduced with Aspose.PDF for .NET 18.6.

    public static bool RemoveLinkAndConvertToPdf(string filePath, string targetPath)
    {
        var options = new HtmlLoadOptions();
        using (var document = new Document(filePath, options))
        {
            for (var eachPage = 1; eachPage <= document.Pages.Count; eachPage++)
            {
                var page = document.Pages[eachPage];
                var totalAnnotationsCount = page.Annotations.Count;
                for (var annotation = 1; annotation <= totalAnnotationsCount; annotation++)
                {
                    if (page.Annotations[annotation].AnnotationType != AnnotationType.Link) continue;
                    var linkAnnot = (LinkAnnotation)page.Annotations[annotation];
                    var uriAction = (GoToURIAction)linkAnnot.Action;
                    if (uriAction == null) continue;
                    var urlString = uriAction.URI;
                    if (!urlString.Contains("Constants.AttachmentUrl")) continue;
                    linkAnnot.Hyperlink = null;
                    linkAnnot.Flatten();
                    var annot = new RedactionAnnotation(document.Pages[eachPage], linkAnnot.Rect)
                    {
                        FillColor = Aspose.Pdf.Color.White,
                        Color = Aspose.Pdf.Color.Black
                    };
                    annot.Redact();
                }
                document.Save(targetPath);
            }
            bool isConversionSuccessful = true;
            return isConversionSuccessful;
        }
    }

Would you please create a narrowed down sample application along with all relevant files so that we may try to reproduce and investigate it in our environment.