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 Jul 20, 2021 7:39 pm

Hello,

is it possible to put watermark on separate lines?

profiler007
 
Posts: 73
Joined: Wed Nov 13, 2019 11:32 am

Wed Jul 21, 2021 9:27 am

Hello,

Thanks for your inquiry.
Yes, our Spire.PDF support drawing multi-line watermarks in PDF documents. Please refer to the following code to meet your requirement.
Code: Select all
        public static void Method1()
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"test.pdf");

            PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);

            PdfDocument result = new PdfDocument();
            PdfPageBase page = null;
            foreach (PdfPageBase pdfPageBase in doc.Pages)
            {
                page = result.AppendPage();
                for (int i = 50; i < pdfPageBase.Size.Height; i += 50)
                {
                    page.Canvas.DrawString("999",
                        new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, pdfPageBase.Size.Width / 4, i,
                        new PdfStringFormat(PdfTextAlignment.Center));

                    page.Canvas.DrawString("999",
                        new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, pdfPageBase.Size.Width / 2, i,
                        new PdfStringFormat(PdfTextAlignment.Center));

                    page.Canvas.DrawString("999",
                        new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, pdfPageBase.Size.Width / 4 * 3, i,
                        new PdfStringFormat(PdfTextAlignment.Center));
                    page.Canvas.DrawTemplate(pdfPageBase.CreateTemplate(), new PointF(0, 0));
                }

            }
            result.SaveToFile("TextWaterMark.pdf");
        }

        public static void Method2()
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"test.pdf");

            PdfPageBase page = doc.Pages[0];

            PdfTilingBrush brush
                = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 3, page.Canvas.ClientSize.Height / 5));
            brush.Graphics.SetTransparency(0.3f);
            brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
            brush.Graphics.DrawString("999",
                new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, 0, 0,
                new PdfStringFormat(PdfTextAlignment.Center));
            page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));

            doc.SaveToFile("TextWaterMark.pdf");
        }


If this cannot help you, please provide your input file and your desired output for further investigation. You can send them to us ([email protected]) via email. Thanks in advance for your assistance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Jul 21, 2021 10:09 am

Hello, Thanks for the quick reply!

The code you suggested me doing something different. I want to place something like the following 3 (multiple) lines one below the other:

Contact Full name
Company name
[email protected]

The lines have to appear only once in a page.

Regards

profiler007
 
Posts: 73
Joined: Wed Nov 13, 2019 11:32 am

Thu Jul 22, 2021 1:55 am

Hello,

Thanks for your feedback.
Please refer to the following modified code.
Code: Select all
        public static void Method1()
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"test.pdf");

            PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);

            PdfDocument result = new PdfDocument();
            PdfPageBase page = null;
            foreach (PdfPageBase pdfPageBase in doc.Pages)
            {
                page = result.AppendPage();
                page.Canvas.DrawString("Contact Full name",
                    new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, pdfPageBase.Size.Width / 2, pdfPageBase.Size.Height/4,
                    new PdfStringFormat(PdfTextAlignment.Center));

                page.Canvas.DrawString("Company name",
                    new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, pdfPageBase.Size.Width / 2, pdfPageBase.Size.Height/2,
                    new PdfStringFormat(PdfTextAlignment.Center));

                page.Canvas.DrawString("[email protected]",
                    new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, pdfPageBase.Size.Width / 2, pdfPageBase.Size.Height/4*3,
                    new PdfStringFormat(PdfTextAlignment.Center));
                page.Canvas.DrawTemplate(pdfPageBase.CreateTemplate(), new PointF(0, 0));

            }
            result.SaveToFile("TextWaterMark.pdf");
        }

        public static void Method2()
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"test.pdf");

            foreach(PdfPageBase page in doc.Pages)
            {
                PdfTilingBrush brush
                = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height));
                brush.Graphics.SetTransparency(0.3f);
                brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
                PdfFont pdfFont = new PdfFont(PdfFontFamily.Helvetica, 24);
                float height = pdfFont.MeasureString("Contact Full name").Height;
                brush.Graphics.DrawString("Contact Full name", pdfFont, PdfBrushes.Violet, 0, 0,
                    new PdfStringFormat(PdfTextAlignment.Center));
                brush.Graphics.DrawString("Company name", pdfFont, PdfBrushes.Violet, 0, height,
                    new PdfStringFormat(PdfTextAlignment.Center));
                brush.Graphics.DrawString("[email protected]", pdfFont, PdfBrushes.Violet, 0, height*2,
                    new PdfStringFormat(PdfTextAlignment.Center));

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

            doc.SaveToFile("TextWaterMark.pdf");
        }

If this cannot help you, please provide your input file and your desired output for further investigation. You can send them to us ([email protected]) via email. Thanks in advance for your assistance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Thu Jul 22, 2021 7:08 am

It works fine, thanks!

profiler007
 
Posts: 73
Joined: Wed Nov 13, 2019 11:32 am

Thu Jul 22, 2021 8:46 am

You are welcome.
If you encounter any issues related to our products in the future, please feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF

cron