We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert PDF to PDF/A in Java using Aspose.PDF - StackOverflowError is thrown

Hello!

I use Aspose.PDF for Java 20.5 and when the attached pdf-file is resized and converted to PDF/A, a StackOverflowError is thrown from the save() method.

Surprisingly the error only occurs when I set the defaultFontName in the PdfSaveOptions, what is needed for many documents. The error also not happens when the document is not converted to PDF/A or not resized.

stackoverflow.pdf (12.2 KB)

Code:

public convert(Document document) {
	// resize doc
	for (Page page : document.getPages()) {
		int targetWidth = (int) PageSize.getA4().getWidth();
		int targetHeight = (int) PageSize.getA4().getHeight();
		double originalWidth = page.getPageRect(true).getWidth();
		double originalHeight = page.getPageRect(true).getHeight();
		long originalWidthLong = Math.round(originalWidth);
		long originalHeightLong = Math.round(originalHeight);

		boolean landscape = originalWidthLong > originalHeightLong;
		if (landscape) {
			int tmp = targetWidth;
			targetWidth = targetHeight;
			targetHeight = tmp;
		}
		double scale = Math.min(Math.min(targetWidth / originalWidth, targetHeight / originalHeight), 1.0d);
		if (scale < 1) {
			log.info("doc has to ba resized");
			double destWidth = originalWidth * scale;
			double destHeight = originalHeight * scale;

			resize(document, page.getNumber(), targetWidth, targetHeight, destWidth, destHeight);
			log.info("resized");
		}
	}
	// convert to PDF/A
	PdfFormatConversionOptions options = new PdfFormatConversionOptions(PdfFormat.PDF_A_1B, ConvertErrorAction.Delete);
	document.convert(options
	
	PdfSaveOptions saveOptions = new PdfSaveOptions();
	saveOptions.setDefaultFontName("Arial"); // no StackOverflowError, if this line is commented out
	
	ByteArrayOutputStream stream = new ByteArrayOutputStream();
	document.save(stream, saveOptions); // leads to StackOverflowError
	return stream.toByteArray();
}
	
	

private void resize(Document document, int pageNumber, double targetWidth, double targetHeight, double width, double height) {
	double marginWidth = (targetWidth - width) / 2;
	double marginHeight = (targetHeight - height) / 2;

	ContentsResizeParameters par = ContentsResizeParameters.pageResize(width, height);
	par.setTopMargin(ContentsResizeValue.units(marginHeight));
	par.setBottomMargin(ContentsResizeValue.units(marginHeight));
	par.setLeftMargin(ContentsResizeValue.units(marginWidth));
	par.setRightMargin(ContentsResizeValue.units(marginWidth));

	new PdfFileEditor().resizeContents(document, new int[] { pageNumber }, par);
}

Do you have any hints about these problem?

@dvtdaten

We tested the scenario using Aspose.PDF for Java 20.8 and were unable to notice any issue. For your kind reference, an output PDF is also attached which was generated your code snippet in our environment.

Would you please try using latest version of the API and in case issue still persists, please feel free to let us know.

test_20.8.pdf (43.8 KB)

Hello!
Now I tried the posted code with Aspose.PDF for Java 20.8 and the StackOverflowError still occurs at the save() method. Do you have an idea or workaroud for this problem?

Kind regards!

@dvtdaten

Have you tried with same PDF file that you had shared with us? Would you kindly share your environment details i.e. OS Name and Version, JDK Version, Application Type, Java Heap Size, etc. with us. Also, please share the screenshot of the error that you are noticing. We will further proceed to assist you accordingly.

Yes, I tested with the same PDF file that I shared withh you.
OS is Microsoft Windows 10 Enterprise, Java Runtime is 1.8.0_221.
Heap: -Xmx6000m

Der Error also occurs on OS Windows Server 2019 Standard
with OpenJDK Runtime Environment (11.0.8+10) and similar heap size.

As stated bevore, the error only ocurs when den PDF is resized and convertet to PDF/A and when the default font name is set.

@dvtdaten

We again tested the scenario in our environment which was same as above and were unable to notice the issue.

We used the code snippet which you have shared previously. This time we tested it with Aspose.PDF for Java 20.9. Would you please try to test it with the latest version of the API and in case you still face any issue, please share a sample console application with us which is able to reproduce the error. We will again try to replicate it at our side and share our feedback with you.

test_20.9.pdf (43.8 KB)

Hello!

After a while I took another look on this error. I want to inform you, that the problem occurred on our side up to version 21.6, but has vanished with Aspose.PDF 21.7.

Kind regards!

@dvtdaten

It is nice to hear from you that your issue has been resolved. Please keep using our API and feel free to create a new topic in case you need further assistance.