在PdfiumViewer库的pdfViewer1控件的给每一页pdf添加水印 写一下示例代码,把所有的using引用也带上
时间: 2023-12-06 22:44:38 浏览: 188
以下是一个示例代码,可以在PdfiumViewer库的pdfViewer1控件的每一页上添加水印。
```csharp
using PdfiumViewer;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
private void AddWatermarkToPdf(PdfDocument document, string watermarkText)
{
using (var font = new Font("Arial", 30))
{
for (int pageIndex = 0; pageIndex < document.PageCount; pageIndex++)
{
var page = document.Pages[pageIndex];
using (var graphics = page.CreateGraphics())
{
var watermarkSize = graphics.MeasureString(watermarkText, font);
var watermarkPosition = new PointF(page.Width - watermarkSize.Width - 20, page.Height - watermarkSize.Height - 20);
using (var brush = new SolidBrush(Color.FromArgb(50, Color.Gray)))
{
graphics.FillRectangle(brush, new RectangleF(watermarkPosition.X, watermarkPosition.Y, watermarkSize.Width, watermarkSize.Height));
}
using (var brush = new SolidBrush(Color.Red))
{
graphics.DrawString(watermarkText, font, brush, watermarkPosition);
}
}
}
}
}
private void PdfViewer1_LoadCompleted(object sender, EventArgs e)
{
AddWatermarkToPdf(pdfViewer1.Document, "My Watermark Text");
pdfViewer1.Refresh();
}
```
此示例使用了PdfiumViewer库来打开并编辑PDF文档。通过在PdfDocument的每一页上创建Graphics对象并绘制水印,可以将水印添加到PDF中。请注意,此示例简单地使用了纯文本水印,但你可以使用更复杂的图像或其他元素来创建自己的水印。
阅读全文