unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    Function setShapeData(shapeId:Integer;data:String): Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  zlDir:String = 'C:\\Program Files (x86)\\中琅条码标签打印软件\\';
  docFile:String = 'd:\\demo.zhl';
  labelImg:String = 'd:\\demo.png';
  outputPdf:String = 'd:\\outputDemo.pdf';
  outputPdfCustom:String = 'd:\\outputCustomDemo.pdf';
  overlayPdf:String = 'd:\\overlayDemo.pdf';
  demoText:String = '   中琅条码标签打印软件(LabelPainter)是一款强大、专业、易用的条码生成工具、二维码生成器、标签制作软件、批量打印工具。她适用于：防伪标签、物流标签、商品标签、超市价签、医药标签、珠宝标签、吊牌证卡等的设计制作及批量打印工作。\n' + '   中琅条码生成软件不仅仅支持普通的条码打印机、喷墨打印机、激光打印机，还支持各类数码印刷设备。软件支持RGB、CMYK(含专色)、HSL、HSV等颜色模式，及PDF矢量图导入、一键输出PDF文档等。';
  barcode1d, barcode2d, singleLineText, multiLineText, rect:Integer;
  pageCount:Integer = 10;
  pointXs:array[0..3] of Double = (0.8, 18.5, 18.5, 0.8);
  pointYs:array[0..3] of Double = (19.3, 19.3, 22.2, 22.2);

implementation

{$R *.dfm}

uses LabelPainterSDK;
Function TForm1.setShapeData(shapeId:Integer;data:String): Boolean;
var
  rt:eRET;
begin
  rt := ZL_SetDataCustom(shapeId, data);
  While RET_BUSY = rt do
  begin
    rt := ZL_SetDataCustom(shapeId, data);
  end;
  result:=(RET_OK = rt)
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  rt:eRET;
  i:Integer;
begin
  /////////////////////////////////////////////////////////////////
  ///                        常规操作示例                   ///////
  /////////////////////////////////////////////////////////////////
  //初始化
  rt := ZL_Initialization(zlDir);
  if (rt <> RET_OK) then
  begin
    ShowMessage('初始化失败');
  end;

  //新建文档
  rt := ZL_CreateDoc(60, 30);
  if (rt <> RET_OK) then
  begin
    ShowMessage('创建文档失败');
  end;

  //设置布局
  rt := ZL_SetLayout(0, 0, 0, 0, 1, 1, -1, -1, -1, -1, 1);
  if (rt <> RET_OK) then
  begin
    ShowMessage('设置布局失败');
  end;

  //插入二维码
  barcode2d := ZL_InsertBarcode(1.6, 1.9, 16.1, 16.1, 'http://www.tiaomaruanjian.com/', '', 58, 6, 0, false, false, RGB(0, 0, 0), RGB(255, 255, 255), '', 0, 0, 0, 0, 0, 0);
  if (barcode2d < 1) then
  begin
    ShowMessage('图形插入失败');
  end;

  //插入多边形
  rect := ZL_InsertPolygon(@pointXs[0], @pointYs[0], 4, 0.2, 1, RGB(128, 128, 128), RGB(255, 255, 255), 0);
  if (rect < 1) then
  begin
    ShowMessage('图形插入失败');
  end;
  //SetLength(@pointXs, 0);
  //SetLength(@pointYs, 0);

  //插入单行文本
  singleLineText := ZL_InsertText(1.2, 18.9, 16.7, 3.3, '中琅条码标签打印软件', 0, false, '微软雅黑', 6, 0, RGB(255, 0, 0), 0, 0.1, 0, 0);
  if (singleLineText < 1) then
  begin
    ShowMessage('图形插入失败');
  end;

  //插入多行文本
  multiLineText := ZL_InsertText(19.8, 1.0, 38.9, 28.2, demoText, 1, false, '宋体', 5, 0, RGB(0, 0, 0), 0, 0.1, 0.6, 0);
  if (multiLineText < 1) then
  begin
    ShowMessage('图形插入失败');
  end;

  //保存标签图片
  rt := ZL_SaveLabelImage(labelImg);
  if (rt <> RET_OK) then
  begin
    ShowMessage('标签图片生成失败');
  end;

  //直接输出
  ZL_OutputToPDF(outputPdf, false, '', 0, 1, 10, false);
  if (rt <> RET_OK) then
  begin
    ShowMessage('输出文档失败');
  end;

  //保存文档
  rt := ZL_SaveDoc(docFile);
  if (rt <> RET_OK) then
  begin
    ShowMessage('保存文档失败');
  end;

  //关闭文档
  rt := ZL_CloseDoc();
  if (rt <> RET_OK) then
  begin
    ShowMessage('关闭文档失败');
  end;


  /////////////////////////////////////////////////////////////////
  ///                        自定义输出示例                 ///////
  /////////////////////////////////////////////////////////////////
  //打开文档
  rt := ZL_OpenDoc(docFile, '');
  if (rt <> RET_OK) then
  begin
    ShowMessage('打开文档失败');
  end;

  //插入条形码
  barcode1d := ZL_InsertBarcode(1.0, 23.3, 17, 5.3, '123456', '??? ???', 20, 6, 0, false, false, RGB(0, 0, 0), RGB(255, 255, 255), '微软雅黑', 6, 0, RGB(0, 0, 255), 1, 0, 0);
  if (barcode1d < 1) then
  begin
    ShowMessage('图形插入失败');
  end;

  //删除图形
  rt := ZL_DeleteShape(rect);
  if (rt <> RET_OK) then
  begin
    ShowMessage('删除图形失败');
  end;
  rt := ZL_DeleteShape(multiLineText);
  if (rt <> RET_OK) then
  begin
    ShowMessage('删除图形失败');
  end;
  rt := ZL_DeleteShape(singleLineText);
  if (rt <> RET_OK) then
  begin
    ShowMessage('删除图形失败');
  end;
  rt := ZL_DeleteShape(barcode2d);
  if (rt <> RET_OK) then
  begin
    ShowMessage('删除图形失败');
  end;

  //自定义输出
  rt := ZL_OutputToPDF(outputPdfCustom, false, '', 0, 1, 10, true);
  if (rt <> RET_OK) then
  begin
    ShowMessage('自定义输出失败');
  end
  else
  begin
    //单页赋值并输出
    for i:= 1 to pageCount do
    begin
      //图形赋值
      if (false = setShapeData(barcode1d, Format('%.6d', [i]))) then
      begin
        ShowMessage('图形赋值失败');
        break;
      end;

      //输出当前页
      rt := ZL_StartOutputCustom();
      if (rt <> RET_OK) then
      begin
        ShowMessage('输出当前页失败');
        break;
      end;
    end;

    //停止输出
    rt := ZL_StopOutput();
    if (rt <> RET_OK) then
    begin
      ShowMessage('停止输出失败');
    end;
  end;

  //关闭文档
  rt := ZL_CloseDoc();
  if (rt <> RET_OK) then
  begin
    ShowMessage('关闭文档失败');
  end;


  /////////////////////////////////////////////////////////////////
  ///                        PDF工具箱示例                  ///////
  /////////////////////////////////////////////////////////////////
  rt := ZL_PdfTool(overlayPdf, outputPdfCustom, outputPdf, 3, true, false, 1);
  if (rt <> RET_OK) then
  begin
    ShowMessage('PDF文档处理失败');
  end;


  //释放资源
  rt := ZL_Release();
  if (rt <> RET_OK) then
  begin
    ShowMessage('释放资源失败');
  end;
end;

end.
