How to add duplicated text watermarks on pdf

Hello,

I want to add multiple text watermarks on pdf, how to fullfil this?
Requirement:

  1. text will rotate 45degree
  2. watermark will be duplicated acc. to pdf page width and height
  3. watermark should be transparent and on the top of the pdf page
  4. original pdf file is converted from pptx, so the text in the pdf can be selected. After adding watermarks and export new pdf, the text on the new pdf cannot be selected.

Watermark style is like attachment.
clipboard-202312251330-m9sbt.png (37.9 KB)

Thank you!

@msdos41

With Aspose.PDF, you can off course achieve all of these requirements. Please go through the below documentation page that has enough information and code snippets to add text stamps with transparency, rotation, multiline as well as styling.

For your last point though, it looks like you want the complete PDF to be an image so that no text can be selected. Right? If so, you need to convert all PDF pages into images after adding the stamps and then generate a new PDF from obtained images. Please check the below articles:

@asad.ali

Hello, my current codes as follows.

I rotate the stamp and want to iterate to add to one page in different position.

question1: when I rotate the stamp, which anchor point of the stamp to decide the xdent and ydent? I thought the left top corner of the stamp before, but when i tested to put (0,0) and rotate 45deg, the position was not what I thought.

question2: Due to the dynamic input of the stamp contents, the stamp width and height is also different. So when I iterated the stamps, I wanted to decide the position acc. to the current stamp width and height. But in my code, after setting all the attributes of the stamp, the values of width and height of stamp are still 0. How to deal with it?

You could find what i wanted to fullfil the watermarks in my first post attachment.

Thanks!

        public byte[] AddWatermarksOnPdf(InputFileInfoExpDto input)
        {
            using (var pdfOrigin = new Aspose.Pdf.Document(input.FilePath))
            using (var ms = new MemoryStream())
            {
                Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})");
                //Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})",
                //                                                                                      new Aspose.Pdf.Facades.FontColor(128,128,128),
                //                                                                                      Aspose.Pdf.Facades.FontStyle.Unknown,
                //                                                                                      Aspose.Pdf.Facades.EncodingType.Identity_h,
                //                                                                                      true,
                //                                                                                      20,
                //                                                                                      5);
                formattedText.AddNewLineText(input.Email, 10);
                formattedText.AddNewLineText(input.DownloadTime.ToString("yyyy-MM-dd HH:mm:ss"), 10);
                Aspose.Pdf.TextStamp stamp = new Aspose.Pdf.TextStamp(formattedText);
                //stamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
                //stamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
                //stamp.TextAlignment = Aspose.Pdf.HorizontalAlignment.Left;
                stamp.XIndent = 0;
                stamp.YIndent = 0;
                stamp.RotateAngle = 45;
                stamp.Draw = true;
                stamp.Background = false;
                stamp.Opacity = 0.6;
                //stamp.Width = 180;
                //stamp.Height = 180;
                stamp.TextState.FontSize = 20;
                stamp.TextState.ForegroundColor = Aspose.Pdf.Color.Gray;

                double xGap = 10;
                double yGap = 10;
                foreach (var page in pdfOrigin.Pages)
                {
                    var width = page.MediaBox.Width;
                    var height = page.MediaBox.Height;

                    int horizontCount = (int)(width / (stamp.Width + xGap)) + 1;
                    int verticalCount = (int)(height / (stamp.Height + yGap)) + 1;

                    for (int i = 0;  i < horizontCount;  i++)
                    {
                        for (int j = 0; j < verticalCount; j++)
                        {
                            stamp.XIndent = i * (stamp.Width + xGap);
                            stamp.YIndent = j * (stamp.Height + yGap);
                            page.AddStamp(stamp);
                        }
                    }
                }

                pdfOrigin.Save(ms);

                return ms.ToArray();
            }
        }

@msdos41

The (0,0) in Aspose.PDF means bottom-left. Also, the unit of the measurement is point where 72 points = 1 inch. You can adopt coordinate calculations with this specification. Furthermore, the text is rotated on its center point. We have written a sample code example that adds text stamps without using coordinates. It only consumes the alignments. Output is also added that looks similar to what you shared in the first post.

Document doc = new Document();
Page page = doc.Pages.Add();

Facades.FormattedText stamptext = new Facades.FormattedText("Aspose");
stamptext.AddNewLineText("Pty");
stamptext.AddNewLineText("Ltd");

TextStamp topleftstamp = new TextStamp(stamptext);

TextStamp bottomleft = new TextStamp(stamptext);
bottomleft.HorizontalAlignment = HorizontalAlignment.Left;
bottomleft.VerticalAlignment = VerticalAlignment.Bottom;
bottomleft.RotateAngle = 45;

TextStamp bottomcenter = new TextStamp(stamptext);
bottomcenter.HorizontalAlignment = HorizontalAlignment.Center;
bottomcenter.VerticalAlignment = VerticalAlignment.Bottom;
bottomcenter.RotateAngle = 45;

TextStamp bottomright = new TextStamp(stamptext);
bottomright.HorizontalAlignment = HorizontalAlignment.Right;
bottomright.VerticalAlignment = VerticalAlignment.Bottom;
bottomright.RotateAngle = 45;

TextStamp middleright = new TextStamp(stamptext);
middleright.HorizontalAlignment = HorizontalAlignment.Right;
middleright.VerticalAlignment = VerticalAlignment.Center;
middleright.RotateAngle = 45;

TextStamp middlecenter = new TextStamp(stamptext);
middlecenter.HorizontalAlignment = HorizontalAlignment.Center;
middlecenter.VerticalAlignment = VerticalAlignment.Center;
middlecenter.RotateAngle = 45;

TextStamp middleleft = new TextStamp(stamptext);
middleleft.HorizontalAlignment = HorizontalAlignment.Left;
middleleft.VerticalAlignment = VerticalAlignment.Center;
middleleft.RotateAngle = 45;

page.AddStamp(bottomcenter);
page.AddStamp(bottomleft);
page.AddStamp(bottomright);
page.AddStamp(middleleft);
page.AddStamp(middlecenter);
page.AddStamp(middleright);

doc.Save(dataDir + "stamps.pdf");

stamps.pdf (5.7 KB)

You can further modify this code in order to achieve custom formatting and add top row of the text stamp on a page as well. Please share your feedback with us about the given suggestion and we will further proceed accordingly.

@asad.ali

Hello, the code you provided could add 9 stamps at most, right?

But what i need is to put the stamps as much as possible acc. to the stamps size, page size and the gap between stamps I set. Just like I provided the code in my second post. The number of stamps are dynamic.

@msdos41

We need to perform analysis against your particular requirements. For the purpose, a ticket as PDFNET-56225 in our issue tracking system has been logged. We will investigate it and let you know as soon as it is resolved. Please spare us some time.

@asad.ali

Thank you. I have solved this in my own way. I have tested various pdf file, currently works fine.

Furthermore, I want to transfer each page to image and convert back to pdf in case anyone could select the content in pdf. I have checked what you shared in the previous post, and which image format you recommend most? I need relatively good quality and transfer in fast speed.

        public byte[] AddWatermarksOnPdf(InputFileInfoExpDto input)
        {
            using (var pdfOrigin = new Aspose.Pdf.Document(input.FilePath))
            using (var ms = new MemoryStream())
            {
                int fontSize = 20;
                int lineSpacing = 10;
                double rotateAngle = Math.PI / 4;
                //Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})");
                Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})",
                                                                                                      new Aspose.Pdf.Facades.FontColor(128, 128, 128),
                                                                                                      Aspose.Pdf.Facades.FontStyle.Unknown,
                                                                                                      Aspose.Pdf.Facades.EncodingType.Identity_h,
                                                                                                      true,
                                                                                                      fontSize,
                                                                                                      lineSpacing);
                formattedText.AddNewLineText(input.Email, 10);
                formattedText.AddNewLineText(input.DownloadTime.ToString("yyyy-MM-dd HH:mm:ss"), 10);
                Aspose.Pdf.TextStamp stamp = new Aspose.Pdf.TextStamp(formattedText);
                //stamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
                //stamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
                //stamp.TextAlignment = Aspose.Pdf.HorizontalAlignment.Left;
                stamp.XIndent = 0;
                stamp.YIndent = 0;
                stamp.RotateAngle = 45;
                stamp.Draw = true;
                stamp.Background = false;
                stamp.Opacity = 0.6;
                //stamp.Width = 180;
                //stamp.Height = 180;
                //stamp.TextState.FontSize = 20;
                //stamp.TextState.ForegroundColor = Aspose.Pdf.Color.Gray;

                Aspose.Pdf.Facades.FormattedText formattedText1 = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})",
                                                                                      new Aspose.Pdf.Facades.FontColor(128, 128, 128),
                                                                                      Aspose.Pdf.Facades.FontStyle.Unknown,
                                                                                      Aspose.Pdf.Facades.EncodingType.Identity_h,
                                                                                      true,
                                                                                      fontSize,
                                                                                      lineSpacing);
                Aspose.Pdf.Facades.FormattedText formattedText2 = new Aspose.Pdf.Facades.FormattedText($"{input.Email}",
                                                                      new Aspose.Pdf.Facades.FontColor(128, 128, 128),
                                                                      Aspose.Pdf.Facades.FontStyle.Unknown,
                                                                      Aspose.Pdf.Facades.EncodingType.Identity_h,
                                                                      true,
                                                                      fontSize,
                                                                      lineSpacing);
                Aspose.Pdf.Facades.FormattedText formattedText3 = new Aspose.Pdf.Facades.FormattedText($"{input.DownloadTime.ToString("yyyy-MM-dd HH:mm:ss")}",
                                                                      new Aspose.Pdf.Facades.FontColor(128, 128, 128),
                                                                      Aspose.Pdf.Facades.FontStyle.Unknown,
                                                                      Aspose.Pdf.Facades.EncodingType.Identity_h,
                                                                      true,
                                                                      fontSize,
                                                                      lineSpacing);

                var points = new List<PointF>();
                points.Add(new PointF(0, formattedText2.TextHeight + formattedText3.TextHeight + 2 * lineSpacing));
                points.Add(new PointF(formattedText1.TextWidth, formattedText2.TextHeight + formattedText3.TextHeight + 2 * lineSpacing));
                points.Add(new PointF(formattedText1.TextWidth, formattedText2.TextHeight + formattedText3.TextHeight + 2 * lineSpacing + formattedText1.TextHeight));
                points.Add(new PointF(0, formattedText2.TextHeight + formattedText3.TextHeight + 2 * lineSpacing + formattedText1.TextHeight));

                points.Add(new PointF(0, formattedText3.TextHeight + lineSpacing));
                points.Add(new PointF(formattedText2.TextWidth, formattedText3.TextHeight + lineSpacing));
                points.Add(new PointF(formattedText2.TextWidth, formattedText3.TextHeight + lineSpacing + formattedText2.TextHeight));
                points.Add(new PointF(0,formattedText3.TextHeight + lineSpacing + formattedText2.TextHeight));

                points.Add(new PointF(0, 0));
                points.Add(new PointF(formattedText3.TextWidth, 0));
                points.Add(new PointF(formattedText3.TextWidth, formattedText3.TextHeight));
                points.Add(new PointF(0, formattedText3.TextHeight));

                var rotatePoints = new List<PointF>();
                foreach (var point in points)
                {
                    var rotatePoint = RotatePoint(point, rotateAngle);
                    rotatePoints.Add(rotatePoint);
                }
                var rectBox = new RectBoundingBox(rotatePoints);

                double stampWidth = rectBox.Width;
                double stampHeight = rectBox.Height;
                double xGap = 10;
                double yGap = 10;

                foreach (var page in pdfOrigin.Pages)
                {
                    var width = page.MediaBox.Width;
                    var height = page.MediaBox.Height;

                    int horizonCount = (int)(width / (stampWidth + xGap)) + 1;
                    int verticalCount = (int)(height / (stampHeight + yGap)) + 1;

                    for (int i = 0; i < horizonCount; i++)
                    {
                        for (int j = 0; j < verticalCount; j++)
                        {
                            stamp.XIndent = i * (stampWidth + xGap);
                            stamp.YIndent = j * (stampHeight + yGap);
                            page.AddStamp(stamp);
                        }
                    }
                }

                pdfOrigin.Save(ms);

                return ms.ToArray();
            }
        }

@msdos41

It is nice to know that you were able to sort your requirements out. Furthermore, you can generate any type of images e.g. JPG, PNG or TIFF and use them to generate scanned PDF. Every image format has its own effects on speed and performance depending upon their quality. We cannot recommend any image format at this moment as it totally depends upon your requirements. You can try every format and calculate performance metrics to select the best that suits you. In case you have further concerns, please let us know.

@asad.ali

I want to add chinese character in the stamp. My codes follows.

Current question is the first char and last char of chinese string is becoming 口 in the final stamp. The middle 2 char is normal. How to update this?

You could see the error in the attachment
Capture.PNG (2.2 KB)

                int fontSize = 16;
                int lineSpacing = 10;
                double rotateAngle = Math.PI / 4;
                string company = "电气系统";
                var fontColor = new Aspose.Pdf.Facades.FontColor(128, 128, 128);
                var fontStyle = Aspose.Pdf.Facades.FontStyle.CjkFont;
                var encodingType = Aspose.Pdf.Facades.EncodingType.Winansi;
                bool embedded = true;
                //Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})");
                Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})",
                                                                                                      fontColor,
                                                                                                      fontStyle,
                                                                                                      encodingType,
                                                                                                      embedded,
                                                                                                      fontSize,
                                                                                                      lineSpacing);
                //formattedText.AddNewLineText($"{input.DisplayName} ({input.NetId})", 10);
                formattedText.AddNewLineText(input.Email, 10);
                formattedText.AddNewLineText(input.DownloadTime.ToString("yyyy-MM-dd HH:mm:ss"), 10);
                formattedText.AddNewLineText(company, 10);
                Aspose.Pdf.TextStamp stamp = new Aspose.Pdf.TextStamp(formattedText);
                //stamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
                //stamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
                //stamp.TextAlignment = Aspose.Pdf.HorizontalAlignment.Left;
                stamp.XIndent = 0;
                stamp.YIndent = 0;
                stamp.RotateAngle = 45;
                stamp.Draw = true;
                stamp.Background = false;
                stamp.Opacity = 1;

@msdos41

Would you please make sure that Arial Unicode MS font is installed in your system or the font that can support both English and Chinese Characters? In case issue still persists, please share the output file generated at your end. We will further proceed to assist you accordingly.