Unable to delete pdf files

Hi,
We are trying to merge some pdf files in a loop and then delete the individual files. We are able to delete only one pdf file and the rest of the files are not getting deleted. There seem to be some references held to the files. For your reference we are using the below piece of code for this.

public class PDFMerger {

public static void main(String[] args) throws Exception {
	// TODO Auto-generated method stub
			PDFMerger merger= new PDFMerger();
			merger.mergePDFFiles();
}
		
		public void mergePDFFiles() throws Exception
		{
			
			String File1="E://test//1.pdf";
			String File2="E://test//2.pdf";
			String File3="E://test//3.pdf";
			String File4="E://test//4.pdf";
			
			  Document inputDoc=null;

			  List<String> inputDocs = new ArrayList<String>();

			  inputDocs.add(File1);

			  inputDocs.add(File2);
			  inputDocs.add(File3);
			  inputDocs.add(File4);
			  
			  Document targetDoc= new Document();
			  targetDoc.getForm().setType(com.aspose.pdf.FormType.Standard);
			  for (int i = 1; i < inputDocs.size(); i++)
			  {
			   inputDoc = new Document(inputDocs.get(i));
			   inputDoc.getForm().setType(com.aspose.pdf.FormType.Standard);
			   targetDoc.getPages().add(inputDoc.getPages());
			  				   
			  }
			  
			  targetDoc.save("E:\\test\\" + "merged.pdf");
			  inputDoc.close();
			  inputDoc.dispose();
			  targetDoc.dispose();
			  targetDoc.close();
			  
			  for(int j=0;j<inputDocs.size();j++)
			  {
				  File file= new File(inputDocs.get(j));
				  if(file.exists())
				  {
				  file.delete();
				  }
			  }
			 
		}

}

The above piece of code is not deleting the pdf files. We need to delete them after they are merged and saved as a single file. Please help.

Thanks,
-Eswar.

@rsatti.imedx

Thanks for contacting support.

We have tested the scenario in our environment and managed to notice the issue of file deletion. However in our case, only 2 files were deleted and rest stayed in place. We have logged an investigation ticket as PDFJAVA-38042 in our issue tracking system. We will further look into details of this behavior and keep you posted with the status of ticket resolution. Please spare us little time.

We are sorry for the inconvenience.

@rsatti.imedx

We have further investigated the logged ticket and found reason of the issue was - the inputDocs were opened but were not closed properly. You should close an every inputDoc separately:

...

List<Document> inputDocObjs = new ArrayList<Document>();
      
for (int i = 1; i < inputDocs.size(); i++) {
  inputDoc = new Document(inputDocs.get(i));
  inputDoc.getForm().setType(FormType.Standard);
  targetDoc.getPages().add(inputDoc.getPages()); 
  inputDocObjs.add(inputDoc);
}

targetDoc.save(dataDir + "merged.pdf");

// close input docs
 for (Document inputDocObj : inputDocObjs) {
    inputDocObj.close();
}

// delete files

OR you could use streams to avoid blocking the file:

...
    InputStream inputDocSteam = null;
    for (int i = 1; i < inputDocs.size(); i++) {
       inputDocSteam = new FileInputStream(inputDocs.get(i));
       inputDoc = new Document(inputDocSteam);

       inputDoc.getForm().setType(FormType.Standard);
       targetDoc.getPages().add(inputDoc.getPages());
     }
    
     targetDoc.save(dataDir + "merged.pdf");
     inputDoc.close();
     inputDocSteam.close();

    // delete files

In case you still face any issue, please feel free to let us know.