Unable to cast object of typeeeeeee

‘Unable to cast object of type ‘Aspose.Pdf.Annotations.GoToURIAction’ to type ‘Aspose.Pdf.Annotations.GoToAction’.’ iam facing when annotating

‘Unable to cast object of type ‘Aspose.Pdf.Annotations.GoToURIAction’ to type ‘Aspose.Pdf.Annotations.GoToAction’.’ while annotation link these error showing iam using latest aspose pdf

@shyamlal

The error message you are encountering, “Unable to cast object of type ‘Aspose.Pdf.Annotations.GoToURIAction’ to type ‘Aspose.Pdf.Annotations.GoToAction’,” indicates that there is an issue with type casting in your code. This typically occurs when you are trying to treat an object of one type as if it were another incompatible type.

In the context of Aspose.PDF, GoToURIAction and GoToAction are different classes that represent different types of actions. GoToURIAction is used for actions that navigate to a URI, while GoToAction is used for navigating to a specific location within the document.

To resolve this issue, you should ensure that you are using the correct type for the action you are trying to perform. Here’s a general approach to handle this:

  1. Check the Type of the Action: Before casting, check the type of the action you are working with. You can use the is keyword in C# to verify the type.

  2. Use the Correct Type: If you are working with a GoToURIAction, you should not attempt to cast it to GoToAction. Instead, handle it as a GoToURIAction.

Here is a sample code snippet that demonstrates how to check the type before casting:

using Aspose.Pdf;
using Aspose.Pdf.Annotations;

// Load your PDF document
Document pdfDocument = new Document("your-document.pdf");

// Iterate through the annotations
foreach (var annotation in pdfDocument.Pages[1].Annotations)
{
    if (annotation is GoToURIAction uriAction)
    {
        // Handle GoToURIAction
        Console.WriteLine("This is a GoToURIAction with URI: " + uriAction.Uri);
    }
    else if (annotation is GoToAction goToAction)
    {
        // Handle GoToAction
        Console.WriteLine("This is a GoToAction.");
    }
    else
    {
        Console.WriteLine("Unknown annotation type.");
    }
}

This code checks the type of each annotation and processes it accordingly, preventing the casting error you encountered.

If you continue to experience issues, please provide more details about your implementation, and I would be happy to assist further.

Sources:

private static void UpdateLinkAnnotation()
{
// The path to the documents directory
var dataDir = GetDataDir();

// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "sample (1)_merged (2).pdf"))
{
    // Get the first link annotation from first page of document
    var linkAnnot = (Aspose.Pdf.Annotations.LinkAnnotation)document.Pages[1].Annotations[1];

 
    var goToAction = (Aspose.Pdf.Annotations.GoToAction)linkAnnot.Action;

    // Specify the destination for link object
    // The first parameter is document object, second is destination page number.
    // The 5ht argument is zoom factor when displaying the respective page. When using 2, the page will be displayed in 200% zoom
    goToAction.Destination = new Aspose.Pdf.Annotations.XYZExplicitDestination(1, 1, 2, 2);

    // Save PDF document
    document.Save(dataDir + "UpdateLinks_out.pdf");
}

} my code

@shyamlal

Are you using the latest version of the API? Also, is this happening against some specific PDF document? Can you please share your sample file with us so that we can test the scenario in our environment and address it accordingly?