Can't delete file attachment

I’ve created a c# console application that attaches a file (“C:\TEMP\test.wav”) to an Aspose.PDF document and saves the document.

After the pdf is saved, my application attempts to delete the original file attachment. The file can never be deleted due to this exception:

“The process cannot access the file “C:\TEMP\test.wav” because it is being used by another process.“

So it appears to be that the Aspose.PDF is locking the attched file.

Is there a method that I need to call to “Close” or “Dispose” the Aspose PDF object after I’m done using it?

Here is the code for my application:

static void Main(string[] args)
{
// Set license
Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
pdfLicense.SetLicense(“Aspose.Custom.lic”);

// Create PDF doc
Pdf pdf = new Pdf();
Section embeddedFilesSection;
Aspose.Pdf.Table attachedFilesTable;

embeddedFilesSection= new Section(pdf);

HeaderFooter headerFooter = new HeaderFooter(embeddedFilesSection);
embeddedFilesSection.OddHeader = headerFooter;
embeddedFilesSection.EvenHeader = headerFooter;
Text text = new Text( headerFooter, “Attached Files”);
headerFooter.Margin.Top= text.TextInfo.FontSize;
headerFooter.Paragraphs.Add(text);

attachedFilesTable = new Aspose.Pdf.Table();
embeddedFilesSection.Paragraphs.Add(attachedFilesTable);
attachedFilesTable.DefaultCellPadding.Bottom= embeddedFilesSection.TextInfo.FontSize/2;
int attachIconSize= (int)embeddedFilesSection.TextInfo.FontSize;
attachedFilesTable.ColumnWidths = string.Format(”{0} {1}”,
attachIconSize,
embeddedFilesSection.PageInfo.PageWidth-(embeddedFilesSection.PageInfo.Margin.Left+embeddedFilesSection.PageInfo.Margin.Right+attachIconSize));


FileInfo file = new FileInfo(@“C:\TEMP\test.wav”);
if(file.Exists)
{
// Attach File
Row row = attachedFilesTable.Rows.Add();

Attachment fileAttachment = new Attachment();
fileAttachment.AttachmentType = AttachmentType.File;
fileAttachment.AttachedFileName = file.FullName;
if (file.Extension.Length>2)
{
fileAttachment.AttachedFileType = file.Extension.Substring(1).ToLower();
}
else
{
fileAttachment.AttachedFileType= “application”;
}
fileAttachment.FileIconType = FileIconType.PaperClip;
fileAttachment.IconColor = new Aspose.Pdf.Color(“Black”);

Cell attachCell = row.Cells.Add();
attachCell.Paragraphs.Add(fileAttachment);

Cell filenameCell = row.Cells.Add();
Heading heading= new Heading( pdf, embeddedFilesSection, 1);
heading.Segments.Add( file.Name);
filenameCell.Paragraphs.Add(heading);

pdf.Sections.Add(embeddedFilesSection);

// Save PDF doc
pdf.Save(@“C:\TEMP\test.pdf”);

// Delete file attachment
try
{
file.Delete();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}

}

Hi,

Thank you for considering Aspose.

In my case it works fine, the file is deleted after attaching to Pdf file. The problem might be that this “wav” file is opened/loaded in some media player. Please try to close the media player and then try again. Hope it works.

Thanks.

I have confirmed that there are no other applications using this file. This doesn’t just happen with this file, it happens with any file that is attached to the PDF file.

In your case, were you running the exact code that is in my original post or were you using your own code?

Hi,

I am using the code you have posted earlier. Please check with the latest version of Aspose.Pdf and let us know the results.

Thanks.

Thank you, that resolved the issue, no code change needed.