EDN Admin
Well-known member
Im writing some code to join some PDF files together. It was working well except I noticed that the files themselves were being held open because I wasnt disposing the resources. Apparently a Using block would solve these issues, except Im having some issues using the Using statement along with the need to look through each document that needs to be merged.
Here is the code I have at the moment
rivate static Document MergeFiles(string[] fileNames, string finalFileName)
{
Document doc = new Document(PageSize.A4, 20, 20, 25, 25);
if (fileNames.Length > 0)
{
int a = 0;
while (a < fileNames.Length)
{
using (PdfReader reader = new PdfReader(fileNames[a]))
using (FileStream output = new FileStream("C:\temp\" + finalFileName, FileMode.Create))
using (PdfWriter writer = PdfWriter.GetInstance(doc, output))
{
a++;
int n = reader.NumberOfPages;
doc.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
int i = 0;
while (i < n)
{
i++;
doc.SetPageSize(reader.GetPageSizeWithRotation(i));
doc.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
else
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
if (a < fileNames.Length)
{
n = reader.NumberOfPages;
}
}
}
}
doc.Close();
return doc;
}
The error Im receiving is an "ObjectDisposedException was unhandled" error because it "Cannot access a closed file". I presume this is happening because the Using statement is closing the readers and writers, but if theyre being recreated in the loop then shouldnt they be open??
Any help would be appreciated. This is doing my head in
View the full article
Here is the code I have at the moment

{
Document doc = new Document(PageSize.A4, 20, 20, 25, 25);
if (fileNames.Length > 0)
{
int a = 0;
while (a < fileNames.Length)
{
using (PdfReader reader = new PdfReader(fileNames[a]))
using (FileStream output = new FileStream("C:\temp\" + finalFileName, FileMode.Create))
using (PdfWriter writer = PdfWriter.GetInstance(doc, output))
{
a++;
int n = reader.NumberOfPages;
doc.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
int i = 0;
while (i < n)
{
i++;
doc.SetPageSize(reader.GetPageSizeWithRotation(i));
doc.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
else
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
if (a < fileNames.Length)
{
n = reader.NumberOfPages;
}
}
}
}
doc.Close();
return doc;
}
The error Im receiving is an "ObjectDisposedException was unhandled" error because it "Cannot access a closed file". I presume this is happening because the Using statement is closing the readers and writers, but if theyre being recreated in the loop then shouldnt they be open??
Any help would be appreciated. This is doing my head in

View the full article