System.InvalidCastException: Unable to cast object of type Concatenate

Please help!!!
Got error when try use concatenate method of Aspose.Pdf.Kit.PdfFileEditor

System.InvalidCastException: Unable to cast object of type ‘xeb116a323308e2f7.xae94bcfc1c7ddd9a’ to type ‘xeb116a323308e2f7.x9268cb62a90047a5’

                    Aspose.Pdf.Kit.PdfFileEditor MergePDF = new Aspose.Pdf.Kit.PdfFileEditor();

                    string[] FileName = new string[PDFFilelist.Count];
                    for (int loop = 0; loop < PDFFilelist.Count; loop++)
                    {
                        FileName[loop] = PDFFilelist[loop].ToString();
                    }
                    try
                    {
                        
                            MergePDF.Concatenate(FileName, DirPDFFolder.FullName + "\\ToUpload.pdf");
                        }
1 Like

@IqorRamandeep

Summary
You are receiving a System.InvalidCastException when calling PdfFileEditor.Concatenate. The exception is caused by using the deprecated Aspose.Pdf.Kit namespace (and an outdated overload) together with the current 25.10 Aspose.PDF for .NET library. The old Kit implementation attempts an internal cast that fails with the newer assembly.

Why it happens

  • Deprecated classAspose.Pdf.Kit.PdfFileEditor was removed from the public API long ago. In the 25.x releases the functional class is Aspose.Pdf.Facades.PdfFileEditor (or Aspose.Pdf.PdfFileEditor).
  • Internal type mismatch – The Kit version still exists only for backward compatibility and internally casts objects that belong to the new assembly, which triggers the InvalidCastException.
  • Input issues – If any element of the file‑name array is null, points to a non‑PDF file, or the file is locked, the editor cannot create the required PdfDocument object and may also raise the same exception.

What to do next

  1. Switch to the supported class
using Aspose.Pdf.Facades; // correct namespace

// Create the editor
PdfFileEditor editor = new PdfFileEditor();

// Build a clean list of PDF file paths
List<string> sourceFiles = new List<string>();
foreach (var item in PDFFilelist)
{
string path = item?.ToString();
if (!string.IsNullOrEmpty(path) && File.Exists(path))
sourceFiles.Add(path);
else
Console.WriteLine($"Skipped invalid entry: {path}");
}

// Concatenate the PDFs
string outputPath = Path.Combine(DirPDFFolder.FullName, "ToUpload.pdf");
editor.Concatenate(sourceFiles.ToArray(), outputPath);
  1. Validate inputs before calling
  • Ensure every entry in sourceFiles points to an existing, readable PDF file.
  • Remove any null or empty strings from the array.
  • Verify that the files are not opened elsewhere with an exclusive lock.
  1. Clean the project references
  • Keep only the NuGet package Aspose.PDF (version 25.10 is fine).
  • Delete any using Aspose.Pdf.Kit; statements and remove Kit references from the project file.
  • Re‑build the solution so that only the new assembly is loaded.
  1. Avoid the Kit class (if you still have code that references it)
  • Rather than adding binding redirects, replace the Kit usage with the Facades class as shown above. This eliminates the fragile workaround and aligns the code with the supported API.
  1. Run a minimal test
  • Create two small one‑page PDF files in a temporary folder.
  • Execute the code above against only those two files.
  • If the operation succeeds, gradually add the remaining files to identify any problematic source PDF.

If the exception continues after completing these steps, capture the full stack trace and note the exact file that triggers the error. That information will help pinpoint any remaining issues.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.