为什么通过下列代码创建的水印 即使在代码中修改水印文本结果显示的时候还是第一次创建的水印 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, watermark, x, y, 45); over.EndText(); over.RestoreState(); } stamper.Close(); pdfViewer1.Document = PdfiumViewer.PdfDocument.Load("temp.pdf"); }
时间: 2023-12-11 13:05:45 浏览: 36
这是因为你在创建水印时,使用了一个固定的文本变量 "watermark",而没有实时更新它。所以,无论你在代码中如何修改文本,都只会显示第一次创建的水印。
要解决这个问题,你需要在循环中动态地更新水印文本变量。例如,你可以将水印文本变量放在一个数组或列表中,并在循环中根据当前页数来获取对应的文本。示例代码如下:
```
string[] watermarkTexts = new string[] { "水印1", "水印2", "水印3" };
using (PdfStamper stamper = new PdfStamper(reader, new FileStream("temp.pdf", FileMode.Create)))
{
for (int i = 1; i <= pageCount; i++)
{
// 获取当前页码对应的水印文本
string watermarkText = watermarkTexts[i % watermarkTexts.Length];
// 其他水印代码...
}
}
```
这样,每次循环都会根据当前页数获取对应的水印文本,从而实现动态更新水印的效果。
相关问题
使用itextsharp 在winfrom上先在每一页添加水印再显示pdf
可以使用 iTextSharp 库在每一页添加水印并且显示 PDF,步骤如下:
1. 引入 iTextSharp 库。
2. 在窗体加载时,使用 iTextSharp 库读取 PDF 文件,并在每一页上添加水印。
3. 显示带有水印的 PDF 文件。
下面是一个示例代码:
```c#
using iTextSharp.text;
using iTextSharp.text.pdf;
private void Form1_Load(object sender, EventArgs e)
{
// 读取 PDF 文件
PdfReader reader = new PdfReader("your_pdf_file.pdf");
int pageCount = reader.NumberOfPages;
// 添加水印
for (int i = 1; i <= pageCount; i++)
{
PdfStamper stamper = new PdfStamper(reader, new FileStream("temp.pdf", FileMode.Create));
// 水印文本
string watermarkText = "This is a watermark text";
// 水印字体
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 32, Font.BOLD, new BaseColor(255, 0, 0, 128));
// 水印位置
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(font.BaseFont, font.Size);
over.SetColorFill(font.Color);
over.ShowTextAligned(Element.ALIGN_CENTER, watermarkText, x, y, 45);
over.EndText();
over.RestoreState();
stamper.Close();
}
// 显示 PDF 文件
axAcroPDF1.src = "temp.pdf";
}
```
注意要在窗体上添加一个 ActiveX 控件,名称为 “axAcroPDF1”,用于显示 PDF 文件。
itextsharp php,详解C#使用iTextSharp添加PDF水印的代码案例
很抱歉,我并不是一个可以展示代码的应用程序,也无法进行详细的教学。但我可以给你一些指导:
iTextSharp 是一个用于创建和操作 PDF 文件的 C# 库。它可以用来添加水印、文本、图片和表单域等元素到 PDF 文件中。
下面是一个简单的示例,展示如何使用 iTextSharp 在 PDF 文件中添加一个文本水印:
```csharp
using iTextSharp.text;
using iTextSharp.text.pdf;
// 打开需要添加水印的 PDF 文件
PdfReader reader = new PdfReader("input.pdf");
// 创建一个输出流,用于将修改后的 PDF 写入文件
PdfStamper stamper = new PdfStamper(reader, new FileStream("output.pdf", FileMode.Create));
// 创建一个字体对象,用于设置水印的字体和大小
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 48);
// 在 PDF 的每个页面上添加一个文本水印
int pageCount = reader.NumberOfPages;
for (int i = 1; i <= pageCount; i++)
{
// 获取该页面的 PDF 内容
PdfContentByte content = stamper.GetUnderContent(i);
// 创建一个列对象,用于设置水印的位置和旋转角度
ColumnText column = new ColumnText(content);
// 设置水印的位置和旋转角度
column.SetSimpleColumn(new Phrase("水印文本", font), 0, 0, 0, 0, 0, Element.ALIGN_CENTER);
// 设置水印的旋转角度
column.Alignment = Element.ALIGN_CENTER;
column.SetTextMatrix(30, 30);
column.Angle = 45;
// 将水印添加到页面中
column.Go();
}
// 关闭输出流和输入流
stamper.Close();
reader.Close();
```
这是一个简单的示例,仅用于演示如何使用 iTextSharp 添加水印。如果你需要更详细的教程或更复杂的功能,请参考 iTextSharp 的官方文档。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)