I have a C# .NET program running in Visual Studio. Its function is to merge PDF files together based on their file names. It works fine for the first 12 or so PDFs but then suddenly exits with code 0 at a certain point. I am not using multi-threading and the PDF library I am using is iTextSharp.
public static string MergePDFs(List<string> fileNames, ref string targetPdf)
{
string merged = "";
using (FileStream stream = new FileStream(targetPdf, FileMode.Create))
{
Document document = new Document();
PdfCopy pdf = new PdfCopy(document, stream);
PdfReader reader = null;
try
{
document.Open();
foreach (var file in fileNames)
{
reader = new PdfReader(file);
pdf.AddDocument(reader);
reader.Close();
}
}
catch (Exception)
{
// merged = "false";
if (reader != null) reader.Close();
}
finally
{
if (document != null)
{
Console.WriteLine("Closing document.");
document.Close(); // exits here
Console.WriteLine("Document closed.");
}
}
}
return merged;
}
The output in the debugger is:
...
Closing document.
The program has exited with code 0 (0x0)