// CPP.cpp : 定义控制台应用程序的入口点。
//

//#include "../../Include/jni.h" //添加头文件引用  
//#include "../../Include/jni_md.h" //添加头文件引用  
#include <stdio.h>
#include "../../Include/LabelPainter_SDK.h" //添加头文件引用  

#pragma comment(lib, "../../Lib/LabelPainter_SDK.lib") //添加lib文件引用

int getRGB(int red, int green, int blue) {
	return ((255 & 0xff) << 24) | ((red & 0xff) << 16) | ((green & 0xff) << 8) | ((blue & 0xff) << 0);
}

bool setShapeData(int shapeId, char *data)
{
	eRET rt = ZL_SetDataCustom(shapeId, data);
	while (rt == RET_BUSY)
	{
		rt = ZL_SetDataCustom(shapeId, data);
	}
	return rt == RET_OK;
}

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

	/////////////////////////////////////////////////////////////////
	///                        常规操作示例                   ///////
	///////////////////////////////////////////////////////////////// 
	//初始化
	rt = ZL_Initialization(zlDir);
	if (rt != RET_OK)
		printf("初始化失败");

	//新建文档
	rt = ZL_CreateDoc(60, 30);
	if (rt != RET_OK)
		printf("创建文档失败");

	//设置布局
	rt = ZL_SetLayout(0, 0, 0, 0, 1, 1, -1, -1, -1, -1, 1);
	if (rt != RET_OK)
		printf("设置布局失败");

	//插入二维码
	barcode2d = ZL_InsertBarcode(1.6, 1.9, 16.1, 16.1, "http://www.tiaomaruanjian.com/", "", 58, 6, 0, false, false, getRGB(0, 0, 0), getRGB(255, 255, 255), "", 0, 0, 0, 0, 0, 0);
	if (barcode2d < 1)
		printf("图形插入失败");

	//插入多边形
	rect = ZL_InsertPolygon(new double[4] { 0.8, 18.5, 18.5, 0.8 }, new double[4] { 19.3, 19.3, 22.2, 22.2 }, 4, 0.2, 1, getRGB(128, 128, 128), getRGB(255, 255, 255), 0);
	if (rect < 1)
		printf("图形插入失败");

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

	//插入多行文本
	multiLineText = ZL_InsertText(19.8, 1.0, 38.9, 28.2, demoText, 1, false, "宋体", 5, 0, getRGB(0, 0, 0), 0, 0.1, 0.6, 0);
	if (multiLineText < 1)
		printf("图形插入失败");

	//保存标签图片
	rt = ZL_SaveLabelImage(labelImg);
	if (rt != RET_OK)
		printf("标签图片生成失败");

	//直接输出
	ZL_OutputToPDF(outputPdf, false, "", 0, 1, 10, false);
	if (rt != RET_OK)
		printf("输出文档失败");

	//保存文档
	rt = ZL_SaveDoc(docFile);
	if (rt != RET_OK)
		printf("保存文档失败");

	//关闭文档
	rt = ZL_CloseDoc();
	if (rt != RET_OK)
		printf("关闭文档失败");


	/////////////////////////////////////////////////////////////////
	///                        自定义输出示例                  ///////
	///////////////////////////////////////////////////////////////// 
	//打开文档
	rt = ZL_OpenDoc(docFile, "");
	if (rt != RET_OK)
		printf("打开文档失败");

	//插入条形码
	barcode1d = ZL_InsertBarcode(1.0, 23.3, 17, 5.3, "123456", "??? ???", 20, 6, 0, false, false, getRGB(0, 0, 0), getRGB(255, 255, 255), "微软雅黑", 6, 0, getRGB(0, 0, 255), 1, 0, 0);
	if (barcode1d < 1)
		printf("图形插入失败");

	//删除图形
	rt = ZL_DeleteShape(rect);
	if (rt != RET_OK)
		printf("删除图形失败");
	rt = ZL_DeleteShape(multiLineText);
	if (rt != RET_OK)
		printf("删除图形失败");
	rt = ZL_DeleteShape(singleLineText);
	if (rt != RET_OK)
		printf("删除图形失败");
	rt = ZL_DeleteShape(barcode2d);
	if (rt != RET_OK)
		printf("删除图形失败");

	//自定义输出
	rt = ZL_OutputToPDF(outputPdfCustom, false, "", 0, 1, 10, true);
	if (rt != RET_OK)
		printf("自定义输出失败");
	else
	{
		//单页赋值并输出
		for (int i = 1; i <= pageCount; i++)
		{
			//图形赋值
			//char *str = "";
			char str[10];
			sprintf_s(str, 10, "%06d", i);
			if (!setShapeData(barcode1d, str))
			{
				printf("图形赋值失败");
				break;
			}

			//输出当前页
			rt = ZL_StartOutputCustom();
			if (rt != RET_OK)
			{
				printf("输出当前页失败");
				break;
			}
		}

		//停止输出
		rt = ZL_StopOutput();
		if (rt != RET_OK)
			printf("停止输出失败");
	}

	//关闭文档
	rt = ZL_CloseDoc();
	if (rt != RET_OK)
		printf("关闭文档失败");


	///////////////////////////////////////////////////////////////////
	/////                        PDF工具箱示例                   ///////
	///////////////////////////////////////////////////////////////////
	rt = ZL_PdfTool(overlayPdf, outputPdfCustom, outputPdf, 3, true, false, 1);
	if (rt != RET_OK)
		printf("PDF文档处理失败");


	//释放资源
	rt = ZL_Release();
	if (rt != RET_OK)
		printf("释放资源失败");


	return 0;
}
