Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PDF file stored as blob in the database

Status
Not open for further replies.

Kenbla

Programmer
Oct 11, 2006
68
SE
Hello, I have PDF file which is stored in a blob column in an SQL Server database. The blob was created by a .Net web service (WCF) as a byte array.
In my Delphi XE application I am supposed to "decode" the byte array blob and show the PDF file as it looked originally.
Is this possible to do and if so: How do I do it?
I have a component which can display PDF files but I don't know how to convert it from the blob field!
Thanks!
/Kenbla
 
The PDF component you have - Can it load a PDF from a Stream? If not, then you'll have to extract the PDF from the DB, save it to file, then load it into your component. (Also, since you don't have control over how the pdf file is saved to the database, there's no guarantee the code below will save the file as a valid pdf.

Code:
procedure TForm1.GetBlobField;
var
   BS: TBlobStream;
   FS: TFileStream;
begin
   BS := YouQuery.CreateBlobStream(YourQuery.FieldByName('BlobFieldName'), bmRead);
   try
      FS := TFileStream.Create('FilePath\FileName.pdf>' fmCreate);
      try
         FS.CopyFrom(BS, BS.Size);
      finally
         FS.Free;
      end;
   finally
      BS.Free;
   end;
end;

Now the PDF is in the file you just created, and you can load it into your component, or if the pdf component can use a stream directly, then you don't need the create the FileStream, and read the pdf directly from the BlobStream.

Remember to add the declaration for this method to your form.

Code:
TForm1 = class(TForm)
  ...
private
  procedure GetBlobField;
  ...
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top