Database

This example uses MS SQL Express database. There is one table in the database named Templates. Table contains BLOB field with name Data. Demo application expects that at least one record is inserted in the table. Database is not included in the archive. You may use the script below to create similar table in your environment.

BEGIN TRANSACTION
GO
CREATE TABLE dbo.Templates
(
ID uniqueidentifier NOT NULL,
DATA varbinary(MAX) NULL
) ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE dbo.Templates ADD CONSTRAINT
PK_Table_1 PRIMARY KEY CLUSTERED
(
ID
)
WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]

GO
COMMIT

INSERT Templates (ID, DATA)
VALUES (NEWID(), Null)

 

Delphi form

There is ADOConnection component on the form with ADOTable component. Table component is connected to the Templates sql table. Pascal unit contains procedures for writing, reading rtf file from BLOB field, and making report from template stored in memory.

 

Load template from file to Blob

procedure TForm2.LoadBtnClick(Sender: TObject);
var BF:TBlobField;
SFileName:string;
begin

SFileName := 'sample_file.rtf';

(* check file exists*)
if not FileExists(SFileName) then
begin
if RTFDialog.Execute then begin
SFileName := RTFDialog.FileName;
end else exit;
end;

BF := TBlobField(Templates.FieldByName('Data'));
Templates.Edit;
BF.LoadFromFile(SFileName);
Templates.Post;
showmessage('File is saved to database');
end;

Save template from Blob to file

procedure TForm2.SaveBtnClick(Sender: TObject);
var BF:TBlobField;
begin
BF := TBlobField(Templates.FieldByName('Data'));
BF.SaveToFile('save.rtf');
showmessage('File is saved from database to disk');
end;

Load template in memory and make report

procedure TForm2.ReportBtnClick(Sender: TObject);
var BF:TBlobField;
BS:TStream;
TemplateBuffer : ByteArray;
Ln:longint;
begin
BF := TBlobField(Templates.FieldByName('Data'));
BS := Templates.CreateBlobStream(BF,bmRead);
Ln := BF.BlobSize;
SetLength(TemplateBuffer, Ln);
BS.Read(TemplateBuffer[0],Ln);
EkRtf1.SetTemplateBuffer(TemplateBuffer,Ln);
EkRtf1.ExecuteOpen([],SW_SHOW);
BS.Free;
end;