Free Support Forum - aspose.com

How to add watermark in 2 lines in asp.net using Aspose.PDF

Hi

i am using Aspose.Total api in .net , i have string ,when passing to watermark i will add existing string with current data and time using this
strText = strText & vbNewLine & System.DateTime.Now

when i pass to this sting to ,i will not get out pdf file,This is my code


Dim textStamp As New TextStamp(strText)
textStamp.RotateAngle = “45”
'set text properties
textStamp.TextState.Font = “Arial”
Dim strSize As String = “12”
textStamp.TextState.FontSize = Convert.ToSingle(strSize)
textStamp.TextState.FontStyle = FontStyles.Bold
textStamp.TextState.FontStyle = FontStyles.Italic
textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red)
textStamp.HorizontalAlignment = HorizontalAlignment.Center
textStamp.VerticalAlignment = VerticalAlignment.Center
textStamp.VerticalAlignment = VerticalAlignment.Bottom
textStamp.Opacity = 0.5
'add stamp to particular page
For Each page As Page In pdfDocument.Pages
page.AddStamp(textStamp)
Next page
pdfDocument.Save(path)

Note: i want my own string and current system datetime in 2 lines in watermark

Pls reply asap

Regards
Aravind

Hi Aravind,


Thanks for contacting support.

In order to display text in two lines i.e with a line break, you need to use FormattedText. This way the formatting of the text will be preserved and it will be displayed with a line break int the generated document. Please check the following code snippet to add TextStamp as watermark inside PDF. I have added highlighted part into your code and also attached the generated output for your reference.

Dim
pdfDocument As New Document()<o:p></o:p>

pdfDocument.Pages.Add()<o:p></o:p>

Dim str
As String =
“This is sample string” + Environment.NewLine + DateTime.Now.ToString()<o:p></o:p>

Dim text
As New Facades.FormattedText(str)<o:p></o:p>

Dim
textStamp As New TextStamp(text)
<o:p></o:p>

textStamp.RotateAngle
= 45<o:p></o:p>

'set
text properties
<o:p></o:p>

textStamp.TextState.Font
=
FontRepository.FindFont(“Arial”)<o:p></o:p>

Dim
strSize As String =
“12”<o:p></o:p>

textStamp.TextState.FontSize
=
Convert.ToSingle(strSize)<o:p></o:p>

textStamp.TextState.FontStyle
=
FontStyles.Bold<o:p></o:p>

textStamp.TextState.FontStyle
=
FontStyles.Italic<o:p></o:p>

textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red)<o:p></o:p>

textStamp.HorizontalAlignment
= HorizontalAlignment.Center<o:p></o:p>

textStamp.VerticalAlignment
= VerticalAlignment.Center<o:p></o:p>

textStamp.VerticalAlignment
= VerticalAlignment.Bottom<o:p></o:p>

textStamp.Opacity
= 0.5<o:p></o:p>

'add
stamp to particular page
<o:p></o:p>

For Each
page As Page In pdfDocument.Pages<o:p></o:p>

page.AddStamp(textStamp)<o:p></o:p>

Next<o:p></o:p>

pdfDocument.Save(dataDir + "Watermark_out.pdf")


In case if you need any further assistance please feel free to let us know.


Best Regards,

Hi

Thank you for quick reply,working fine for text watermark,But we need to for imageStamp,below is my code to add image in watermark,here how i can add system date in second line ,i mean image in first line and system date in second line.

Dim imageStamp As New ImageStamp(“D:/sample.png”)
imageStamp.RotateAngle = “90”
imageStamp.BottomMargin = 10
imageStamp.HorizontalAlignment = HorizontalAlignment.Center
imageStamp.VerticalAlignment = VerticalAlignment.Center
imageStamp.Opacity = 0.5
'add footer on all pages
For Each page As Page In pdfDocument.Pages
page.AddStamp(imageStamp)
Next page
pdfDocument.Save(path)

Regards
Aravind

Hi Aravind,


Thanks for your inquiry.

In order to add text below image stamp you need to add TextStamp and set its position so that it can be displayed under the image. Please check the following code in which I have added ImageStamp and right below the image I have added TextStamp by specifying its position. I have also attached an output file for your reference.

Dim
pdfDocument As New Document()<o:p></o:p>

pdfDocument.Pages.Add()

Dim imageStamp As New ImageStamp(dataDir + "aspose1.png")

imageStamp.RotateAngle = 90

imageStamp.BottomMargin = 10

imageStamp.HorizontalAlignment = HorizontalAlignment.Center

imageStamp.VerticalAlignment = VerticalAlignment.Center

imageStamp.Opacity = 0.5

imageStamp.XIndent = 200

imageStamp.YIndent = 500

Dim str As String = DateTime.Now.ToString()

Dim text As New Facades.FormattedText(str)

Dim textStamp As New TextStamp(text)

'set text properties

textStamp.TextState.Font = FontRepository.FindFont("Arial")

Dim strSize As String = "12"

textStamp.TextState.FontSize = Convert.ToSingle(strSize)

textStamp.TextState.FontStyle = FontStyles.Bold

textStamp.TextState.FontStyle = FontStyles.Italic

textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red)

textStamp.Opacity = 0.5

textStamp.YIndent = 480 - imageStamp.Height

textStamp.XIndent = 230

For Each page As Page In pdfDocument.Pages

page.AddStamp(imageStamp)

page.AddStamp(textStamp)

Next

pdfDocument.Save(dataDir + "Watermark_out.pdf")



In case of any further assistance please feel free to contact us.


Best Regards,

Hi

Thank u for ur reply, from ur code ,if it is image in square shape then no problem and HorizontalAlignment and VerticalAlignment always in center,
But in our case HorizontalAlignment only center and VerticalAlignment may be Top or Center or Bottom and also RotateAngle may be 0 or 45 or 90 or 180 or 360 ,so how find set text below the image, only for VerticalAlignment is bottom then text can move to top of the image.And image dimension like 250*51.

I try with above code with sample image and i will get pdf out not as expected,only i change RotateAngle 90 to 360 .Here i attach out pdf file.

Pls reply asap

Thanks
Aravind

Hi Aravind,


Thanks for writing back. I am looking into the details of the scenario and will get back to you shortly. Please be patient.

Best Regards,

Hi Aravind,


Thanks for your patience. If you please take a look at previous code snippet which I have shared you will see that I was adding two (image and text) stamps inside the document. Please note that I have set the position of the text stamp relative to the position of image stamp.

bpanchu:
But in our case HorizontalAlignment only center and VerticalAlignment may be Top or Center or Bottom and also RotateAngle may be 0 or 45 or 90 or 180 or 360 ,so how find set text below the image, only for VerticalAlignment is bottom then text can move to top of the image.And image dimension like 250*51.

In such case you need to set the position of the text stamp according to the position of the image stamp as per your requirement. Please check the following code snippet which I have used to add stamps (image and text) with the rotation angle of 90. In the below code I have set the position of the text stamp so that it can be displayed right after the image. You may use following approach to add text stamp with the image stamp by specifying its position.

Dim pdfDocument As New Document()<o:p></o:p>

pdfDocument.Pages.Add()

Dim
imageStamp As New ImageStamp(dataDir +
“sample_image.png”)<o:p></o:p>

imageStamp.RotateAngle = 90

imageStamp.BottomMargin = 10

imageStamp.HorizontalAlignment = HorizontalAlignment.Center

imageStamp.VerticalAlignment = VerticalAlignment.Center

imageStamp.Opacity = 0.5

Dim str As String = DateTime.Now.ToString()

Dim text As New Facades.FormattedText(str)

Dim textStamp As New TextStamp(text)

'set text properties

textStamp.TextState.Font = FontRepository.FindFont("Arial")

Dim strSize As String = "12"

textStamp.TextState.FontSize = Convert.ToSingle(strSize)

textStamp.TextState.FontStyle = FontStyles.Bold

textStamp.TextState.FontStyle = FontStyles.Italic

textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red)

textStamp.Opacity = 0.5

textStamp.XIndent = 320

textStamp.VerticalAlignment = VerticalAlignment.Center

textStamp.RotateAngle = 90

For Each page As Page In pdfDocument.Pages

page.AddStamp(imageStamp)

page.AddStamp(textStamp)

Next

pdfDocument.Save(dataDir + "Watermark1_out.pdf")


Moreover I have also attached an output file generated by the above code. In case of any further assistance please feel free to contact us.


Best Regards,

Hi

Thank you for ur reply,my question is

"But in our case HorizontalAlignment only center and VerticalAlignment may be Top or Center or Bottom and also RotateAngle may be 0 or 45 or 90 or 180 or 360 ,so how find set text below the image, only for VerticalAlignment is bottom then text can move to top of the image.And image dimension like 25051. "

Means RotateAngle 45,90,180,360 and VerticalAlignment is Top,Center and Bottom so totally 43 = 12 probability.
Means

if RotateAngle = 45 and VerticalAlignment =top ----- > Xindent or Yindent ???
if RotateAngle = 90 and VerticalAlignment =top ----- > Xindent or Yindent ???
if RotateAngle = 180 and VerticalAlignment =top ----- > Xindent or Yindent ???
if RotateAngle = 360 and VerticalAlignment =top ----- > Xindent or Yindent ???

if RotateAngle = 45 and VerticalAlignment =Center ----- > Xindent or Yindent ???
if RotateAngle = 90 and VerticalAlignment =Center ----- > Xindent or Yindent ???
if RotateAngle = 180 and VerticalAlignment =Center ----- > Xindent or Yindent ???
if RotateAngle = 360 and VerticalAlignment =Center ----- > Xindent or Yindent ???

if RotateAngle = 45 and VerticalAlignment =bottom ----- > Xindent or Yindent ???
if RotateAngle = 90 and VerticalAlignment =bottom ----- > Xindent or Yindent ???
if RotateAngle = 180 and VerticalAlignment =bottom ----- > Xindent or Yindent ???
if RotateAngle = 360 and VerticalAlignment =bottom ----- > Xindent or Yindent ???


Note image size may have different dimensions like 10050 , 50100 , 2525, 9080 etc

Previous code for RotateAngle 90 and it works for Top,center and bottom,what about other RotateAngle and different VerticalAlignment

Here i attach screenshot of all cases,i want to show text below that image.

This is my code

Dim pdfDocument As New Document(“D:\Bugs\Watermark_out.pdf”)
’ Dim pdfDocument As New Document()
pdfDocument.Pages.Add()
Dim imageStamp As New ImageStamp(“D:\Bugs\km-logo.png”)
imageStamp.RotateAngle = 360
imageStamp.BottomMargin = 10
imageStamp.HorizontalAlignment = HorizontalAlignment.Center
imageStamp.VerticalAlignment = VerticalAlignment.Bottom
imageStamp.Opacity = 0.5
'imageStamp.XIndent = 200
'imageStamp.YIndent = 500
Dim str As String = DateTime.Now.ToString()
Dim text As New Facades.FormattedText(str)
Dim textStamp As New TextStamp(text)
'set text properties
textStamp.TextState.Font = FontRepository.FindFont(“Arial”)
Dim strSize As String = “12”
textStamp.TextState.FontSize = Convert.ToSingle(strSize)
textStamp.TextState.FontStyle = FontStyles.Bold
textStamp.TextState.FontStyle = FontStyles.Italic
textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red)
textStamp.Opacity = 0.5
’ textStamp.XIndent = 320
textStamp.XIndent = imageStamp.Height + 280
'textStamp.YIndent = imageStamp.YIndent
'textStamp.XIndent = imageStamp.YIndent
textStamp.VerticalAlignment = VerticalAlignment.Bottom
textStamp.RotateAngle = 360

For Each page As Page In pdfDocument.Pages
page.AddStamp(imageStamp)
page.AddStamp(textStamp)
Next
pdfDocument.Save(“D:\Bugs\Watermark_out.pdf”)

Note: I want space between image and text,for all cases VerticalAlignment is bottom ,then text need to show top of the image.
Regards
Aravind

Hi Aravind,


Thanks for sharing more details.

bpanchu:
Previous code for RotateAngle 90 and it works for Top,center and bottom,what about other RotateAngle and different VerticalAlignment

Please check following code snippets which I have used to display text and image stamps with other rotation angles.

RotateAngle = 45 and VerticalAlignment = top

textStamp.Value
=
“RotateAngle = 45 and VerticalAlignment =top”<o:p></o:p>

textStamp.RotateAngle = 45

imageStamp.RotateAngle = 45

textStamp.VerticalAlignment = VerticalAlignment.Top

imageStamp.VerticalAlignment = VerticalAlignment.Top

imageStamp.HorizontalAlignment = HorizontalAlignment.None

imageStamp.XIndent = 200

textStamp.XIndent = 260


RotateAngle = 45 and VerticalAlignment = Center


textStamp.Value = "RotateAngle = 45 and VerticalAlignment =Center"

textStamp.RotateAngle = 45

imageStamp.RotateAngle = 45

textStamp.VerticalAlignment = VerticalAlignment.Center

imageStamp.VerticalAlignment = VerticalAlignment.Center

imageStamp.HorizontalAlignment = HorizontalAlignment.None

imageStamp.XIndent = 200

textStamp.XIndent = 260


RotateAngle = 45 and VerticalAlignment = Bottom


textStamp.Value = "RotateAngle = 45 and VerticalAlignment =Bottom"

textStamp.RotateAngle = 45

imageStamp.RotateAngle = 45

textStamp.VerticalAlignment = VerticalAlignment.Bottom

imageStamp.VerticalAlignment = VerticalAlignment.Bottom

imageStamp.HorizontalAlignment = HorizontalAlignment.None

imageStamp.XIndent = 200

textStamp.XIndent = 260

textStamp.BottomMargin = imageStamp.Height + 10


RotateAngle = 360 and VerticalAlignment = Top


textStamp.Value = "RotateAngle = 360 and VerticalAlignment =Top"

textStamp.RotateAngle = 360

imageStamp.RotateAngle = 360

textStamp.VerticalAlignment = VerticalAlignment.Top

imageStamp.VerticalAlignment = VerticalAlignment.Top

textStamp.XIndent = 260


RotateAngle = 360 and VerticalAlignment = Center


textStamp.Value = "RotateAngle = 360 and VerticalAlignment =Center"

textStamp.RotateAngle = 360

imageStamp.RotateAngle = 360

textStamp.VerticalAlignment = VerticalAlignment.Center

imageStamp.VerticalAlignment = VerticalAlignment.Center

textStamp.XIndent = 260


RotateAngle = 360 and VerticalAlignment = Bottom


textStamp.Value = "RotateAngle = 360 and VerticalAlignment =Bottom"

textStamp.RotateAngle = 360

imageStamp.RotateAngle = 360

textStamp.VerticalAlignment = VerticalAlignment.Bottom

imageStamp.VerticalAlignment = VerticalAlignment.Bottom

textStamp.XIndent = 260

textStamp.BottomMargin = imageStamp.Height


RotateAngle = 180 and VerticalAlignment = Top


textStamp.Value = "RotateAngle = 180 and VerticalAlignment =Top"

textStamp.RotateAngle = 180

imageStamp.RotateAngle = 180

textStamp.VerticalAlignment = VerticalAlignment.Top

imageStamp.VerticalAlignment = VerticalAlignment.Top

textStamp.XIndent = 260


RotateAngle = 180 and VerticalAlignment = Center


textStamp.Value = "RotateAngle = 180 and VerticalAlignment =Center"

textStamp.RotateAngle = 180

imageStamp.RotateAngle = 180

textStamp.VerticalAlignment = VerticalAlignment.Center

imageStamp.VerticalAlignment = VerticalAlignment.Center

textStamp.XIndent = 260


RotateAngle = 180 and VerticalAlignment = Bottom


textStamp.Value = "RotateAngle = 180 and VerticalAlignment =Bottom"

textStamp.RotateAngle = 180

imageStamp.RotateAngle = 180

textStamp.VerticalAlignment = VerticalAlignment.Bottom

imageStamp.VerticalAlignment = VerticalAlignment.Bottom

textStamp.XIndent = 260

textStamp.BottomMargin = imageStamp.Height


I have also attached an output which is generated by above code snippets for your reference.


bpanchu:

for all cases VerticalAlignment is bottom ,then text need to show top of the image.


You may check in the attached file that I have set the text stamp above the image in such cases where vertical alignment is set to bottom. Moreover I have also used an image with different dimensions for image stamp as you can see in the output file. I hope this will help you achieving the functionality as per your requirement. In case if you still face any issue please feel free to let us know.


Best Regards,

Thanks , All working fine, Thanks a lot…


Regards

Aravind
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000; min-height: 14.0px} span.s1 {font-kerning: none}

Thanks for your feedback. It is good to know that suggested code worked for you.


Please keep using our API and feel free to contact us for any question or concern, we will be more than happy to extend our support.


Best Regards,