Merge multiple PDF documents into a single PDF in Java using Aspose.PDF Cloud REST API NullReferenceException error

We are seeing the following error when merging PDFs
{
“ClassName”: “System.NullReferenceException”,
“Message”: “Object reference not set to an instance of an object.”,
“Data”: null,
“InnerException”: null,
“HelpURL”: null,
“StackTraceString”: " at Aspose.Pdf.XImage.#=zH77BQ9J3uiGd()\r\n at Aspose.Pdf.XImage.get_Grayscaled()\r\n at Aspose.Pdf.Cloud.Business.ImagePresentation…ctor(XImage image, PresentationUriBuilder uriBuilder, ImageCollection imageCollection, Rectangle _rectangle, Int32 pageNumber) in C:\ms\src\Aspose.PDF.Cloud\Business\Images\ImagePresentation.cs:line 26\r\n at Aspose.Pdf.Cloud.Business.ImageCollection…ctor(PresentationUriBuilder uriBuilder, ImagePlacementCollection imagePlacements, Int32 pageNumber) in C:\ms\src\Aspose.PDF.Cloud\Business\Images\ImageCollection.cs:line 24\r\n at Aspose.Pdf.Cloud.Business.PdfPagePresentation.get_Images() in C:\ms\src\Aspose.PDF.Cloud\Business\PdfPagePresentation.cs:line 172\r\n at Aspose.Pdf.Cloud.Business.PresentationExtension.ToDTO(PageCollection pages) in C:\ms\src\Aspose.PDF.Cloud\Business\PresentationExtension.cs:line 53\r\n at Aspose.Pdf.Cloud.Business.PresentationExtension.ToDTO(IPdfDocumentPresentation presentation) in C:\ms\src\Aspose.PDF.Cloud\Business\PresentationExtension.cs:line 35\r\n at Aspose.Pdf.Cloud.Business.ResponseExtension.ToResponse(IPdfDocumentPresentation presentation) in C:\ms\src\Aspose.PDF.Cloud\Business\ResponseExtension.cs:line 23\r\n at Aspose.Pdf.Cloud.Controllers.MergeController.PutMergeDocuments(String name, MergeDocuments mergeDocuments, String storage, String folder) in C:\ms\src\Aspose.PDF.Cloud\Controllers\MergeController.cs:line 54",
“RemoteStackTraceString”: null,
“RemoteStackIndex”: 0,
“ExceptionMethod”: “8\n#=zH77BQ9J3uiGd\nAspose.PDF, Version=21.1.0.0, Culture=neutral, PublicKeyToken=\nAspose.Pdf.XImage\nSystem.Drawing.Bitmap #=zH77BQ9J3uiGd()”,
“HResult”: -2147467261,
“Source”: “Aspose.PDF”,
“WatsonBuckets”: null
}

sample payload:
{
“List”: [
“06817000000yTm1AAE.pdf”
,“06817000000yTm6AAE.pdf”
,“06817000000yTlwAAE.pdf”
]
}

Note: We DO NOT see the error with this payload (which simply removes the final entry)
{
“List”: [
“06817000000yTm1AAE.pdf”
,“06817000000yTm6AAE.pdf”
]
}

@jnugent

We have tested the PutMergeDocuments method with the latest Aspose.PDF Cloud SDK for .NET and unable to reproduce the issue. Please share your input documents and code here. We will further investigate the issue and will share our findings with you.

var names = new[] { "4pages.pdf", "barcode-examples.pdf", "02_pages.pdf" };
string localFolder = "C:/Temp/";
string remoteFolder = "Temp";
string resultName = "MergingResult.pdf";

var mergeDocuments = new MergeDocuments(new List<string>());
foreach (var name in names)
{
    using (var file = System.IO.File.OpenRead(localFolder + name))
    {
        pdfApi.UploadFile(remoteFolder + "/" + name, file);
    }
    mergeDocuments.List.Add(remoteFolder + "/" + name);
}

var response = pdfApi.PutMergeDocuments(resultName, mergeDocuments, folder: remoteFolder);

Please test using the Cloud Java SDK.

https://api.aspose.cloud/v3.0/pdf/MergedFile_PREVIEW.pdf/merge?

The merge request goes through but errors out when responding.

@jnugent

Thanks for your feedback. I have tested the scenario with my sample files with Aspose.PDF Cloud SDK for Java 21.1 and unable to reproduce the issue. As requested above please share your sample code along with the input documents. It will help us to investigate the issue exactly.

// Get App Key and App SID from https://dashboard.aspose.cloud/
PdfApi pdfApi = new PdfApi("xxxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx");
			
String folder="Temp";
// Storage name is default storage
String storage = null;

ArrayList<String> nameList = new ArrayList<String>();
nameList.add("4pages.pdf");
nameList.add("barcode-examples.pdf");
nameList.add("02_pages.pdf");	        

for (String name : nameList)
{
	File file = new File("C:/Temp/"+name);
	pdfApi.uploadFile(folder+"/"+name ,file,storage);
    //th.uploadFile(name);
}

String resultName = "MergingResult.pdf";

MergeDocuments mergeDocuments = new MergeDocuments();

for(int i = 0; i < nameList.size(); i++)
{
    nameList.set(i, folder + '/' + nameList.get(i));
    }

    mergeDocuments.setList(nameList);
    

    DocumentResponse response = pdfApi.putMergeDocuments(resultName, mergeDocuments, null, folder);
    
//Download Merge PDF file from Cloud Storage
File downloadResponse = pdfApi.downloadFile(folder+"/"+resultName, null, null);
File dest = new File("C:/Temp/MergedPDF.pdf");
Files.copy(downloadResponse.toPath(), dest.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
System.out.println("Completed......");

We are invoking the API using Postman.

PUT: https://api.aspose.cloud/v3.0/pdf/MergedFile_PREVIEW.pdf/merge?storage=BullhornSandbox&folder=a2e17000000uYFxAAM

Successful request:

{
  "List": [
    "06817000000yTm1AAE.pdf"
    ,"06817000000yTm6AAE.pdf"
  ]
}

Request that fails:

{
  "List": [
    "06817000000yTm1AAE.pdf"
    ,"06817000000yTm6AAE.pdf"
    ,"06817000000yTlwAAE.pdf"
  ]
}

Rather than including the PDF files in this public forum, could I email them to you or could you access them directly from our storage account?

Thanks!

@jnugent

I am afraid we cannot access customer storage accounts as these are encrypted. However, you may send me via private message. Please click on my user icon and use the message option for the purpose.

@jnugent

Thanks for sharing the documents in the private message. We reproduced the reported exception with shared documents and logged a ticket PDFCLOUD-1873 for rectification. We will notify you as soon as we resolve the issue.

@tilal.ahmad, has any progress been made on this ticket? It would be helpful to know when we should expect it to be resolved. Thanks.

@jnugent

After initial investigation, we found that an error occurs while creating a response. However, the process of merging and uploading the resulting file to the storage goes without error. Despite the API error, you can find the resulting file in the storage. However, we are working on the complete fix of the issue. We will notify you as soon as it is resolved.

@tilal.ahmad, thank you for the additional info. Our process requires the response to accurately indicate the file was generated. Can this be escalated? And when should we expect a fix? This issue is disrupting our process.

@jnugent

I am afraid we cannot share any ETA at the moment as the issue investigation is pending in the queue. However, we have recorded your concern and will share an ETA as soon as the issue investigation is completed.

@jnugent

Please note your above reported issue(PDFCLOUD-1873) is resolved in Aspose.PDF Cloud 21.3. Kindly use the latest version of Aspose.PDF Cloud, it will fix the issue.