Следующий код используется для создания водяных знаков PDF:IOError на удаление файла
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);
Он бросает мне следующую ошибку
Процесс не может получить доступ к файлу «C: \ Users \ Desktop \ Hello.pdf», потому что он используется другим процессом. Пожалуйста, помогите мне найти проблему в моем коде.
Не могли бы вы закрыть файл и удалить его? Вы можете открыть его в своем коде для выполнения какой-либо операции и не закрывать его. поэтому, пожалуйста, проверьте это и попробуйте. Также проверьте работу VS в режиме администратора. можно ли вставить здесь 'iTextSharp' dll? если да, пожалуйста, вставьте DLL здесь –