删除文件时的 IOError

IOError on file deletion

以下代码用于给pdf加水印:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WaterDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            string FileLocation = "C:\Users\Desktop\Hello.pdf";
          //  string watermarkedFile = "Watermarked.pdf";
            // Creating watermark on a separate layer
            // Creating iTextSharp.text.pdf.PdfReader object to read the Existing PDF Document
            PdfReader reader1 = new PdfReader(FileLocation);
            using (FileStream fs = new FileStream(FileLocation.Replace(".pdf","[temp][file].pdf"), FileMode.Create))
            // Creating iTextSharp.text.pdf.PdfStamper object to write Data from iTextSharp.text.pdf.PdfReader object to FileStream object
            using (PdfStamper stamper = new PdfStamper(reader1, fs))
            {
                // Getting total number of pages of the Existing Document
                int pageCount = reader1.NumberOfPages;

                // Create New Layer for Watermark
                PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
                // Loop through each Page
                for (int i = 1; i <= pageCount; i++)
                {
                    // Getting the Page Size
                    Rectangle rect = reader1.GetPageSize(i);

                    // Get the ContentByte object
                    PdfContentByte cb = stamper.GetUnderContent(i);

                    // Tell the cb that the next commands should be "bound" to this new layer
                    cb.BeginLayer(layer);
                    cb.SetFontAndSize(BaseFont.CreateFont(
                      BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);

                    PdfGState gState = new PdfGState();
                    gState.FillOpacity = 0.25f;
                    cb.SetGState(gState);

                    cb.SetColorFill(BaseColor.BLACK);
                    cb.BeginText();
                    cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Confidential", rect.Width / 2, rect.Height / 2, 45f);
                    cb.EndText();

                    // Close the layer
                    cb.EndLayer();
                }
                stamper.Close();
            }

            File.Delete(FileLocation);  //error on this line
            File.Move(FileLocation.Replace(".pdf", "[temp][file].pdf"), FileLocation);
        }
    }
}

在这一行我收到错误

File.Delete(FileLocation);

它向我抛出以下错误

The process cannot access the file 'C:\Users\Desktop\Hello.pdf' because it is being used by another process. Please help me find the problem in my code.

您没有在 PdfReader 对象 reader1 上调用 Close,在 File.Delete 调用之前添加:

reader1.Close()

您还应该考虑在这一行之后添加花括号:

using (FileStream fs = new FileStream(FileLocation.Replace(".pdf","[temp][file].pdf"), FileMode.Create))

如果直接在该行下方添加一行,则存在风险,因为 using 语句直接在下面的行终止,因此您的代码将停止工作。