Problem saving PDF doc to SQL DB Image column


#1

Hello,

Can someone point me in the right direction. I'm trying to save a PDF document to a MS Sql database. I can accomplish this if I save the document to the hard drive first, read the file BACK into memory, save to the DB. Then I can read from DB and save back to the hard drive. Everything great. But I would really like to skip the step of saving to hard drive first. However when I use a Memory Steam "pdfdoc.save(memorystream)" I get a byte array that is larger than the saved document and when I write it straight to disk it is garbage, so there is something I'm messing up somewhere. Here is a code snippet (let me know if there is anything obvious):

Dim mStream_ As New IO.MemoryStream()

'This is a test document about 81KB in size.
_ioPDFDoc_.Save(mStream_)

Dim lByteArr_ As Byte()
lByteArr_ = mStream_.GetBuffer

'NOTE: I'm getting back 214K back from the memorystream????? I'm not expert but would expect about 81K

'update db
loCmd_ = New SqlCommand
loCmd_.Connection = loCxn_
loCmd_.CommandTimeout = 180 'waits 3 minutes before timing out with error
loCmd_.CommandType = CommandType.Text
loCmd_.CommandText = "Update dbo.cs_ntis set Statements_pdf = @Statements_pdf where id = @id "

loCmd_.Parameters.Add("@id", SqlDbType.Decimal)
loCmd_.Parameters("@id").Value = adCsNtisID_

Dim P_ As New SqlParameter("@statements_pdf", SqlDbType.Binary, lByteArr_.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, lByteArr_)
loCmd_.Parameters.Add(P_)

loCmd_.ExecuteNonQuery()


'Test the blob update
loCmd_ = New SqlCommand
loCmd_.Connection = loCxn_
loCmd_.CommandTimeout = 180 'waits 3 minutes before timing out with error
loCmd_.CommandType = CommandType.Text
loCmd_.CommandText = "select Statements_pdf from dbo.cs_ntis where id = @id "
loCmd_.Parameters.Add("@id", SqlDbType.Decimal)
loCmd_.Parameters("@id").Value = adCsNtisID_

ldr_ = loCmd_.ExecuteReader
ldr_.Read()
Dim b2_(CInt(ldr_.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1)) As Byte
ldr_.GetBytes(0, 0, b2_, 0, b2_.Length)
Dim lfs_ As New System.IO.FileStream("C:\temp\NtisFromDb.pdf", IO.FileMode.Create, IO.FileAccess.Write)
lfs_.Write(b2_, 0, b2_.Length)
lfs_.Close()

'When I look at the file on the HD the file won't open it is corrupted. If I just use the pdfdoc.save("C:\temp\ntis.pdf") the document is perfect.

Any help appreciated,

James.


#2

Dear James,

I tried to save the pdf into MemoryStream and get the byte array and then write the array into a FileStream. No error is found. So I think this problem may be related to the content of the pdf. Can you please provide the code used to generate the pdf document?


#3

The document is built from data in a database, so I'm not sure the code will help. If you still want it I'll attach the vb.net class with the next post, just let me know. The document saves to the hard drive ok, I just can seem to get it back from the byte array after saving to the memory stream. Can you give me the code you used to save to a memory stream, byte array and finally a file stream? Maybe that will point me in the right direction.

Thanks

James.


#4

Here is the code I used:

Dim mStream_ As New IO.MemoryStream

Dim pdf1 As Pdf = New Pdf
Dim sec1 As Section = pdf1.Sections.Add

Dim img1 As Image = New Image
img1.ImageInfo.File = "c:/images/apple.jpg"
sec1.Paragraphs.Add(img1)

pdf1.Save(mStream_)

Dim lByteArr_ As Byte()
lByteArr_ = mStream_.GetBuffer

Dim fStream As New IO.FileStream("d:/test/test.pdf", IO.FileMode.Create)
fStream.Write(lByteArr_, 0, lByteArr_.Length)

fStream.Close()
mStream_.Close()


#5

Mr. Wang,

Thanks for the quick replies. You all provide awesome customer support. I looked at your code and could not find anything different but it was still a help to know I was doing it correctly. After a little experimentation I discovered that if I DID NOT save the document to a file using the PDF.save(FileName) function but instead used ONLY PDF.save(MemoryStream) then everything works as expected. I was first saving the document to the file system (so I could look at it and compare with the memory stream result), then saving to the memory steam and finally writting to the file system. This is what caused the problem. Is this an intentional result that after saving with pdf.save(filename) that you are not supposed to call the pdf.save() function a second time? I tried just calling the pdf.save(filename) twice in a row and the second call to a different filename resulted in a corrupted file.

I now have the application working as desired but wondered if you knew of this problem and hopefully my problem/solution will help someone else. Of course it is more likely it something I was doing but your assitance helped me find a work around. Again thank you for your help.

James.


#6

Dear James,

This might be a bug. We have not noticed it before. We will check this issue and try to fix it in the future version.