I am having an issue reading the javascript from a PDF created by our CAD system. I can view and update the javascript manually in Adobe Pro but when I attempt to use Aspose I can read the keys of the javascripts but not the contents themselves. When I attempt to retrieve the contents I get a null reference exception.
System.NullReferenceException: Object reference not set to an instance of an object.
at Aspose.Pdf.JavaScriptCollection.get_Item(String key)
I test the same code with a basic PDF I create in adobe and I am able to read the contents no problem.
Below is a sample program I made to read and log the contents of both PDFs. I attached a zip with all the data required to recreate the error. In the zip is a sample PDF from the CAD system (TestModel.pdf), the basic PDF I created in adobe(TestPdf.pdf), and the log from running the program. To test send the path of this folder as the only arg to the program. It will also need a license file, I did not include ours.
This has all been tested with the latest Nuget package.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Aspose.Pdf;
namespace JavascriptTest
{
class Program
{
static List<string> Log = new List<string>();
static void Main(string[] args)
{
var dataDir = args[0];
try
{
var licensepath = $"{dataDir}\\Aspose.PDF.NET.lic";
License license = new License();
using (FileStream fs = new FileStream(licensepath, FileMode.Open))
{
license.SetLicense(fs);
}
Log.Add("License set");
ListJavascripts($"{dataDir}\\TestPdf.pdf");
ListJavascripts($"{dataDir}\\TestModel.pdf");
}
catch (Exception ex)
{
Log.Add(ex.ToString());
}
finally
{
File.WriteAllLines($"{dataDir}\\log.txt", Log);
}
}
static void ListJavascripts(string path)
{
var doc = new Document(path);
Log.Add("Listing document javascripts");
IList keys = (IList)doc.JavaScript.Keys;
Log.Add($"{keys.Count} Javascripts found.");
foreach (string key in keys)
{
try
{
Log.Add($"{key}\n{doc.JavaScript[key]}\n");
}
catch (Exception ex)
{
Log.Add($"Error retrieving contents of {key}.\n{ex}");
}
}
doc.Dispose();
}
}
}
Data.7z (512.6 KB)