1unit rptbarcode;
2
3
4{$mode objfpc}{$H+}
5{$I demos.inc}
6
7interface
8
9uses
10  Classes,
11  SysUtils,
12  fpreport,
13  fpreportcontnr,
14  fpreportbarcode,
15  udapp;
16
17type
18
19  { TCountry }
20
21  TCountry = Class(TCollectionItem)
22  private
23    FName: String;
24    FPopulation: Int64;
25  Published
26    Property Name : String Read FName Write FName;
27    Property Population : Int64 Read FPopulation Write FPopulation;
28  end;
29
30  { TCollectionDemo }
31
32  { TBarcodeDemo }
33
34  TBarcodeDemo = class(TReportDemoApp)
35  private
36    procedure SetBarcodeValue(Sender: TFPReportElement);
37  Protected
38    FReportData : TFPReportObjectData;
39    FBarcode: TFPReportBarcode;
40  public
41    procedure   InitialiseData; override;
42    constructor Create(AOWner :TComponent); override;
43    Class function Description : string; override;
44    procedure   CreateReportDesign;override;
45    procedure   LoadDesignFromFile(const AFilename: string);
46    procedure   HookupData(const AComponentName: string; const AData: TFPReportData);
47    destructor  Destroy; override;
48  end;
49
50
51
52implementation
53
54uses
55  fpReportStreamer,
56  fpTTF,
57  fpJSON,
58  jsonparser;
59
60procedure TBarcodeDemo.CreateReportDesign;
61var
62  p: TFPReportPage;
63  TitleBand: TFPReportTitleBand;
64  DataBand: TFPReportDataBand;
65  GroupHeader: TFPReportGroupHeaderBand;
66  Memo: TFPReportMemo;
67  PageFooter: TFPReportPageFooterBand;
68
69begin
70  Inherited;
71  rpt.Author := 'Michael Van Canneyt';
72  rpt.Title := 'FPReport Demo : Barcodes';
73
74  p :=  TFPReportPage.Create(rpt);
75  p.Orientation := poPortrait;
76  p.PageSize.PaperName := 'A4';
77  { page margins }
78  p.Margins.Left := 30;
79  p.Margins.Top := 20;
80  p.Margins.Right := 30;
81  p.Margins.Bottom := 20;
82  p.Data := FReportData;
83  p.Font.Name := 'LiberationSans';
84
85  TitleBand := TFPReportTitleBand.Create(p);
86  TitleBand.Layout.Height := 40;
87  {$ifdef ColorBands}
88  TitleBand.Frame.Shape := fsRectangle;
89  TitleBand.Frame.BackgroundColor := clReportTitleSummary;
90  {$endif}
91
92  Memo := TFPReportMemo.Create(TitleBand);
93  Memo.Layout.Left := 35;
94  Memo.Layout.Top := 20;
95  Memo.Layout.Width := 80;
96  Memo.Layout.Height := 10;
97  Memo.Text := 'COUNTRY AND POPULATION AS OF 2014';
98
99  GroupHeader := TFPReportGroupHeaderBand.Create(p);
100  GroupHeader.Layout.Height := 15;
101  GroupHeader.GroupCondition := 'copy(''[Name]'',1,1)';
102  {$ifdef ColorBands}
103  GroupHeader.Frame.Shape := fsRectangle;
104  GroupHeader.Frame.BackgroundColor := clGroupHeaderFooter;
105  {$endif}
106
107  Memo := TFPReportMemo.Create(GroupHeader);
108  Memo.Layout.Left := 0;
109  Memo.Layout.Top := 5;
110  Memo.Layout.Width := 10;
111  Memo.Layout.Height := 8;
112  Memo.UseParentFont := False;
113  Memo.Text := '[copy(Name,1,1)]';
114  Memo.Font.Size := 16;
115
116  DataBand := TFPReportDataBand.Create(p);
117  DataBand.Layout.Height := 8;
118  {$ifdef ColorBands}
119  DataBand.Frame.Shape := fsRectangle;
120  DataBand.Frame.BackgroundColor := clDataBand;
121  {$endif}
122
123  Memo := TFPReportMemo.Create(DataBand);
124  Memo.Layout.Left := 15;
125  Memo.Layout.Top := 1;
126  Memo.Layout.Width := 50;
127  Memo.Layout.Height := 5;
128  Memo.Text := '[Name]';
129
130  Memo := TFPReportMemo.Create(DataBand);
131  Memo.Layout.Left := 70;
132  Memo.Layout.Top := 1;
133  Memo.Layout.Width := 30;
134  Memo.Layout.Height := 5;
135  Memo.Text := '[formatfloat(''#,##0'', Population)]';
136
137  FBarcode := TFPReportBarcode.Create(DataBand);
138  FBarcode.Layout.Left := 100;
139  FBarcode.Layout.Top := 1;
140  FBarcode.Layout.Width := 50;
141  FBarcode.Layout.Height := 5;
142  FBarCode.PadLength:=12;
143  // Only one of the 2 ways must be used: either set expression, either use callback.
144  FBarcode.Expression:='Population';
145  // Databand.OnBeforePrint:=@SetBarcodeValue;
146
147
148  PageFooter := TFPReportPageFooterBand.Create(p);
149  PageFooter.Layout.Height := 20;
150  {$ifdef ColorBands}
151  PageFooter.Frame.Shape := fsRectangle;
152  PageFooter.Frame.BackgroundColor := clPageHeaderFooter;
153  {$endif}
154
155  Memo := TFPReportMemo.Create(PageFooter);
156  Memo.Layout.Left := 130;
157  Memo.Layout.Top := 13;
158  Memo.Layout.Width := 20;
159  Memo.Layout.Height := 5;
160  Memo.Text := 'Page [PageNo]';
161  Memo.TextAlignment.Vertical := tlCenter;
162  Memo.TextAlignment.Horizontal := taRightJustified;
163end;
164
165procedure TBarcodeDemo.LoadDesignFromFile(const AFilename: string);
166var
167  rs: TFPReportJSONStreamer;
168  fs: TFileStream;
169  lJSON: TJSONObject;
170begin
171  if AFilename = '' then
172    Exit;
173  if not FileExists(AFilename) then
174    raise Exception.CreateFmt('The file "%s" can not be found', [AFilename]);
175  fs := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
176  try
177    lJSON := TJSONObject(GetJSON(fs));
178  finally
179    fs.Free;
180  end;
181  rs := TFPReportJSONStreamer.Create(nil);
182  rs.JSON := lJSON; // rs takes ownership of lJSON
183  try
184    rpt.ReadElement(rs);
185  finally
186    rs.Free;
187  end;
188end;
189
190procedure TBarcodeDemo.HookupData(const AComponentName: string; const AData: TFPReportData);
191var
192  b: TFPReportCustomBandWithData;
193begin
194  b := TFPReportCustomBandWithData(rpt.FindRecursive(AComponentName));
195  if Assigned(b) then
196    b.Data := AData;
197end;
198
199destructor TBarcodeDemo.Destroy;
200begin
201  FreeAndNil(FReportData);
202  inherited Destroy;
203end;
204
205constructor TBarcodeDemo.Create(AOWner: TComponent);
206begin
207  inherited;
208  FReportData := TFPReportCollectionData.Create(nil);
209  TFPReportCollectionData(FReportData).OwnsCollection:=True;
210end;
211
212class function TBarcodeDemo.Description: string;
213begin
214  Result:='Demo showing native support for barcodes';
215end;
216
217{ TBarcodeDemo }
218
219procedure TBarcodeDemo.SetBarcodeValue(Sender: TFPReportElement);
220
221begin
222  FBarcode.Value:=FReportData.FieldValues['Population'];
223end;
224
225procedure TBarcodeDemo.InitialiseData;
226
227Var
228  SL : TStringList;
229  i : Integer;
230  N,V : String;
231  C : TCountry;
232  Coll : TCollection;
233
234begin
235  Coll:=TCollection.Create(TCountry);
236  TFPReportCollectionData(FReportData).Collection:=coll;
237  SL:=TStringList.Create;
238  try
239    {$I countries.inc}
240    SL.Sort;
241    For I:=0 to SL.Count-1 do
242      begin
243      C:=Coll.Add As TCountry;
244      SL.GetNameValue(I,N,V);
245      C.Name:=N;
246      C.Population:=StrToInt64Def(V,0);
247      end;
248  finally
249    SL.Free;
250  end;
251end;
252
253end.
254
255