在下边的代码中如何释放pdfViewer1.Document using (PdfReader reader = new PdfReader(filepath+ filetype_pdf)) { int pageCount = reader.NumberOfPages; // 添加水印 using (PdfStamper stamper = new PdfStamper(reader, new FileStream("temp.pdf", FileMode.Create))) { for (int i = 1; i <= pageCount; i++) { // 水印文本 //string watermarkText = "鲁达LUDA 鲁达LUDA 鲁达LUDA 鲁达LUDA 鲁达LUDA"; // 水印字体 BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); Font font = new Font("Black", 75, FontStyle.Bold); //Brush brush = new SolidBrush(Color.FromArgb(128, Color.LightGray)); // 水印位置 iTextSharp.text.Rectangle rect = reader.GetPageSize(i); float x = rect.Width / 2; float y = rect.Height / 2; // 添加水印 PdfContentByte over = stamper.GetOverContent(i); over.SaveState(); over.SetGState(new PdfGState() { FillOpacity = 0.5f, StrokeOpacity = 0.5f }); over.BeginText(); over.SetFontAndSize(bf, font.Size); over.SetColorFill(BaseColor.LIGHT_GRAY); over.ShowTextAligned(Element.ALIGN_CENTER, "鲁达LUDA 鲁达LUDA 鲁达LUDA 鲁达LUDA 鲁达LUDA", x, y, 45); over.EndText(); over.RestoreState(); } stamper.Close(); pdfViewer1.Document = PdfiumViewer.PdfDocument.Load("temp.pdf"); } }
时间: 2023-12-18 18:02:33 浏览: 175
可以使用以下代码释放pdfViewer1.Document:
```csharp
if (pdfViewer1.Document != null)
{
pdfViewer1.Document.Dispose();
pdfViewer1.Document = null;
}
```
将其放在 `using` 块的外面即可,如下所示:
```csharp
using (PdfReader reader = new PdfReader(filepath+ filetype_pdf))
{
int pageCount = reader.NumberOfPages;
using (PdfStamper stamper = new PdfStamper(reader, new FileStream("temp.pdf", FileMode.Create)))
{
for (int i = 1; i <= pageCount; i++)
{
// 添加水印
// ...
}
stamper.Close();
if (pdfViewer1.Document != null)
{
pdfViewer1.Document.Dispose();
pdfViewer1.Document = null;
}
pdfViewer1.Document = PdfiumViewer.PdfDocument.Load("temp.pdf");
}
}
```
阅读全文