为什么通过下列代码创建的水印 即使在代码中修改水印文本结果显示的时候还是第一次创建的水印 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-14 11:39:57 浏览: 22
这是因为你在添加水印时创建了新的临时 PDF 文件,但在设置 pdfViewer1.Document 时,你却仍然使用原始的 PDF 文件(即 filepath+filetype_pdf)。因此,你需要将 pdfViewer1.Document 设置为新创建的临时文件 temp.pdf。在代码的最后添加以下代码即可:
```
pdfViewer1.Document = PdfiumViewer.PdfDocument.Load("temp.pdf");
```
相关问题
为什么通过下列代码创建的水印 即使在代码中修改水印文本结果显示的时候还是第一次创建的水印 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"); }
这是因为你在创建水印时,使用了一个固定的文本变量 "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 文件。