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

Free Support Forum - aspose.com

Exception when merging pdfs

Hello,

While trying to merge pdfs using following snippet using aspose-pdf v19.8:

public static byte[] mergePDF(List<String> pdfPhysicalPathList) {

	Document mergedPdfDoc = null;
	List<Document> mergedPdfList = new ArrayList<>();
	try {
		for (String pdfPhysicalPath : pdfPhysicalPathList) {
			if (null == mergedPdfDoc) {
				mergedPdfDoc = new Document(pdfPhysicalPath);
				mergedPdfList.add(mergedPdfDoc);
			} else {
				Document nextPagesToMerge = new Document(pdfPhysicalPath);
				mergedPdfDoc.getPages().add(nextPagesToMerge.getPages());
				mergedPdfList.add(nextPagesToMerge);
			}
		}

		byte[] mergedDocBytes = null;
		if (null != mergedPdfDoc) {
			try (ByteArrayOutputStream mergedPdfOutStream = new ByteArrayOutputStream()) {
				mergedPdfDoc.save(mergedPdfOutStream);
				mergedDocBytes = mergedPdfOutStream.toByteArray();
			} catch (Exception e) {
				LOGGER.error(COMMON_ERROR_MESSAGE, e);
			}
		}
		return mergedDocBytes;
	} finally {
		for (Document mergedPdf : mergedPdfList) {
			closePdf(mergedPdf);
		}

	}
}

With some pdfs we get following exception:

com.aspose.pdf.internal.ms.System.l5if: List contains object of invalid type
at com.aspose.pdf.PageCollection.lI(Unknown Source)
at com.aspose.pdf.PageCollection.add(Unknown Source)
at com.XX.XXX.XXXX.util.PdfUtil.mergePDF(PdfUtil.java:215)

Could you guys please suggest?

Thanks
Srikanta

@edleads,

Thanks for contacting support.

Can you please try to use Aspose.PDF latest version 20.4 and if there is still an issue than please share source file so that we may investigate this issue on our end to help you out.