Concatenating PDFs - Can I close each pdf File Stream after concatenation?

I have the following java code:

PdfFileEditor pdfEditor = new PdfFileEditor();
pdfEditor.setOptimizeSize(true);
pdfEditor.setCloseConcatenatedStreams(true);
pdfEditor.concatenate((String[]) pdfs.toArray(), mergedPdfFileLocation);

The code works and concatenates the pdfs in the Array into a single Merged PDF. However, after this is complete, I can’t remove the original PDFs that were concatenated together. When I try deleting them, I receive the following message:
2018-10-04_9-27-47.jpg (39.6 KB)

It looks like the PdfEditor Object is keeping the stream open after concatenation? Is there any way I can close each stream so the pdfs can be removed?

Any ideas would be greatly appreciated.

Thanks,
Brandon

@Brandon9595

Thank you for contacting support.

We have tried to use pdfs as string array and everything worked fine. Would you please share complete code which includes loading of files in pdfs stream so that we may try to reproduce and investigate it further. Also, you may try to assign null value to pdfEditor object after the concatenation completes, then share your kind feedback with us.

@Farhan.Raza
Hi sorry for my late response.

//pdfs2Merge is a comma separate list of file paths. These are the files that will be merged.
List pdfs = Arrays.asList(pdfs2Merge.split(","));

PdfFileEditor pdfEditor = new PdfFileEditor();
pdfEditor.setCloseConcatenatedStreams(true);
pdfEditor.setOptimizeSize(true);
//mergedPdfFileLocation is the path of the new merged file
pdfEditor.concatenate((String[]) pdfs.toArray(), mergedPdfFileLocation);

//The method that runs the merge code returns the file path of the merged document.
File mergedFile = new File(mergeResultString);

if(mergedFile.exists()) {
//Remove individual pdfs.
List pdfs2Remove = Arrays.asList(fileList.split(","));

			if(pdfs2Remove.size() > 1) {
				for(int pdfIndex = 0; pdfIndex < pdfs2Remove.size(); pdfIndex++) {
					String fileLocation = pdfs2Remove.get(pdfIndex);
					File pdfFile = new File(fileLocation);
					
					boolean pdfRemoved = pdfFile.delete();
					
					if(pdfRemoved == true) {
						System.out.println(prefix + pdfFile.getName() + " Deleted Successfully from the Server Docs Folder!");
					} else {
						System.out.println(prefix + pdfFile.getName() + " was NOT Deleted Successfully from the Server Docs Folder!");
					}
				}
			}

}

The attempt to delete the old pdfs at the end fails and I get a message saying the jvm still has each of those documents open.

Thanks in Advance

I did try assigning null to the PdfFileEditor but that didn’t seem to change the results.

@Brandon9595

Thank you for getting back to us.

Kindly share SSCCE code with us including the declaration and initialization of pdfs2Merge list so that we may try to reproduce and investigate it in our environment. Before sharing requested data, please ensure using Aspose.PDF for Java 18.11.

Hi @Farhan.Raza

Thank you for your reply. I ended up slimming down the code to see if I could isolate the issue.

Here’s a copy of the test webservice endpoint I created in order to test this issue out. I attached the two test pdf’s as well TEST 1.pdf (144.7 KB), TEST 2.pdf (145.1 KB)

If I run the following code in a simple java project on my machine I don’t seem to get the issue where I can’t delete the TEST 1 and TEST 2 pdfs after the merge… However, if I run the code from a webservice endpoint (Deployed on a local tomcat instance) then I can’t seem to delete the TEST pdfs after the merge.

@GET
@Path("MergeDocs")
public String mergeDocs() {
	com.aspose.pdf.License pdfLicense = new com.aspose.pdf.License();
	InputStream keyStream = LicenseeLookupEndpointsImpl.class.getResourceAsStream("/aspose_key/Aspose.Total.Java.lic");
	try {
		pdfLicense.setLicense(keyStream);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
	try {
		keyStream.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	String[] files = new String[2];
	files[0] = "C:\\Users\\cea296\\Desktop\\TEST 1.pdf";
	files[1] = "C:\\Users\\cea296\\Desktop\\TEST 2.pdf";
	
	PdfFileEditor pdfEditor = new PdfFileEditor();
	pdfEditor.setCloseConcatenatedStreams(true);
	pdfEditor.setOptimizeSize(true);
	pdfEditor.concatenate(files, "C:\\Users\\cea296\\Desktop\\MergeTest.pdf");
	return "Done";
} 

Please let me know what you think.
Thanks in Advance.

Oh, sorry I forgot to mention that I am now using version 18.11. Previously I was not, but updated today. The issue seems to persist between versions.

@Brandon9595

Thank you for elaborating it.

It appears like some module of web server holds the resources for a while and probably not a problem with Aspose.PDF for Java API. However, we still have logged an investigation ticket with ID PDFJAVA-38246 in our issue management system for further investigations. We will let you know as soon as some significant updates will be available in this regard.

The issues you have found earlier (filed as PDFJAVA-38246) have been fixed in Aspose.PDF for Java 19.1.