rptbarcode.pp 5.9 KB

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