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?