Aspose PDF 24.8 throws ArrayIndexOutOfBoundsException

Aspose PDF 24.8 throws a ArrayIndexOutOfBoundsException when I run the following code. This code runs fine on Aspose PDF 23.7.

@Test
void addHtmlFragment() throws Exception
{
	//Using aspose classes directly
	Document document = new Document();
	Page page = document.getPages().add();
	Paragraphs paragraphs = page.getParagraphs();
	HtmlFragment htmlFragment = new HtmlFragment("test");
	paragraphs.add(htmlFragment);
	document.processParagraphs();
}

Here is a stack trace from this test code in Aspose PDF 24.8:

java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at com.aspose.pdf.l2f.lI(Unknown Source)
	at com.aspose.pdf.l2f.lI(Unknown Source)
	at com.aspose.pdf.l2f.lI(Unknown Source)
	at com.aspose.pdf.HtmlFragment.lI(Unknown Source)
	at com.aspose.pdf.FormattedFragment.lI(Unknown Source)
	at com.aspose.pdf.l5t.lI(Unknown Source)
	at com.aspose.pdf.l5t.le(Unknown Source)
	at com.aspose.pdf.Page.lf(Unknown Source)
	at com.aspose.pdf.Page.lf(Unknown Source)
	at com.aspose.pdf.Page.l0t(Unknown Source)
	at com.aspose.pdf.ADocument.processParagraphs(Unknown Source)
	at com.aspose.pdf.Document.processParagraphs(Unknown Source)
	at AsposePdfTest.addHtmlFragment(AsposePdfTest.java:790)
	...

Please reach out if you need any other info from me on this.

Thanks

@RyanWilliamsUSC

What JDK version are you using? Have you tried with 24.9 version of the API?

I have used both JDK 11 and JDK 17.

I just updated to 24.9 so I can do my test again in JDK 17. The test still fails.

Here is my stack trace on 24.9:

java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at com.aspose.pdf.l2f.lI(Unknown Source)
	at com.aspose.pdf.l2f.lI(Unknown Source)
	at com.aspose.pdf.l2f.lI(Unknown Source)
	at com.aspose.pdf.HtmlFragment.lI(Unknown Source)
	at com.aspose.pdf.FormattedFragment.lI(Unknown Source)
	at com.aspose.pdf.l5t.lI(Unknown Source)
	at com.aspose.pdf.l5t.le(Unknown Source)
	at com.aspose.pdf.Page.lf(Unknown Source)
	at com.aspose.pdf.Page.lf(Unknown Source)
	at com.aspose.pdf.Page.l0t(Unknown Source)
	at com.aspose.pdf.ADocument.processParagraphs(Unknown Source)
	at com.aspose.pdf.Document.processParagraphs(Unknown Source)
	at AsposePdfTest.addHtmlFragment(AsposePdfTest.java:790)

@RyanWilliamsUSC

It looks strange that we are not able to replicate this issue in our environment. If possible, could you please try JDK1.8 as well? Please let us know what results you get with it.

I just ran the test again on 24.9 in a new project without our static setup code (so no license file or fonts) and it looks to work in JDK 17.

I then added the part of our static setup code to isolate what is causing the failure. There looks to be a bug in how fonts are handled for paragraphs.add(htmlFragment).

Here is the setup code for this test:

@BeforeAll
static void beforeAll() throws Exception {
	License asposeLicense = new com.aspose.pdf.License();
	asposeLicense.setLicense(new ClassPathResource("config/aspose/Aspose.Total.Java.lic").getInputStream());

	//Setup Fonts

	ResourceLoader resourceLoader = new DefaultResourceLoader();

	// Get the testing font paths
	final List<String> fontPaths = new ArrayList<>();
	fontPaths.add(new ClassPathResource("config/fonts").getPath());
		
	// Remove the System font folders.
	FontRepository.getLocalFontPaths().clear();
	FontRepository.getSources().clear(); // This causes the problem

	// process the provided font paths.
	fontPaths.stream().map(s -> {
		log.info("Attempting to load font resource into aspose.pdfs: {}", s);
		Resource resource = resourceLoader.getResource(s);
		String resourcePath = null;
		try {
			resourcePath = resource.getURI().getPath();
		}
		catch (IOException e) {
			log.error("Unable to load font resource: {}", s);
			return null;
		}
		log.info("{} resource path found at: {}", s, resourcePath);
		return resourcePath;
	}).filter(Objects::nonNull).forEach(path -> FontRepository.getSources().add(new FolderFontSource(path)));
}

We clear the fonts during setup and use fonts from a resource folder config/fonts because our deployment environments have different sets of system fonts available from each other and our dev workstations (We use Ubuntu and RHEL for our deployments and Windows for development). This is done to make it so everything looks consistent regardless where the code is ran.

We do change other font settings (Like overrides and fallbacks) but this is a stripped down version for this test.

It looks like our call to FontRepository.getSources().clear() is what causes paragraphs.add(htmlFragment) to throw a exception.

This code worked as of 23.7 and is broken as of 24.8.

I did a bisect and it looks like a change that was made between 23.8 and 23.9 is what most likely broke this.

@RyanWilliamsUSC

We are checking it and will get back to you shortly.

@RyanWilliamsUSC

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-44397

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.