//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"

#include <stdio.h>
#include <alloc.h>

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------


//defined for ApSetFunction
#define Ap_Set_MultiPage			101
#define Ap_Set_ImagePosAndSize		104
#define Ap_Set_Encrypt 				105
#define Ap_Set_Font					106
#define Ap_Set_NewOrClosePage		109
#define Ap_Set_BookMark				110
#define Ap_Set_ViewerPreferences	121
#define Ap_Set_Orientation			122
#define Ap_Set_Recompress			123
#define Ap_Set_Skewcorrect			126
#define Ap_Set_JpegQuality			128


//defined for ApGetFunction
#define Ap_Get_ImagePageCount		201
#define Ap_Get_ImageSizeW			202
#define Ap_Get_ImageSizeH			203
#define Ap_Get_ImageDpiX			204
#define Ap_Get_ImageDpiY			205
#define Ap_Get_PdfPageCount			206
#define Ap_Get_PdfCurrentPageSize	209
#define Ap_Get_StringWidth			250
#define Ap_Get_StringHeight			251
#define Ap_Get_Recomporess			210
#define Ap_Get_ImageBits			211

//defined for ApAddText
#define Ap_DT_LEFT			0
#define Ap_DT_CENTER		1
#define Ap_DT_RIGHT			2
#define Ap_DT_WORDBREAK		0x10
#define Ap_DT_CALCRECT		0x400

//defined permission
#define permission_Print	1
#define permission_Copy		2
#define permission_Modify	4

#define MaxPath 512

//---------------------------------------------------------------------------

//prototype declare
typedef long	(__stdcall *ApCreate)(char *filename);

typedef long	(__stdcall *ApOpen)(char *filename);

typedef void	(__stdcall *ApClose)(long id);

typedef long	(__stdcall *ApAddInfo)(long id,
									   char * title,
									   char * subject,
									   char * author,
									   char * keywords,
									   char * creator);
typedef long	(__stdcall *ApSetFunction)(long id,
										   long code,
										   long para1,
										   long para2,
										   char *para3,
										   char *para4);
typedef long	(__stdcall *ApGetFunction)(long id,
										   long code,
										   long para1,
										   long para2,
										   char *para3,
										   char *para4);

typedef long	(__stdcall *ApAddImage)(long id,char *filename);

typedef long	(__stdcall *ApAddImageData)(long id,
											char *data,
											long width,
											long height,
											long color/*1,8,24*/);

typedef long	(__stdcall *ApAddTextEx)(long id,
										 long x,
										 long y,
										 char *string,
										 long color);
typedef long	(__stdcall *ApAddText)(long id,
									   long x,
									   long y,
									   long width,
									   long height, 
									   char *string,
									   long color,
									   long bkColor,
									   long format);
typedef long	(__stdcall *ApAddTxt)(long id,
									  char *textFileName,
									  long color,	
									  long width,
									  long height,
									  long autoNewLine,
									  long autoWidthAdjust, 
									  long leftMargin,
									  long rightMargin,
									  long topMargin,
									  long bottomMargin,	
									  long tabSize);
typedef long	(__stdcall *ApAddLine)(long id,
									   long sx,
									   long sy,
									   long ex,
									   long ey,
									   long width,
									   long color);
typedef long	(__stdcall *ApAddRect)(long id,
									   long sx,
									   long sy,
									   long ex,
									   long ey,
									   long sideWidth,
									   long sideColor,
									   long flagFill,
									   long fillColor);


//----------------------------------------------------------------------------------------
HMODULE				g_hApW=NULL;

ApCreate			pApCreate;
ApOpen				pApOpen;
ApAddImage			pApAddImage;
ApAddImageData		pApAddImageData;
ApClose				pApClose;	
ApAddInfo			pApAddInfo;	
ApSetFunction		pApSetFunction;
ApGetFunction		pApGetFunction;
ApAddTextEx			pApAddTextEx;
ApAddText			pApAddText;
ApAddTxt			pApAddTxt;
ApAddLine			pApAddLine;
ApAddRect			pApAddRect;

void	GetModulePath(char *out_path,char *in_name);
BOOL	LoadApWDll(char*pDll);
void	FreeApWDll();




//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------

void GetModulePath(char *out_path,char *in_name)
{
	char *p;
	GetModuleFileName(NULL,out_path,MaxPath);
	p =strrchr(out_path,'\\');
	p[1]=0;
	strcat(out_path,in_name);
}
//----------------------------------------------------------------------------------------

BOOL LoadApWDll(char *pDll)
{

	g_hApW=LoadLibrary(pDll);

	if(g_hApW==NULL)
		goto Failer;

	pApCreate=(ApCreate)GetProcAddress(g_hApW,"ApCreate");
	if(pApCreate==NULL)
		goto Failer;
	pApOpen=(ApOpen)GetProcAddress(g_hApW,"ApOpen");
	if(pApOpen==NULL)
		goto Failer;
	pApClose=(ApClose)GetProcAddress(g_hApW,"ApClose");
	if(pApClose==NULL)
		goto Failer;
	pApAddImage=(ApAddImage)GetProcAddress(g_hApW,"ApAddImage");
	if(pApAddImage==NULL)
		goto Failer;
	pApAddImageData=(ApAddImageData)GetProcAddress(g_hApW,"ApAddImageData");
	if(pApAddImageData==NULL)
		goto Failer;
	pApAddInfo=(ApAddInfo)GetProcAddress(g_hApW,"ApAddInfo");
	if(pApAddInfo==NULL)
		goto Failer;
	pApSetFunction=(ApSetFunction)GetProcAddress(g_hApW,"ApSetFunction");
	if(pApSetFunction==NULL)
		goto Failer;
	pApGetFunction=(ApGetFunction)GetProcAddress(g_hApW,"ApGetFunction");
	if(pApGetFunction==NULL)
		goto Failer;
	pApAddTextEx=(ApAddTextEx)GetProcAddress(g_hApW,"ApAddTextEx");
	if(pApAddTextEx==NULL)
		goto Failer;
	pApAddText=(ApAddText)GetProcAddress(g_hApW,"ApAddText");
	if(pApAddText==NULL)
		goto Failer;
	pApAddTxt=(ApAddTxt)GetProcAddress(g_hApW,"ApAddTxt");
	if(pApAddTxt==NULL)
		goto Failer;
	pApAddLine=(ApAddLine)GetProcAddress(g_hApW,"ApAddLine");
	if(pApAddLine==NULL)
		goto Failer;
	pApAddRect=(ApAddRect)GetProcAddress(g_hApW,"ApAddRect");
	if(pApAddRect==NULL)
		goto Failer;
	return TRUE;
Failer:
	FreeApWDll(); 
	return FALSE;

}
//----------------------------------------------------------------------------------------

void FreeApWDll()
{
	if(g_hApW==NULL)
		return ;
	FreeLibrary(g_hApW);
	g_hApW=NULL;
}


//----------------------------------------------------------------------------------------

void AddImageData(long id)
{
	int i,j;
	int temp1,temp2;
	int red,green,blue;
	int MemImageWidth=640;
	int MemImageHeight=640;
	int color=24;//can be equal 1 , 8 or 24
	char *MemData=(char*)malloc(MemImageWidth*MemImageHeight*3);
	for(i=0;i<MemImageHeight - 1;i++){
		for(j=0;j<MemImageWidth - 1;j++){
			temp1=i&8;
			temp2=j&8;
			if(temp1|temp2){
				red=255;
				green = blue = 0;
			}else{
                red = 0;
                green = blue = 255	;		
			}
            MemData[i * MemImageWidth * 3 + j * 3 + 0] = red;
            MemData[i * MemImageWidth * 3 + j * 3 + 1] = green;
            MemData[i * MemImageWidth * 3 + j * 3 + 2] = blue;
		}
	}

	//create page
	pApSetFunction(id,Ap_Set_NewOrClosePage,MemImageWidth,MemImageHeight,0,0);

	pApAddImageData(id,MemData,MemImageWidth,MemImageHeight,color);

	//close page
	pApSetFunction(id,Ap_Set_NewOrClosePage,0,0,0,0);

	free(MemData);
	MemData=NULL;
}

//----------------------------------------------------------------------------------------
//split image file
void One2Multiple(char *pPdf,char *pImage)
{
	long id,pagecount,beginpage;
	char szPDFFile[MaxPath];
	
	//get multipage image page count
	pagecount=pApGetFunction(0,Ap_Get_ImagePageCount,0,0,pImage,0);
	for(beginpage=1; beginpage<=pagecount; beginpage++)
	{
		sprintf(szPDFFile,"%s%04d.pdf",pPdf,beginpage);

		if( (id=pApCreate(szPDFFile)) >0 )
		{
			pApSetFunction(id,Ap_Set_MultiPage,beginpage,1,0,0);	
			pApAddImage(id,pImage);
			pApClose(id);
		}
	}
}

//----------------------------------------------------------------------------------------
void One2One(char *pPdf, char *pImage)
{
	long id;

	if( (id=pApCreate(pPdf)) >0 )
	{
		pApAddImage(id,pImage);
		pApClose(id);
	}
}
//----------------------------------------------------------------------------------------
//merge image file
void Multiple2One(char *pPdf, char *pImage[],long len)
{	
	long id,i;
	
	if( (id=pApCreate(pPdf)) >0 )
	{
		for(i = 0; i < len; i++)
		{
			pApAddImage(id,pImage[i]);	
		}
		pApClose(id);
	}
}

//----------------------------------------------------------------------------------------

void AppendPDF(char *pPdf,char *pImage[],long len)
{
	long id,i;

	for(i = 0; i < len; i++){
		if( (id=pApOpen(pPdf)) >0 )//Append PDF file
		{
			pApAddImage(id,pImage[i]);
			pApClose(id);
		}else
		if( (id=pApCreate(pPdf)) >0 )//Create new PDF file
		{
			pApAddImage(id,pImage[i]);
			pApClose(id);
		}
	}
		
}
//----------------------------------------------------------------------------------------

void Skewcorrect(char *pPdf,char *pImage)
{
	long id;

	if( (id=pApCreate(pPdf)) >0 )
	{
		pApAddImage(id,pImage);

		//set skewcorrect
		pApSetFunction(id,Ap_Set_Skewcorrect,1,0,NULL,NULL);

		pApAddImage(id,pImage);		

		pApClose(id);	
	}

}
//----------------------------------------------------------------------------------------

void RecompressTruecolorFile(char *pPdf, char *pImage)
{
	long id;
	char szPDFFile[MaxPath];

	// recompress lzw
	sprintf(szPDFFile,"%s_lzw.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,3,0,"lzw",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	// recompress zip
	sprintf(szPDFFile,"%s_zip.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,3,0,"zip",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	// recompress packbits
	sprintf(szPDFFile,"%s_packbits.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,3,0,"packbits",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	// recompress jpeg,quality=100
	sprintf(szPDFFile,"%s_jpeg_100.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,3,0,"jpeg",NULL);
		pApSetFunction(id,Ap_Set_JpegQuality,100,0,NULL,NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	// recompress jpeg,quality=50
	sprintf(szPDFFile,"%s_jpeg_50.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,3,0,"jpeg",NULL);
		pApSetFunction(id,Ap_Set_JpegQuality,50,0,NULL,NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

}
//----------------------------------------------------------------------------------------

void RecompressGrayFile(char *pPdf, char *pImage)
{
	long id;
	char szPDFFile[MaxPath];

	// recompress lzw
	sprintf(szPDFFile,"%s_lzw.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,2,0,"lzw",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	// recompress zip
	sprintf(szPDFFile,"%s_zip.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,2,0,"zip",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	// recompress packbits
	sprintf(szPDFFile,"%s_packbits.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,2,0,"packbits",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	// recompress jpeg, default quality=75
	sprintf(szPDFFile,"%s_jpeg.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,2,0,"jpeg",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

}
//----------------------------------------------------------------------------------------

void RecompressBlackWhiteFile(char *pPdf, char *pImage)
{

	long id;
	char szPDFFile[MaxPath];

	// recompress lzw
	sprintf(szPDFFile,"%s_lzw.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,1,0,"lzw",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	// recompress zip
	sprintf(szPDFFile,"%s_zip.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,1,0,"zip",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	// recompress CCITT Group 3 fax encoding
	sprintf(szPDFFile,"%s_g3.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,1,0,"g3",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	// recompress CCITT Group 4 fax encoding
	sprintf(szPDFFile,"%s_g4.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{
		pApSetFunction(id,Ap_Set_Recompress,1,0,"g4",NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

}
//----------------------------------------------------------------------------------------

void Recompress(char *pPdf, char *pImage)
{
	long bits;
	bits=pApGetFunction(0,Ap_Get_ImageBits,0,0,pImage,NULL);	
	switch(bits)
	{
	case 1:
		RecompressBlackWhiteFile(pPdf,pImage);
		break;
	case 8:
		RecompressGrayFile(pPdf,pImage);
		break;
	case 24:
		RecompressTruecolorFile(pPdf,pImage);
		break;
	default :
		break;
	}

}

//----------------------------------------------------------------------------------------

void PageSize(char *pPdf, char *pImage)
{
	long id;
	float fZoom, zw,zh;
	char szW[32],szH[32];
	long ImageWidth,ImageHeight,ImageWidth2,ImageHeight2,PageWidth,PageHeight;
	long xdpi,ydpi;

	if( (id=pApCreate(pPdf)) >0 )
	{
		//Get image width and height
		pApGetFunction(id,Ap_Get_ImageSizeW,(long)&ImageWidth,(long)&ImageHeight,pImage,NULL);
		//Get image dpi (Resolution)
		pApGetFunction(id,Ap_Get_ImageDpiX,(long)&xdpi,(long)&ydpi,pImage,NULL);

		//keep image dpi (resolution)
		if(xdpi>0&&ydpi>0){
 			ImageWidth2  = (long)(ImageWidth * (72.0/xdpi));
 			ImageHeight2 = (long)(ImageHeight* (72.0/ydpi));
		}
		pApSetFunction(id,Ap_Set_NewOrClosePage,ImageWidth2,ImageHeight2,0,0);
		pApAddImage(id,pImage);

		//page size with image size
		pApSetFunction(id,Ap_Set_NewOrClosePage,ImageWidth,ImageHeight,0,0);
		pApAddImage(id,pImage);

		//A4
		pApSetFunction(id,Ap_Set_NewOrClosePage,2,2,0,0);
		pApSetFunction(id,Ap_Set_ImagePosAndSize,0,0,ltoa(ImageWidth,szW,10),ltoa(ImageHeight,szH,10));
		pApAddImage(id,pImage);

		//custom page size and fit width/height 
		PageWidth=768;
		PageHeight=1024;
		pApSetFunction(id,Ap_Set_NewOrClosePage,PageWidth,PageHeight,0,0);
		zw = (float)PageWidth/ImageWidth;
		zh = (float)PageHeight/ImageHeight;
		fZoom= zw < zh ? zw : zh;
		ImageWidth = (long)(ImageWidth * fZoom);
		ImageHeight=(long)(ImageHeight * fZoom);
		pApSetFunction( id,Ap_Set_ImagePosAndSize,(PageWidth-ImageWidth)/2,(PageHeight-ImageHeight)/2,
						ltoa(ImageWidth,szW,10),ltoa(ImageHeight,szH,10));
		pApAddImage(id,pImage);
		
		pApClose(id);
	}

}

//----------------------------------------------------------------------------------------

void PageLayout(char *pPdf, char *pImage[], long len)
{
	long id,i,nPos=1;
	long nPages,index;
	long PageWidth,PageHeight;
	char szW[32],szH[32];

	PageWidth=768;
	PageHeight=1024;

	if( (id=pApCreate(pPdf)) >0 )
	{
		for(i = 0; i < len; i++)
		{
			//get pages
			nPages=pApGetFunction(id,Ap_Get_ImagePageCount,0,0,pImage[i],0);
			//
			for(index = 1; index <= nPages; index++)
			{
				switch(nPos%4){
				case 1:{
						//new page
						pApSetFunction(id,Ap_Set_NewOrClosePage,PageWidth,PageHeight,0,0);
						//set image posion
						pApSetFunction(id,Ap_Set_ImagePosAndSize,0,0,ltoa(PageWidth/2,szW,10),ltoa(PageHeight/2,szH,10));
						pApSetFunction(id,Ap_Set_MultiPage,index,1,0,0);
						pApAddImage(id,pImage[i]);
					   }break;
				case 2:{
						pApSetFunction(id,Ap_Set_ImagePosAndSize,PageWidth/2,0,ltoa(PageWidth/2,szW,10),ltoa(PageHeight/2,szH,10));
						pApSetFunction(id,Ap_Set_MultiPage,index,1,0,0);
						pApAddImage(id,pImage[i]);
					   }break;
				case 3:{
						pApSetFunction(id,Ap_Set_ImagePosAndSize,0,PageHeight/2,ltoa(PageWidth/2,szW,10),ltoa(PageHeight/2,szH,10));
						pApSetFunction(id,Ap_Set_MultiPage,index,1,0,0);
						pApAddImage(id,pImage[i]);
					   }break;
				case 0:{
						pApSetFunction(id,Ap_Set_ImagePosAndSize,PageWidth/2,PageHeight/2,ltoa(PageWidth/2,szW,10),ltoa(PageHeight/2,szH,10));
						pApSetFunction(id,Ap_Set_MultiPage,index,1,0,0);
						pApAddImage(id,pImage[i]);
						//close page
						pApSetFunction(id,Ap_Set_NewOrClosePage,0,0,0,0);
					   }break;
				}			
				nPos++;
			}
		}
		pApClose(id);
	}

}
//----------------------------------------------------------------------------------------

void PageOrientation(char *pPdf, char *pImage)
{
	long id;
	char szPDFFile[MaxPath];
	long nOrientation;

	nOrientation=90;
	sprintf(szPDFFile,"%s_90.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{	
		pApSetFunction(id,Ap_Set_Orientation,nOrientation,0,NULL,NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	nOrientation=180;
	sprintf(szPDFFile,"%s_180.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{	
		pApSetFunction(id,Ap_Set_Orientation,nOrientation,0,NULL,NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

	nOrientation=270;
	sprintf(szPDFFile,"%s_270.pdf",pPdf);
	if( (id=pApCreate(szPDFFile)) >0 )
	{	
		pApSetFunction(id,Ap_Set_Orientation,nOrientation,0,NULL,NULL);
		pApAddImage(id,pImage);
		pApClose(id);
	}

}
//----------------------------------------------------------------------------------------

void Watermark(char *pPdf, char *pImage[],long len)
{
	long id,i;
	long nPages,index;
	long ImageWidth,ImageHeight;
	char szW[32],szH[32],szP[32];
	long nPosX,nPosY;

	if( (id=pApCreate(pPdf)) >0 )
	{
		for(i = 0; i < len; i++)
		{
			nPages=pApGetFunction(id,Ap_Get_ImagePageCount,0,0,pImage[i],0);
			for(index=1; index<=nPages; index++)
			{
				//get image width and height
				pApGetFunction(id,Ap_Get_ImageSizeW,(long)&ImageWidth,(long)&ImageHeight,pImage[i],ltoa(index,szP,10));
				//new page
				pApSetFunction(id,Ap_Set_NewOrClosePage,ImageWidth,ImageHeight,0,0);
				//set image pos
				pApSetFunction(id,Ap_Set_ImagePosAndSize,0,0,ltoa(ImageWidth,szW,10),ltoa(ImageHeight,szH,10));
			
				pApSetFunction(id,Ap_Set_MultiPage,index,1,0,0);
				pApAddImage(id,pImage[i]);
				
				//set system font and size
				pApSetFunction(id,Ap_Set_Font,1,32,"Arial",0);
				//add text
				nPosX=10;
				nPosY=ImageHeight/2;
				pApAddTextEx(id,nPosX,nPosY,"(c)Copyright XXXXXXXXXX",RGB(255,0,0));

				//close page
				pApSetFunction(id,Ap_Set_NewOrClosePage,0,0,0,0);
			}
		}
		pApClose(id);
	}
	
}

//----------------------------------------------------------------------------------------
void PDFsecurity(char *pPdf, char *pImage)
{
	long id;

	if( (id=pApCreate(pPdf)) >0 )
	{
		pApSetFunction(id,Ap_Set_Encrypt,128,permission_Print+permission_Copy+permission_Modify,"owner","user");
		pApAddImage(id,pImage);
		pApClose(id);
	}

}
//----------------------------------------------------------------------------------------

void PDFInfo(char *pPdf, char *pImage)
{
	long id;

	if( (id=pApCreate(pPdf)) >0 )
	{
		pApAddInfo(id,"Title","Subject","Author","Keywords","Creator");
		pApAddImage(id,pImage);
		pApClose(id);
	}
	
}
//----------------------------------------------------------------------------------------


void Bookmark(char *pPdf, char *pImage)
{
	int beginpage;
	int pagecount;
	long id;
	char szBookmark[64];

	if( (id=pApCreate(pPdf)) >0 )
	{
		//get multi page image page count
		pagecount=pApGetFunction(id,Ap_Get_ImagePageCount,0,0,pImage,0);
		for(beginpage=1; beginpage <= pagecount; beginpage++){
			//add bookmark
			sprintf(szBookmark,"bookmark %d",beginpage);
			pApSetFunction(id,Ap_Set_BookMark,0,0,szBookmark,0);
			//Add page
			pApSetFunction(id,Ap_Set_MultiPage,beginpage,1,0,0);			
			pApAddImage(id,pImage);
		}
		pApClose(id);
	}

}

//----------------------------------------------------------------------------------------

void ViewerPreferences(char *pPdf, char *pImage)
{
	long id;

	if( (id=pApCreate(pPdf)) >0 )
	{
		pApSetFunction(id,Ap_Set_ViewerPreferences,4+4096,2,0,0);
		pApAddImage(id,pImage);
		pApClose(id);
	}
	
}

//----------------------------------------------------------------------------------------


void TextAndGraphics(char *pPdf)
{

	long id,width,height;
	char string[]="this is a text! this is a text! this is a text! this is a text!";

	if( (id=pApCreate(pPdf)) >0 )
	{
		//create a page for a3
		pApSetFunction(id,Ap_Set_NewOrClosePage,1,1,0,0);
		//set system font and size
		pApSetFunction(id,Ap_Set_Font,1,32,"Arial",0);
		//add text
		pApAddTextEx(id,300,300,"this is a text!",RGB(0,255,255));

		//set system font and size
		pApSetFunction(id,Ap_Set_Font,1,32,"Helvetica",0);
		//add text
		pApAddTextEx(id,300,400,"this is a text!",RGB(0,255,255));

		//set adobe font and size
		pApSetFunction(id,Ap_Set_Font,203,32,0,0);
		//add text with rectangle
		width =400;
		height=200;
		pApAddText(id,100,50,width,height,string,RGB(0,0,255),RGB(255,0,255),Ap_DT_WORDBREAK|Ap_DT_CENTER); 

		//set system font and size
		pApSetFunction(id,Ap_Set_Font,1,32,"Verdana",0);
		//add text
		pApAddTextEx(id,300,500,"this is a text!",RGB(0,255,255));

		//create a page for b5
		pApSetFunction(id,Ap_Set_NewOrClosePage,4,4,0,0);
		//add graphic
		pApAddLine  (id,10,10,100,200,5,RGB(0,0,255));
		pApAddRect(id,20,210,300,400,3,RGB(0,255,0),1,RGB(255,0,255));
		//close page
		pApSetFunction(id,Ap_Set_NewOrClosePage,0,0,0,0);

		//add a txt file
		pApSetFunction(id,Ap_Set_Font,200,16,0,0);
		pApAddTxt(id,"test.txt",0,0,0,1,1,64,64,64,64,4);

		//add memory image 
		AddImageData(id);

		pApClose(id);
	}

}



//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
	char szMultiPage[MaxPath];
	char szBlackWhite[MaxPath];
	char szGray[MaxPath];
	char szTrueColor[MaxPath];
	char szSkewcorrect[MaxPath];
	char szJpg[MaxPath];
	char szBmp[MaxPath];
	char szGif[MaxPath];
	char szEmf[MaxPath];
	char szWmf[MaxPath];
	char szDcx[MaxPath];
	char szPcx[MaxPath];
	char szPsd[MaxPath];
	char *input[20];
	char path[MaxPath];

	GetModulePath(szMultiPage,"MultiPage.tif");
	GetModulePath(szBlackWhite,"BlackWhite.tif");
	GetModulePath(szGray,"Gray.tif");
	GetModulePath(szTrueColor,"TrueColor.tif");
	GetModulePath(szSkewcorrect,"Skewcorrect.tif");
	GetModulePath(szJpg,"test.jpg");
	GetModulePath(szBmp,"test.bmp");
	GetModulePath(szGif,"test.gif");
	GetModulePath(szEmf,"test.emf");
	GetModulePath(szWmf,"test.wmf");
	GetModulePath(szDcx,"test.dcx");
	GetModulePath(szPcx,"test.pcx");
	GetModulePath(szPsd,"test.psd");

	GetModulePath(path,"imagetopdf.dll");
	if(!LoadApWDll(path))
		return ;

	GetModulePath(path,"One2Multiple1");
	One2Multiple(path,szMultiPage);
	GetModulePath(path,"One2Multiple2");
	One2Multiple(path,szDcx);
	
	GetModulePath(path,"One2One1.pdf");
	One2One(path,szMultiPage);
	GetModulePath(path,"One2One2.pdf");
	One2One(path,szDcx);

	input[0] = szBlackWhite;
	input[1] = szGray;
	input[2] = szTrueColor;
	input[3] = szMultiPage;
	input[4] = szJpg;
	input[5] = szBmp;
	input[6] = szGif;
	input[7] = szEmf;
	input[8] = szWmf;
	input[9] =szDcx;
	input[10] =szPcx;
	input[11] =szPsd;

	GetModulePath(path,"Multiple2One.pdf");
	Multiple2One(path,input,12);

	GetModulePath(path,"Append.pdf");
	AppendPDF(path,input,2);

	//re-compress image files to PDFs
	GetModulePath(path,"BlackWhite");
	Recompress(path,szBlackWhite);
	GetModulePath(path,"Gray");
	Recompress(path,szGray);
	GetModulePath(path,"TrueColor");
	Recompress(path,szTrueColor);

	GetModulePath(path,"Skewcorrect.pdf");
	Skewcorrect(path,szSkewcorrect);

	GetModulePath(path,"PageSize.pdf");
	PageSize(path,szTrueColor);

	GetModulePath(path,"PageLayout.pdf");
	PageLayout(path,input,12);

	GetModulePath(path,"PageOrientation");
	PageOrientation(path,szMultiPage);

	GetModulePath(path,"Watermark.pdf");
	Watermark(path,input,12);

	GetModulePath(path,"Security.pdf");
	PDFsecurity(path,szTrueColor);

	GetModulePath(path,"Information.pdf");
	PDFInfo(path,szTrueColor);

	GetModulePath(path,"Bookmark.pdf");
	Bookmark(path,szMultiPage);

	GetModulePath(path,"ViewerPreferences.pdf");
	ViewerPreferences(path,szTrueColor);
	
	GetModulePath(path,"TextAndGraphics.pdf");
	TextAndGraphics(path);

	FreeApWDll();

	return ;
}
//---------------------------------------------------------------------------
 