rptbarcode.pp 5.9 KB

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