为什么使用下列代码给pdf添加的水印 就算修改了watermarktext变量的文本 打开pdf文件后里边的水印还是第一次生成代码时的水印 using (PdfReader reader = new PdfReader(path)) { int pageCount = reader.NumberOfPages; // 添加水印 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, watermarkText, x, y, 45); over.EndText(); over.RestoreState(); } stamper.Close(); pdfViewer1.Document = PdfiumViewer.PdfDocument.Load("temp.pdf"); }
时间: 2023-12-27 21:02:54 浏览: 234
这是因为你每次都是将原始的PDF文件作为源文件进行处理,而不是修改已经添加水印的PDF文件。因此,每次生成的PDF文件都是带有相同水印的原始文件。要解决这个问题,你需要在每次添加水印时使用新的输出文件名,而不是使用相同的文件名。修改代码如下:
```
using (PdfReader reader = new PdfReader(path))
{
int pageCount = reader.NumberOfPages;
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);
iTextSharp.text.Rectangle rect = reader.GetPageSize(i);
float x = rect.Width / 2;
float y = rect.Height / 2;
string outputFileName = Path.Combine(Path.GetDirectoryName(path), $"temp{i}.pdf");
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(outputFileName, FileMode.Create)))
{
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, watermarkText, x, y, 45);
over.EndText();
over.RestoreState();
}
pdfViewer1.Document = PdfiumViewer.PdfDocument.Load(outputFileName);
}
}
```
这样,每次添加水印时都会生成一个新的PDF文件,每个文件都包含不同的水印。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)