使用PdfiumViewer库的pdfViewer1控件查看PDF时每一页都添加水印
时间: 2023-11-27 13:53:28 浏览: 139
你可以在PdfiumViewer库的pdfViewer1控件的Paint事件中添加水印。以下是示例代码:
```csharp
private void pdfViewer1_Paint(object sender, PaintEventArgs e)
{
string watermarkText = "Confidential";
Font watermarkFont = new Font("Arial", 30);
Brush watermarkBrush = new SolidBrush(Color.FromArgb(50, Color.Red));
for (int i = 0; i < pdfViewer1.PageCount; i++)
{
RectangleF watermarkRect = pdfViewer1.GetPageRectangle(i);
SizeF textSize = e.Graphics.MeasureString(watermarkText, watermarkFont);
// Center the watermark text on the page
float x = (watermarkRect.Width - textSize.Width) / 2 + watermarkRect.Left;
float y = (watermarkRect.Height - textSize.Height) / 2 + watermarkRect.Top;
// Draw the watermark text on the page
e.Graphics.DrawString(watermarkText, watermarkFont, watermarkBrush, x, y);
}
}
```
该代码将在每个页面上添加“Confidential”水印。您可以使用不同的文本、字体和颜色来自定义水印。
阅读全文