Free Support Forum - aspose.com

Create Watermark

Hi,

I am trying to create a watermark before displaying the pdf in IE. SQL Server reporting services currently gives me a byte array and I use Response.BinaryWrite to open the result as a pdf in the browser. Before I do Response.BinaryWrite I want to modify the byte array so it has a watermark and then display it. Can someone give me sample code to do this?

Any help is greatly appreciated.

Aarti

Hi,

Thank you for considering Aspose.

Are you getting Pdf stream from SQL Server Reporting Services, if yes then just get that byte Array and convert it to Memory Stream and then by using Aspose.Pdf.Kit you can add Watermark. After then you need to send that OutStream to Web Page. Please refer to following code to add Watermark.

[C#]

PdfFileStamp stamp = new PdfFileStamp(innerStream,outputStream);
stamp.AddWatermark(imageFile,10,10);
outputStream.Close();
innerStream.Close();

Thanks.

Thank you for the response. Yes my Pdf stream is from Reporting Services. I get it as a byte array and converted it to a stream and did what you suggested. However the outputstream is always empty. What am I doing wrong?

Public Function Test(ByVal arrBytes() As Byte) As Byte()

Dim msInputStream As MemoryStream = New MemoryStream(arrBytes, True)

Dim msOutputStream As MemoryStream = New MemoryStream()

Try

Dim stamper As PdfFileStamp = New PdfFileStamp(msInputStream, msOutputStream)

Dim stamp As Stamp = New Stamp()

stamp.BindLogo(New FormattedText("This is a test"))

stamp.IsBackground = False

stamp.Rotation = 30

stamp.SetOrigin(100, 100)

stamper.AddStamp(stamp)

arrBytes = GetStreamAsByteArray(msOutputStream)

msInputStream.Close()

msOutputStream.Close()

stamper.Close()

Catch ex As Exception

Console.WriteLine(ex.Message)

End Try

Return arrBytes

End Function

Private Function GetStreamAsByteArray(ByVal stream As System.IO.Stream) As Byte()

Dim streamLength As Integer = Convert.ToInt32(stream.Length)

Dim fileData As Byte() = New Byte(streamLength) {}

' Read the file into a byte array

stream.Read(fileData, 0, streamLength)

stream.Close()

Return fileData

End Function

End Class

I also dont see a method called AddWatermark in the PdfFileStamp class.

Hi,

I am sorry for my mistake, it should have to be AddStamp(). About the other problem, please first try to save that Byte Array in some file and give it’s Extension “pdf” and try to open that file manually with Adobe Reader. If it is opened fine then it means that the stream is of pdf stream type and send us that pdf file so that we should investigate this issue. But if the file after saving on the disk can’t be open in Adobe Reader then you can’t stamped that stream using Aspose.Pdf.Kit because the byteArray wasn’t of type Pdf. So, please check it and let us know about it.

Thanks.

Did you get this working? I have the same problem. FileStream is no use, it needs to be MemoryStream, but the result is always an empty stream.

Chandy

Hi,

Please send us the pdf file that you are trying to stamp as well as the code that you are using so that we can check this out. Please ensure that you are using the latest version.

Thanks.

I am trying to use an evaluation copy to test watermark creation and I am facing the same issue. I have created a pdf and saved it to a memory stream. When I render it to the browser (or save it as a file) it works. But if I try to add a watermark to the memorystream, it fails. Can you please let me know if you got a solution for this?

Hi,

Can you please provide us with the code you are using as well as the PDF file so that we can test it.

Thanks.

Here is the sample code I tried. I have been able to adda text watermark using FloatingBox. The FileInfo and PdfFileStamp object I have used are giving me errors with the memorystream.

Code is in the attached file.

Thanks

Hi,

Please call the following statement before calling the PdfFileStamp and PdfFileInfo constructor:

ss.Seek(0, SeekOrigin.Begin);

Thanks.