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.

Fri Sep 15, 2023 1:18 pm

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

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

Mon Sep 18, 2023 5:35 am

Hi,

Thanks for your inquiry.
You can refer to the following picture to modify your code:
lQLPJxJqqyyTjRTM9s0CrrDUnyRR40XlfQT7OFrMgE8A_686_246.png

If you have any issue, just feel free to contact us.

Sincerely,
Ula
E-iceblue support team
Last edited by Ula.wang on Wed Sep 20, 2023 7:53 am, edited 1 time in total.
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Tue Sep 19, 2023 7:04 am

The same issue would persist if you update the script as you suggested.

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

Tue Sep 19, 2023 10:11 am

Hi,

Thanks for your feedback.
Please note that the page of your document has a 270 degree rotation. You can obtain the angle of page rotation through the first line circled in the red box in the image. Therefor, to achieve the effect you wanted, you need to rotate the watermark 315 degrees (270+45) counterclockwise.
You can refer to the following picture to modify your code:
27665E7A-4B55-446e-8DFF-EB40AC59B4C9.png


Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Wed Sep 20, 2023 10:01 am

Hi,

Thank you for your more inquiry.
The page rotation angle of the two documents you provided are different with the rotation angle of pdf you provided last time, so you need to set different Angle for the watermarked text.For those pdf files which have all kinds of page rotation angle, if you want to use the same code to achieve the same effect, you can obtain the rotation angle of the page and calculate the watermark rotation angle ,please refer to the following code:
Code: Select all
PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"34577\New folder\New folder\4_PreviewDocument_output.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];
            Console.WriteLine(page.Rotation);
            PdfPageRotateAngle rotateAngle = page.Rotation;
            String Text=rotateAngle.ToString();
            String subString = Text.Substring(11);   
            int number = int.Parse(subString);

            //Draw text watermark
            PdfTilingBrush brush
                = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));
            brush.Graphics.SetTransparency(0.3f);
            brush.Graphics.Save();
            brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
            brush.Graphics.RotateTransform(-(number + 45));
            String text = "20230912\nWelcom Support\nSupport@Welcom";
            brush.Graphics.DrawString(text,
                new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, 0, 0,
                new PdfStringFormat(PdfTextAlignment.Center));
            brush.Graphics.Restore();
            brush.Graphics.SetTransparency(1);
            page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
            //Save pdf file
            doc.SaveToFile(@"E:34577\\New folder\\New folder\\7.pdf");
            doc.Close();


Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Mon Oct 30, 2023 7:42 am

Hi,

I would like to know if you have resolved the issue you have encountered, and our team hope to have more communication with you.If you have any issue just feel free to contact us.Thank you in advance.

Sincerely
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Return to Spire.PDF

cron