Reading ffile into memoryStream and creat watermark generates an error


In my application I save PDF documents on DB

when user download PDF we add watermark to the document

here is the relevant part of my code:

Dim PageWidth As Long

Dim PageHeight As Long

Dim mMemoryStream As MemoryStream

Dim fileInfo As PdfFileInfo

Dim inputStream As FileStream

Dim mMemoryStream2 As New MemoryStream(CType(db.ExecuteScalar(CommandType.Text, "select ffile from tbl"), Byte()))

mMemoryStream = mMemoryStream2

fileInfo = New PdfFileInfo(mMemoryStream)

pageWidth = CLng(fileInfo.GetPageWidth(1))

PageHeight = CLng(fileInfo.GetPageHeight(1))

Dim stampedStream As New MemoryStream(CInt(mMemoryStream.Length))

Dim securedStream As New MemoryStream(CInt(mMemoryStream.Length))

On This Code line:

Dim pdfStamp As New PdfFileStamp(mMemoryStream, stampedStream)

I get this error:System.IO.IOException: Invalid pdf format:pdf head signature is not found!

Any help will be appreciated !!

Hi Shlomit,

Have you tried to save the MemoryStream into a PDF file, before adding a stamp? I just want you to make sure that the contents of the actual PDF are getting saved and retrieved from the database with out any error. There might be some information missing while saving or retrieving the PDF contents; from the error, it seems so.

Please check whether the contents save in the database and then retrieved are corrent.


Hi Latif,

Thanks for your response.

There is not any error with the contents of the actual PDF that getting saved and retrieved from the database.

when I'm not trying to set the watermark the pdf display properly from DB into memory stream.

My problem is how to read pdf from DB into memory stream

and then add stamp without any error - I think I miss something in my code



Hi Shlomit,

Can you please save the memory stream as a PDF file, without applying watermark, and share it with us. I would also encourage you to share the code snippet with us, so we could investigate that as well.

We really appreciate your patience and cooperation.


Finally found asolution

I had to add this row:

mMemoryStream.Position = 0

before adding mMemoryStream to stamp