1

尝试删除文件时,我不断收到上述错误。请参阅代码注释以准确了解错误发生的位置。

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);
    }
}

这是错误之前的代码,在 PDFImageExtractor.ExtractImagesFromPDF 方法中:

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;
}

我错过了某处的使用!?

4

2 回答 2

3

“raf”变量未使用且未关闭。它正在锁定文件。

RandomAccessFileOrArray raf = 
        new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf);
于 2012-08-23T20:29:43.177 回答
2

我认为你的:

System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);

缺少使用。

PdfReader 或 RandomAccessFileOrArray 是否实现 IDisposable?

可能很傻,但可能值得使用 Process Explorer 来查看是否有其他东西可以处理文件。

于 2012-08-23T20:11:09.833 回答