Hi Team,
I attempted to put a watermark, however it appears on the document's opposite side. please refer the code as below
private Stream ApplyPdfWatermark(Stream fileStream, List<string> data, WatermarkSettings settings,
WatermarkFileOutputType fileOutputType)
{
string text = string.Join(Environment.NewLine, data);
PdfBrush pdfBrush = FontColorMappings.GetFontColor(settings.FontColor);
int noOfMarkerLines = data.Count + 2;
float opacity = settings.Opacity / 100.0f;
var outputStream = new MemoryStream();
using (var doc = new PdfDocument())
{
doc.LoadFromStream(fileStream);
foreach (PdfPageBase page in doc.Pages)
{
RectangleF rectangleF = CalculateWatermarker(page.ActualSize.Width, page.ActualSize.Height);
var fontSize = (int)(Math.Min(rectangleF.Height, rectangleF.Width) / noOfMarkerLines);
var font = new PdfFont(PdfFontFamily.Helvetica, settings.AutoScale ? fontSize : settings.FontSize);
var pdfTilingBrush = new PdfTilingBrush(
new SizeF(page.ActualSize.Width, page.ActualSize.Height));
pdfTilingBrush.Graphics.SetTransparency(opacity);
_ = pdfTilingBrush.Graphics.Save();
float centerX = page.ActualSize.Width / 2;
float centerY = page.ActualSize.Height / 2;
rectangleF.X = page.ActualSize.Width / 2 - rectangleF.Width / 2;
rectangleF.Y = page.ActualSize.Height / 2 - rectangleF.Height / 2;
double radian = Math.Atan(page.ActualSize.Height / page.ActualSize.Width);
var degree = (float)(radian * (180 / Math.PI));
pdfTilingBrush.Graphics.RotateTransform(-degree, new PointF(centerX, centerY));
pdfTilingBrush.Graphics.DrawString(text,
font, pdfBrush, rectangleF,
new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));
pdfTilingBrush.Graphics.Restore();
page.Canvas.DrawRectangle(pdfTilingBrush,
new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
}
if (WatermarkFileOutputType.Png == fileOutputType)
{
doc.SaveToImageStream(0, outputStream, nameof(WatermarkFileOutputType.Png));
}
else
{
doc.SaveToStream(outputStream, FileFormat.PDF);
}
_ = outputStream.Seek(0, SeekOrigin.Begin);
return outputStream;
}
}