C# – Add Image Watermark in PDF Document

We often find watermark in electronic documents online. What is watermark? It can be text or image which can be embedded in one document. It is translucent and will not influence original contents.

Watermark can indicate who owns the document and protect owners’ copyright. For some company materials, people may add text watermark, such as company name or important, secret to show its properties. In this post, I will show one method to add watermark in a PDF document. And I use the component Spire.PDF to help me realize this function.

The watermark I will add in PDF is an image. Firstly, I should add references in my project, including Spire.PDF dll file, system.drawing. Then, the following steps show some details.

  1. After getting references, create a blank PDF document and set margin for it.

  2. Create a page for adding contents. Then, load image which I want to use as watermark.

  3. Draw page. There is a table with data which will be drawn.

            a. Add title and set title format, including title name, format and position.

b. Add data and set table style, including header, borders and font settings.

c. Add note to show where data is from and set format.

  4. Save and launch file.

Coding:

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

using Spire.Pdf.Tables;

namespace ImageWatermark

{

    class Watermark

    {

        static void Main(string[] args)

        {

            //Create PDF

            PdfDocument doc = new PdfDocument();

 

            //Set Margin

            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

            PdfMargins margin = new PdfMargins();

            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

            margin.Bottom = margin.Top;

            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

            margin.Right = margin.Left;

 

            //Create Page

            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);

            Image img = Image.FromFile(@"D:\work\My Documents\F.jpg");

            page.BackgroundImage = img;

 

            //Draw Page

            DrawPage(page);

 

            //Save adn Launch File

            doc.SaveToFile("ImageWaterMark.pdf");

            doc.Close();

            System.Diagnostics.Process.Start("ImageWaterMark.pdf");

        }

 

        static void DrawPage(PdfPageBase page)

        {

            float y = 10;

 

            //Title

            PdfBrush brush1 = PdfBrushes.DarkCyan;

            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Calibri", 14f, FontStyle.Regular));

            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);

            page.Canvas.DrawString("Sales Information/Year", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);

            y = y + font1.MeasureString("Sales Information/Year", format1).Height;

            y = y + 5;

 

            //Data

            String[][] data

                = {

                    new String[]{"Category Name", "1994 Sale Amount", "1995 Sale Amount", "1996 Sale Amount"},

                    new String[]{"Beverages", "38,487.20", "102,479.46", "126,901.53"},

                    new String[]{"Condiments", "16,402.95", "51,041.83", "38,602.31"},

                    new String[]{"Confections", "23,812.90", "79,752.25", "63,792.07"},

                    new String[]{"Dairy Products", "30,027.79", "116,495.45", "87,984.05"},

                    new String[]{"Grains/Cereals", "7,313.92", "53,823.48", "34,607.19"},

                    new String[]{"Meat/Poultry", "19,856.86", "77,164.75", "66,000.75"},

                    new String[]{"Produce", "10,694.96", "45,973.69", "43,315.93"},

                    new String[]{"Seafood", "16,247.77", "64,195.51", "50,818.46"}

                };

 

            PdfTable table = new PdfTable();

            table.Style.CellPadding = 2;

            table.Style.BorderPen = new PdfPen(brush1, 0.55f);

            table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.WhiteSmoke;

            table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Calibri", 11f));

            table.Style.HeaderSource = PdfHeaderSource.Rows;

            table.Style.HeaderRowCount = 1;

            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.LightYellow;

            table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Calibri", 12f, FontStyle.Regular));

            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);

            table.Style.ShowHeader = true;

            table.DataSource = data;

 

            PdfLayoutResult result = table.Draw(page, new PointF(0, y));

            y = y + result.Bounds.Height + 5;

 

            //Note

            PdfBrush brush2 = PdfBrushes.Red;

            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("TimesNewRoman", 10f));

            page.Canvas.DrawString("* All data from NorthWind", font2, brush2, 5, y);

        }

    }

}

Result:

____________________________________________________________________________________________

Click Here to learn more about Spire.PDF

Click Here to download Spire.PDF

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

1 thought on “C# – Add Image Watermark in PDF Document”

  1. Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s