Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 删除文件时出错_C#_File_Itextsharp_Watermark - Fatal编程技术网

C# 删除文件时出错

C# 删除文件时出错,c#,file,itextsharp,watermark,C#,File,Itextsharp,Watermark,以下代码用于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[]

以下代码用于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);
        }
    }
}
这让我犯了以下错误

进程无法访问文件“C:\Users\Desktop\Hello.pdf”,因为另一进程正在使用该文件。 请帮我找到代码中的问题


您没有在PdfReader对象读取器1上调用Close,请将其添加到文件之前。删除调用:

reader1.Close()

你也应该考虑在这条线之后添加一个卷曲支撑:

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

在该行的正下方添加一行可能会导致代码停止工作,因为using语句直接终止于下一行。

请关闭该文件,然后将其删除,好吗?您可能是在代码中打开它来执行某些操作,而不是关闭它。所以请检查一下,然后再试一次。同时检查在管理模式下运行VS的u r。是否可以在此处粘贴
iTextSharp
DLL?如果是,请将dll粘贴到此处
using (FileStream fs = new FileStream(FileLocation.Replace(".pdf","[temp][file].pdf"), FileMode.Create))