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.