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.

Tue Sep 19, 2023 8:30 am

Hello,

I want display a watermark in back- and foreground oft the document. I used PdfTilingBrush, because the watermark has some options to be displayed. I can display the watermark for each page differently.

I don't know how to display the watermark in the foreground of the document. Above the text.

In an old version of Spire.PDF I could use PdfPageBase.DefaultLayerIndex. Now this property is obsolete and removed.

Code: Select all
 PdfPageBase pdfPageBase = pdfDocument.Pages[pageIndex];

 PdfTilingBrush brush =
     new PdfTilingBrush(new SizeF(pdfPageBase.Canvas.ClientSize.Width, pdfPageBase.Canvas.ClientSize.Height));
 var fontColor = new PdfRGBColor(watermarkParameter.FontColor);

 brush.Graphics.SetTransparency(1.0f - watermarkParameter.TransparencyPercent / 100.0f);
 brush.Graphics.Save();
 brush.Graphics.TranslateTransform(GetWight(watermarkParameter.Position, brush.Size.Width), GetHeight(watermarkParameter.Position, brush.Size.Height));
 brush.Graphics.RotateTransform(watermarkParameter.Rotation);
 brush.Graphics.DrawString(watermarkParameter.Text,
     pdfFont, new PdfSolidBrush(fontColor), 0, 0,
     new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));
 brush.Graphics.Restore();

 switch (watermarkParameter.DocumentLevel)
 {
     case DocumentLevel.Background:
         pdfPageBase.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), pdfPageBase.Canvas.ClientSize));
         break;
     case DocumentLevel.Foreground:
         //... draw PdfTilingBrush in foreground
         break;
     default:
         throw new NotImplementedException($"Document level '{watermarkParameter.DocumentLevel}' is not implemented.");
 }



Spire.PDF 9.8.5.1040
C#
Windows
.Net Framework 4.8

chd
 
Posts: 15
Joined: Mon Jul 16, 2018 9:22 am

Wed Sep 20, 2023 7:14 am

Hi,

Thank you for your inquiry.
You can compare the following two lines of code:
Code: Select all
brush.Graphics.SetTransparency(1.0f);

When the value is 1, the watermark will completely appears on the upper layer of the text.
Code: Select all
brush.Graphics.SetTransparency(0.1f);

When the value is less than 1, the watermark will gradually appears in the lower layer of the text.
In addition, I put my complete code below for your reference:
Code: Select all
//Create a pdf document and load file from disk
            PdfDocument doc = new PdfDocument(@"../../data/test.pdf");       

            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Draw text watermark
            PdfTilingBrush brush
                = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));
            brush.Graphics.SetTransparency(1f);
            brush.Graphics.Save();
            brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
            brush.Graphics.RotateTransform(-45);
            brush.Graphics.DrawString("Spire.Pdf Demo",
                new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, 0, 0,
                new PdfStringFormat(PdfTextAlignment.Center));
            brush.Graphics.Restore();
            //brush.Graphics.SetTransparency(0);
            page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));

            doc.SaveToFile(@"../../output/result.pdf", FileFormat.PDF);

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

Sincerely,
Ula
E-iceblue support team
User avatar

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

Tue Sep 26, 2023 12:04 pm

Hi,

thanks for your answer.

But transparency is not that what i'm searching or it does not work.
It is still on the text.

Before "PdfPageBase.DefaultLayerIndex" was removed i used this code to display the rectangle on or under the text:
Code: Select all
switch (watermarkParameter.DocumentLevel)
{
    case DocumentLevel.Background:
        pdfPageBase.DefaultLayerIndex = -1;
        break;
    case DocumentLevel.Foreground:
        pdfPageBase.DefaultLayerIndex = 0;
        break;
    default:
        throw new NotImplementedException($"Document level '{watermarkParameter.DocumentLevel}' is not implemented.");
}

pdfPageBase.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), pdfPageBase.Canvas.ClientSize));


The attachements show how it was displayed in the pdf.
(I used everytime transparency 0.9f)

Now i want the same result without using "PdfPageBase.DefaultLayerIndex".
Showing the watermark in foreground works, but not displaying the watermark in background.

chd
 
Posts: 15
Joined: Mon Jul 16, 2018 9:22 am

Wed Sep 27, 2023 8:39 am

Hi,

Thank you for your more inquiry.
Sorry for the inconvenience caused,the PdfPageBase.DefaultLayerIndex you wanted has been removed from our product and there is currently no new alternative method. If our development team provide a new alternative method in the future, I will inform you promptly.
Now, you can refer to the following complete code to achieve your requirement :
Code: Select all
static void Main(string[] args)
        {
            //Original  document
            PdfDocument doc1 = new PdfDocument();
            doc1.LoadFromFile(@"E:\34614\1.pdf");

            //Original  document as template  document
            PdfTemplate template1 = doc1.Pages[0].CreateTemplate();

            //Create PDF file
            PdfDocument doc = new PdfDocument();
            doc.Pages.Add();

            switch (watermarkParameter.DocumentLevel)
            {
                case DocumentLevel.Background:
                   
                    //Calling the AddWatermark method to add a watermark layer
                    AddWatermark(doc);

                    //Apply template to draw doc1 on doc
                    for (int i = 0; i < doc.Pages.Count; i++)
                    {
                        doc.Pages[i].Canvas.SetTransparency(1.0f, 1.0f, PdfBlendMode.Normal);
                        doc.Pages[i].Canvas.DrawTemplate(template1, new PointF(0, 0));
                    }
                    //Save
                    doc.SaveToFile(@"E:\34614\output7.pdf", FileFormat.PDF);
                    break;
                case DocumentLevel.Foreground:
                    //Calling the AddWatermark method to add a watermark layer
                    AddWatermark(doc1);
                    //Save
                    doc1.SaveToFile(@"E:\34614\output6.pdf", FileFormat.PDF);
                    break;
                default:
                    throw new NotImplementedException($"Document level '{watermarkParameter.DocumentLevel}' is not implemented.");
            }

        }
        private static void AddWatermark(PdfDocument doc)
        {
            //Create a layer called 'Watermark'
            PdfLayer layer = doc.Layers.AddLayer("watermark");

            //Create Font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("TimesRoman", 80f), true);

            //Specify watermark text
            string watermarkText = "oreGer";

            //Get TextSize
            SizeF fontSize = font.MeasureString(watermarkText);

            //Calculate two offsets
            float offset1 = (float)(fontSize.Width * System.Math.Sqrt(2) / 4);
            float offset2 = (float)(fontSize.Height * System.Math.Sqrt(2) / 4);

            //Get Pages
            int pageCount = doc.Pages.Count;

            //Declare two variables
            PdfPageBase page;
            PdfCanvas canvas;

            //Loop through pages
            for (int i = 0; (i < pageCount); i++)
            {
                page = doc.Pages[i];

                //Create a canvas from a layer
                canvas = layer.CreateGraphics(page.Canvas);
                canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2);
                canvas.SetTransparency(1.0f);

                //Draw a string on the canvas of a layer
                canvas.DrawString(watermarkText, font, PdfBrushes.Red, 0, 0);
            }
        }

These are my test screenshots:
foreground.png

background.png

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

Sincerely,
Ula
E-iceblue support team
User avatar

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

Tue Oct 31, 2023 6:54 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 issues, just feel free to contact us. Wishing you a wonderful day.

Sincerely,
Ula
E-commerce Support Team
User avatar

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

Wed Dec 06, 2023 1:10 pm

Hi,

Thanks for help.
Your solution works, but is no good option.

It works good for a document with 1-3 pages, but a document with much more pages is to big.
The performance is very poor. Also, metadata from the document is lost, i. e. I have to copy all of it into the new document.

One way to do that in an existing document would be very important to us.

chd
 
Posts: 15
Joined: Mon Jul 16, 2018 9:22 am

Thu Dec 07, 2023 10:05 am

Hi,

Thank you for your feedback.
For the performance issue, I need some time to communicate with our Dev team to check If there are alternative method to solve it. When I get feedback from our Dev team. I will inform you promptly. We deeply apologize for any inconvenience caused. For your second issue, you can get the metadata of the original document through the following code:
Code: Select all
   
            String input = "..\\..\\..\\..\\..\\..\\Data\\GetXMPMetadata.pdf";
            PdfDocument doc = new PdfDocument();
            // Read a pdf file
            doc.LoadFromFile(input);

            PdfXmpMetadata xmpMetadata = doc.Metadata;

            // Create a StringBuilder object to put the details
            StringBuilder builder = new StringBuilder();
            const string NsPdf = "http://ns.adobe.com/pdf/1.3/";
            if (xmpMetadata.ExistProperty(NsPdf, "Author"))
            {
                builder.AppendLine("Author:" + xmpMetadata.GetPropertyString(NsPdf, "Author"));
            }
            if (xmpMetadata.ExistProperty(NsPdf, "Title"))
            {
                builder.AppendLine("Title: " + xmpMetadata.GetPropertyString(NsPdf, "Title"));
            }
            if (xmpMetadata.ExistProperty(NsPdf, "Subject"))
            {
                builder.AppendLine("Subject: " + xmpMetadata.GetPropertyString(NsPdf, "Subject"));
            }
            if (xmpMetadata.ExistProperty(NsPdf, "Producer"))
            {
                builder.AppendLine("Producer: " + xmpMetadata.GetPropertyString(NsPdf, "Producer"));
            }
            if (xmpMetadata.ExistProperty(NsPdf, "Creator"))
            {
                builder.AppendLine("Creator: " + xmpMetadata.GetPropertyString(NsPdf, "Creator"));
            }
            if (xmpMetadata.ExistProperty(NsPdf, "Keywords"))
            {
                builder.AppendLine("Keywords: " + xmpMetadata.GetPropertyString(NsPdf, "Keywords"));
            }

            String result = "GetXMPMetadata_out.txt";
            File.WriteAllText(result, builder.ToString());
            //Launch the result file
            DocumentViewer(result);

Then refer to the following link to set metadata for result pdf file: https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Document-Operation/Set-XMP-Matedata-of-a-PDF-Document-in-C-VB.NET.html

Sincerely,
Ula
E-iceblue support team
User avatar

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

Fri Dec 08, 2023 8:16 am

Hello,

I'm sorry for the misunderstanding.
I want to keep all the properties from the document and don't want to have to copy everything.
Likewise, for example password, print settings, ...

I will wait for feedback from your DevTeam.
Thanks

chd
 
Posts: 15
Joined: Mon Jul 16, 2018 9:22 am

Fri Dec 08, 2023 10:00 am

Hi,

Thank you for your feedback.
I have recorded this new function about watermark on top and bottom of the source document in our error tracking system, numbered “SPIREPDF-6434”. If this function is implemented, performance and properties issues should be resolved.
Our development team will make every effort to research and achieve it in the future. If this function is completed, we will notify you as soon as possible. We deeply apologize for any inconvenience caused to you.

Sincerely,
Ula
E-iceblue support team
User avatar

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

Thu Feb 13, 2025 1:25 pm

Hello E-commerce Support Team,

Is there any update that could help me?

Thank you.

chd
 
Posts: 15
Joined: Mon Jul 16, 2018 9:22 am

Fri Feb 14, 2025 8:45 am

Hi,

Our dev team has made preliminary adjustments to your issue. However, we have found that the current adjustments may have a significant impact on other scenarios, so the team is working hard to find a more optimized and comprehensive solution to ensure that the problem can be thoroughly resolved while avoiding any impact on other features.
Please give us some more time, we will definitely push forward this matter as soon as possible and update you on the progress in a timely manner. Thank you very much for your understanding.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1290
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF