Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:01:51+00:00 2026-06-10T08:01:51+00:00

I keep getting the above error when trying to delete the file. See code

  • 0

I keep getting the above error when trying to delete the file. See code comment to know exactly where the error happens.

foreach (string file in supportedFiles)
{
    if (file.EndsWith(".pdf"))
    {
        PDFImageExtractor.ExtractImagesFromPDF(
            file, 
            AppVars.TempFolder, 
            Path.GetFileNameWithoutExtension(file));
    }
}

foreach (string file in supportedFiles)
{
    if (file.EndsWith(".pdf"))
    {
        // I get the error here. 
        File.Delete(file);
    }
}

Here’s the code prior to the error, in the PDFImageExtractor.ExtractImagesFromPDF method:

public static void ExtractImagesFromPDF(
    string sourcePdf, 
    string outputPath, 
    string fileName)
{
    // NOTE: This will only get the first image it finds per page.
    PdfReader pdf = new PdfReader(sourcePdf);
    RandomAccessFileOrArray raf = 
        new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf);

    try
    {
        for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
        {
            PdfDictionary pg = pdf.GetPageN(pageNumber);
            PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
            PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));

            if (xobj != null)
            {
                foreach (PdfName name in xobj.Keys)
                {
                    PdfObject obj = xobj.Get(name);
                    if (obj.IsIndirect())
                    {
                        PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                        PdfName type = 
                            (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));

                        if (PdfName.IMAGE.Equals(type))
                        {
                            int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(CultureInfo.InvariantCulture));
                            PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
                            PdfStream pdfStrem = (PdfStream)pdfObj;
                            byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem);
                            if ((bytes != null))
                            {
                                using (MemoryStream memStream = new MemoryStream(bytes))
                                {
                                    memStream.Position = 0;
                                    System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);
                                    // must save the file while stream is open.
                                    if (!Directory.Exists(outputPath))
                                        Directory.CreateDirectory(outputPath);

                                    string path = Path.Combine(outputPath, String.Format(@"{0}.jpg", fileName));

                                    using (EncoderParameters parms = new EncoderParameters(1))
                                    {
                                        parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0);
                                        // GetImageEncoder is found below this method
                                        ImageCodecInfo jpegEncoder = GetImageEncoder("JPEG");
                                        img.Save(path, jpegEncoder, parms);
                                    }

                                    //EncoderParameters parms = new EncoderParameters(1);

                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        pdf.Close();
    }
}

public static ImageCodecInfo GetImageEncoder(string imageType)
{
    imageType = imageType.ToUpperInvariant();

    foreach (ImageCodecInfo info in ImageCodecInfo.GetImageEncoders())
    {
        if (info.FormatDescription == imageType)
        {
            return info;
        }
    }
    return null;
}

Am I missing a using somewhere!?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:01:52+00:00Added an answer on June 10, 2026 at 8:01 am

    The “raf” variable is not used and not closed. It is locking the file.

    RandomAccessFileOrArray raf = 
            new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting the above error when trying to transform XML that's generated in
I'm trying to extend ListFragment, but I keep getting the error stated above. I
Keep getting this error after inserting a subdatasheet into a query and trying to
i keep getting a sytax error when trying to add (function (e) { e.preventDefault;
I am trying to grab a webpage with actionscript, but keep getting this error
I'm trying to parse this string into java, but I keep getting errors. {id:1,jsonrpc:2.0,result:{limits:{end:3,start:0,total:3},sources:[{file:/media/storage/media/re
I'm getting the same error when I'm trying to delete my save files. The
I'm trying to use slime from CVS (2009-01-05) but keep getting this error: LOAD:
I keep getting the following error message when trying to UPDATE one of my
Keep getting the error Arguments are not sufficiently instantiated for the multiplication by addition

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.