Merge PDF documents - ObjectDisposedException at doc.Save() method

I am trying to concatenate pdf documents using the following code:

protected static void AppenDocuments(string mainDocumentPath, string[] appendDocumentPaths)
{
using (Aspose.Pdf.Document doc = new Aspose.Pdf.Document(mainDocumentPath))
{
foreach (string path in appendDocumentPaths)
{
using (Aspose.Pdf.Document d = new Aspose.Pdf.Document(path))
{
doc.Pages.Add(d.Pages);
}
}
doc.Save(mainDocumentPath);
}
}

The doc.Save(mainDocumentPath) throws an ObjectDisposedException.

The following code works, but is likely to leak resources since I am not disposing the appended pdf documents".

protected static void AppenDocuments2(string mainDocumentPath, string[] appendDocumentPaths)
{
using (Aspose.Pdf.Document doc = new Aspose.Pdf.Document(mainDocumentPath))
{
foreach (string path in appendDocumentPaths)
{
Aspose.Pdf.Document d = new Aspose.Pdf.Document(path);
doc.Pages.Add(d.Pages);
}
doc.Save(mainDocumentPath);
}
}

Any help is appreciated

Best regards,
Håkan

@holsen

Thank you for contacting support.

Please note source of ObjectDisposedException is not Aspose.PDF API but is related to System.IOException so the second approach should be used where you may call doc.Dispose method at the end.

We hope this will be helpful. Feel free to contact us if you need any further assistance.

Thank you Farhan for your response.

I do understand that the ObjectDisposedException is an IOException. I do however find it a bit surprising that the inner pdf documents, i.e. the ones that I append to the main document, disposes the main document when they are disposed. Is this on purpose? If it is on purpose it leads to a couple of questions…

If you only sometimes should dispose of the disposable object “Aspose.Pdf.Document”, how do you know when to?

How do I know that the inner pdf documents are disposed when I dispose the main document? Are they?

If I appent many documents to the main document, does this mean that all the file handles have to be open until all documents are appended?

Thank you for your help!
Håkan

@holsen

We have logged an investigation ticket with ID PDFNET-45703 in our issue management system. We will share our findings with you after investigating this in detail. Please be patient and spare us little time.

Is there any news on this matter?

Let’s say that I want to traverse a folder structure recursively in order to concatenate all pdf documents in that structure. Is there a way to do this? If so could you please supply cample code?

Since the Aspose.Pdf.Document object is disposable i assume that you must call the dospose method to prevent leaking resourses.

Thank you for your help!
Håkan

@holsen

You can use following code snippet to merge PDF files in a directory + subdirectories:

string[] filenames = Directory.GetFileSystemEntries("D:\\", "*.pdf", SearchOption.AllDirectories);
Document doc = new Document();
foreach(string file in filenames)
{
  Document document = new Document(file);
  doc.Pages.Add(document.Pages);
}
doc.Save(dataDir + "outpt.pdf");

You can call Dispose() method in order to make sure that all allocated resources are free. However, Document.Save() method also behaves in the same way as it also disposes of allocated objects being used by Document instance.

We regret that earlier logged ticket is not yet resolved due to other high priority tasks and implementations. We will surely inform you as soon as it is resolved. Please spare us some time.

We are sorry for the inconvenience.