Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Sep 25, 2023 7:18 am

Hi Team,

When I try to apply a watermark with a long text, the content is cut off and the watermark does not appear in the center of the page.

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;
}
}

sureshkumar.arumugam
 
Posts: 11
Joined: Fri Apr 28, 2023 6:13 am

Mon Sep 25, 2023 9:48 am

Hi,

Thanks for your inquiry.
Regarding your first question, our PDF product currently does not support automatic font size calculation. However, you can manually adjust the font size according to your specific requirements.

Regarding your second question about watermark placement not being centered on the page, we kindly request you to provide us with a more detailed description of the issue. It would be better if you could attach an example image illustrating the desired watermark placement. This will allow us to better understand your requirements and assist you in achieving the desired effect.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.PDF