Aspose.Pdf

Customizing Watermark

A watermark is usually a design (which can be in the form of an image or some text) embossed into the page. Mostly, watermarks are used for the identification of documents but these can also be used for other purposes. Watermarks are generally translucent designs, which means that these designs get more visible when held up to the light. Companies also use their logos on the company's official documents as watermarks.

 

Aspose.Pdf possesses the capability to add watermarks on the pages of PDF documents. Developers can use FloatingBox as a watermark on the pages of their PDF documents. Only Image ,Graph and Text allowed in FloatingBox are added on each page as watermark. Simply follow the steps below to add a watermark:

 

 

In the example below, we have demonstrated the use of Aspose.Pdf to add a Image Graph and an Text as watermarks.

 

Code Snippet

 

[C#]

 

 

Pdf pdf1 = new Pdf();                                                

Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

Aspose.Pdf.Text text1 = new Text(sec1,"This is text in section1.");

text1.Left = 30;

text1.Top = 100;

sec1.Paragraphs.Add(text1);

Aspose.Pdf.Section sec2 = pdf1.Sections.Add();                                               

Aspose.Pdf.Text text2 = new Text(sec2,"This is text in section2.");

text2.Left = 30;

text2.Top = 100;

sec2.Paragraphs.Add(text2);

 

 

//setting image watermark

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();                                                        

image1.ImageInfo.File = @"C:\world.jpeg";

image1.ImageInfo.ImageFileType = ImageFileType.Jpeg;

image1.ImageScale = 0.1f;       

Aspose.Pdf.FloatingBox watermark1 = new Aspose.Pdf.FloatingBox(108,80);

watermark1.BoxHorizontalPositioning = BoxHorizontalPositioningType.Page;

watermark1.BoxHorizontalAlignment = BoxHorizontalAlignmentType.Center;

watermark1.BoxVerticalPositioning = BoxVerticalPositioningType.Page;

watermark1.BoxVerticalAlignment = BoxVerticalAlignmentType.Center;

watermark1.Paragraphs.Add(image1);

 

 

//graph watermark

Graph graph1 = new Graph(100,400);                                       

float[] posArr = new float[]{0,0,200,80,300,40,350,90};

Curve curve1 = new Curve(graph1,posArr);

graph1.Shapes.Add(curve1);

Aspose.Pdf.FloatingBox watermark2 = new Aspose.Pdf.FloatingBox(108,80);

watermark2.Paragraphs.Add(graph1);

 

 

//text watermark

Text text3 = new Text("Text Watermark");               

Aspose.Pdf.FloatingBox watermark3 = new Aspose.Pdf.FloatingBox(108,80);

watermark3.Left = 50;

watermark3.Top = 500;

watermark3.Paragraphs.Add(text3);

 

 

pdf1.Watermarks.Add(watermark1);

pdf1.Watermarks.Add(watermark2);

pdf1.Watermarks.Add(watermark3);                       

pdf1.Save(@"C:\WatermarkExample.pdf");

 

 

[VB.NET]

 

 

Dim pdf1 As Pdf =  New Pdf()

Dim sec1 As Aspose.Pdf.Section =  pdf1.Sections.Add()

Dim text1 As Aspose.Pdf.Text =  New Text(sec1,"This is text in section1.")

text1.Left = 30

text1.Top = 100

sec1.Paragraphs.Add(text1)

Dim sec2 As Aspose.Pdf.Section =  pdf1.Sections.Add()

Dim text2 As Aspose.Pdf.Text =  New Text(sec2,"This is text in section2.")

text2.Left = 30

text2.Top = 100

sec2.Paragraphs.Add(text2)

 

 

'setting a image watermark

Dim image1 As Aspose.Pdf.Image =  New Aspose.Pdf.Image()

image1.ImageInfo.File = "world.jpg"

image1.ImageInfo.ImageFileType = ImageFileType.Jpeg

image1.ImageScale = 0.1f       

Dim watermark1 As FloatingBox =  New FloatingBox()

watermark1.BoxHorizontalPositioning = BoxHorizontalPositioningType.Page

watermark1.BoxHorizontalAlignment = BoxHorizontalAlignmentType.Center

watermark1.BoxVerticalPositioning = BoxVerticalPositioningType.Page

watermark1.BoxVerticalAlignment = BoxVerticalAlignmentType.Center

watermark1.Paragraphs.Add(image1)

 

 

'setting graph watermark

Dim graph1 As Graph =  New Graph(100,400)

Dim posArr() As single =  New single() {0,0,200,80,300,40,350,90}

Dim curve1 As Curve =  New Curve(graph1,posArr)

graph1.Shapes.Add(curve1)

Dim watermark2 As FloatingBox =  New FloatingBox()

watermark2.Paragraphs.Add(graph1)

 

 

'setting a text watermark

Dim text3 As Text =  New Text("Text Watermark")

Dim watermark3 As FloatingBox =  New FloatingBox()

watermark3.Left = 50

watermark3.Top = 500

watermark3.Paragraphs.Add(text3)

 

 

pdf1.Watermarks.Add(watermark1)

pdf1.Watermarks.Add(watermark2)

pdf1.Watermarks.Add(watermark3)                       

pdf1.Save("WatermarkExample.pdf")

 

[XML]

 

 

<Pdf xmlns="Aspose.Pdf" >

    <Section ID="section1">

       <Text Left="30" Top="100">

             <Segment>This is text in section1.</Segment>

       </Text>

    </Section>

    <Section ID="section2">

       <Text Left="30" Top="100">

             <Segment>"This is text in section2."</Segment>

       </Text>

    </Section>

    <Watermark BoxHorizontalPositioning="Page" BoxHorizontalAlignment="Center" BoxVerticalPositioning="Page" BoxVerticalAlignment="Center">

       <Image File="world.jpg" Type="jpeg" ImageScale="0.1" />

    </Watermark>

    <Watermark>

       <Graph  Width="400" Height="100">

        <Curve Position="0 0 200 80 300 40 350 90" />

       </Graph>

    </Watermark>

    <Watermark Left ="50" Top="500">

       <Text >

         <Segment  IsUnicode="true" FontSize="12">Text Watermark</Segment>      

       </Text>

    </Watermark>

  </Pdf>