<%@ page contentType="text/html; charset=windows-1252" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="com.jacob.com.*" %>
<%@ page import="com.jacob.activeX.*" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>JSP Example</title>
</head>

<body>
<%

	//------------------------------------------------------------------------------------------
	//defined for ApSetFunction
	final long Ap_Set_MultiPage = 101;
	final long Ap_Set_ImagePosAndSize = 104;
	final long Ap_Set_Encrypt = 105;
	final long Ap_Set_Font = 106;
	final long Ap_Set_NewOrClosePage = 109;
	final long Ap_Set_BookMark = 110;
	final long Ap_Set_ViewerPreferences = 121;
	final long Ap_Set_Orientation = 122;
	final long Ap_Set_Recompress = 123;
	final long Ap_Set_Skewcorrect = 126;
	final long Ap_Set_JpegQuality = 128;
	
	//defined for ApGetFunction
	final long Ap_Get_ImagePageCount = 201;
	final long Ap_Get_ImageSizeW = 202;
	final long Ap_Get_ImageSizeH = 203;
	final long Ap_Get_ImageDpiX = 204;
	final long Ap_Get_ImageDpiY = 205;
	final long Ap_Get_PdfPageCount = 206;
	final long Ap_Get_PdfCurrentPageSize = 209;
	final long Ap_Get_StringWidth = 250;
	final long Ap_Get_StringHeight = 251;
	final long Ap_Get_Recomporess = 210;
	final long Ap_Get_ImageBits = 211;
	
	//defined for ApAddText
	final long Ap_DT_LEFT = 0;
	final long Ap_DT_CENTER = 1;
	final long Ap_DT_RIGHT = 2;
	final long Ap_DT_WORDBREAK = 0x10;
	final long Ap_DT_CALCRECT = 0x400;
	
	//defined permission
	final long permission_Print = 1;
	final long permission_Copy = 2;
	final long permission_Modify = 4;
	//------------------------------------------------------------------------------------------
	
	System.runFinalizersOnExit(true);
	ActiveXComponent mf = new ActiveXComponent("ImagetopdfCOM.imagetopdf");
	Object myCom = mf.getObject();
	
	out.println("Create Object Success!  <BR>");
	
	String  pPdf,pImage,szPDFFile,szBookmark,szString;
	String  pImages[] = new String[20];
	Variant id;
	Variant params2[] = new Variant[2];
	Variant params5[] = new Variant[5];
	Variant params6[] = new Variant[6];
	Variant params7[] = new Variant[7];
	Variant params9[] = new Variant[9];
	Variant params12[] = new Variant[12];
	
	int beginPage,i,length,nPos,nOrientation,nPosX,nPosY;
	Variant pageCount;
	Variant ImageWidth,ImageHeight,xdpi,ydpi;
	int ImageWidth2,ImageHeight2,PageWidth,PageHeight;
	float zw,zh,fZoom;	
	
	
	

	//------------------------------------------------------------------------------------------
	//One2Multiple (split image file)
	//------------------------------------------------------------------------------------------
	out.println("One2Multiple.  <BR>");
	pPdf = "d:\\test\\One2Multiple";
	pImage = "d:\\test\\MultiPage.tif";
	
	//get multipage image page count
	params6[0] = new Variant("0");
	params6[1] = new Variant(Long.toString(Ap_Get_ImagePageCount));
	params6[2] = new Variant("0");
	params6[3] = new Variant("0");
	params6[4] = new Variant(pImage);
	params6[5] = new Variant("0");
	pageCount = mf.invoke("ApcGetFunction", params6);
	for( beginPage = 1;  beginPage <= pageCount.toInt(); beginPage++){
		szPDFFile = pPdf + Long.toString(beginPage) + ".pdf";
		id = Dispatch.call(myCom,"ApcCreate",new Variant(szPDFFile));
		if( id.toInt() > 0 ){
			params6[0] = new Variant(id.toString());
			params6[1] = new Variant(Long.toString(Ap_Set_MultiPage));
			params6[2] = new Variant(Long.toString(beginPage));
			params6[3] = new Variant("1");
			params6[4] = new Variant("0");
			params6[5] = new Variant("0");
			mf.invoke("ApcSetFunction", params6);
			params2[0] = new Variant(id.toString());
			params2[1] = new Variant(pImage);
			mf.invoke("ApcAddImage", params2);			
			Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));			
		}
	}

	//------------------------------------------------------------------------------------------
	//One2One
	//------------------------------------------------------------------------------------------
	out.println( "One2One(1).  <BR>");
	pPdf = "d:\\test\\One2One1.pdf";
	pImage = "d:\\test\\MultiPage.tif";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			     
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));	
	}
	out.println( "One2One(2).  <BR>");
	pPdf = "d:\\test\\One2One2.pdf";
	pImage = "d:\\test\\test.dcx";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			     
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));	
	}

	//------------------------------------------------------------------------------------------
	//Multiple2One (merge image file)
	//------------------------------------------------------------------------------------------
	out.println( "Multiple2One.  <BR>");
	pPdf = "d:\\test\\Multiple2One.pdf";
	pImages[0] = "d:\\test\\MultiPage.tif";
	pImages[1] = "d:\\test\\BlackWhite.tif";
	pImages[2] = "d:\\test\\Gray.tif";
	pImages[3] = "d:\\test\\TrueColor.tif";
	pImages[4] = "d:\\test\\test.jpg";
	pImages[5] = "d:\\test\\test.bmp";
	pImages[6] = "d:\\test\\test.gif";
	pImages[7] = "d:\\test\\test.emf";
	pImages[8] = "d:\\test\\test.wmf";
	pImages[9] = "d:\\test\\test.dcx";
	pImages[10] = "d:\\test\\test.pcx";
	pImages[11] = "d:\\test\\test.psd";
	length = 12;

	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
      for( i = 0 ; i<length; i++){
 		 params2[0] = new Variant(id.toString());
		 params2[1] = new Variant(pImages[i]);
		 mf.invoke("ApcAddImage", params2);			     
      } 
      Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}
	
	//------------------------------------------------------------------------------------------
	//AppendPDF	
	//------------------------------------------------------------------------------------------
	out.println( "AppendPDF.  <BR>");
 	pPdf = "d:\\test\\Append.pdf";
	for( i = 0; i<2; i++){
		id = Dispatch.call(myCom,"ApcOpen",new Variant(pPdf));
      	if( id.toInt() <= 0 ){
        	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
      	}
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImages[i]);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	} 	


	//------------------------------------------------------------------------------------------
	//RecompressTruecolorFile
	//------------------------------------------------------------------------------------------	
	out.println( "RecompressTruecolorFile.  <BR>");
	pImage = "d:\\test\\TrueColor.tif";  
	
	//recompress lzw	
	pPdf = "d:\\test\\TrueColor_lzw.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Recompress));
		params6[2] = new Variant("3");
		params6[3] = new Variant("0");
		params6[4] = new Variant("lzw");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}
  
	//recompress zip
	pPdf = "d:\\test\\TrueColor_zip.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Recompress));
		params6[2] = new Variant("3");
		params6[3] = new Variant("0");
		params6[4] = new Variant("zip");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}
	  
	//recompress packbits
	pPdf = "d:\\test\\TrueColor_packbits.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Recompress));
		params6[2] = new Variant("3");
		params6[3] = new Variant("0");
		params6[4] = new Variant("packbits");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}
	  
	//recompress jpeg,quality=100
	pPdf = "d:\\test\\TrueColor__jpeg_100.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Recompress));
		params6[2] = new Variant("3");
		params6[3] = new Variant("0");
		params6[4] = new Variant("jpeg");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_JpegQuality));
		params6[2] = new Variant("100");
		params6[3] = new Variant("0");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}
	  
	//recompress jpeg,quality=50
	pPdf = "d:\\test\\TrueColor__jpeg_50.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Recompress));
		params6[2] = new Variant("3");
		params6[3] = new Variant("0");
		params6[4] = new Variant("jpeg");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_JpegQuality));
		params6[2] = new Variant("50");
		params6[3] = new Variant("0");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}

	//------------------------------------------------------------------------------------------
	//RecompressBlackWhiteFile
	//------------------------------------------------------------------------------------------	
	out.println( "RecompressBlackWhiteFile.  <BR>");
	pImage = "d:\\test\\BlackWhite.tif";  
	
	//recompress lzw	
	pPdf = "d:\\test\\BlackWhite_lzw.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Recompress));
		params6[2] = new Variant("1");
		params6[3] = new Variant("0");
		params6[4] = new Variant("lzw");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}
  
	//recompress zip
	pPdf = "d:\\test\\BlackWhite_zip.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Recompress));
		params6[2] = new Variant("1");
		params6[3] = new Variant("0");
		params6[4] = new Variant("zip");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}
	  
	//recompress g3
	pPdf = "d:\\test\\BlackWhite_g3.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Recompress));
		params6[2] = new Variant("1");
		params6[3] = new Variant("0");
		params6[4] = new Variant("g3");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}
	  
	//recompress g4
	pPdf = "d:\\test\\BlackWhite_g4.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Recompress));
		params6[2] = new Variant("1");
		params6[3] = new Variant("0");
		params6[4] = new Variant("g4");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}
	  
	//------------------------------------------------------------------------------------------
	//Skewcorrect
	//------------------------------------------------------------------------------------------	
	out.println("Skewcorrect.  <BR>");
	pPdf = "d:\\test\\Skewcorrect.pdf";
	pImage = "d:\\test\\Skewcorrect.tif";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){           
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	//set skewcorrect
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Skewcorrect));
		params6[2] = new Variant("1");
		params6[3] = new Variant("0");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);      
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}

	//------------------------------------------------------------------------------------------
	//PageSize
	//------------------------------------------------------------------------------------------	
	out.println("PageSize.  <BR>");	
	pPdf = "d:\\test\\PageSize.pdf";
	pImage = "d:\\test\\TrueColor.tif";	

	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){           	  
      	//Get image width and height
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Get_ImageSizeW));
		params6[2] = new Variant("0");
		params6[3] = new Variant("0");
		params6[4] = new Variant(pImage);
		params6[5] = new Variant("1");
		ImageWidth = mf.invoke("ApcGetFunction", params6);    
		params6[1] = new Variant(Long.toString(Ap_Get_ImageSizeH));
		ImageHeight = mf.invoke("ApcGetFunction", params6);    
      	//Get image dpi (Resolution)
		params6[1] = new Variant(Long.toString(Ap_Get_ImageDpiX));
		xdpi = mf.invoke("ApcGetFunction", params6);    
		params6[1] = new Variant(Long.toString(Ap_Get_ImageDpiY));
		ydpi = mf.invoke("ApcGetFunction", params6);    

      	//keep image dpi (resolution)
      	ImageWidth2 = (int)(ImageWidth.toFloat() * (72.0 / xdpi.toFloat()));
      	ImageHeight2 = (int)(ImageHeight.toFloat() * (72.0 / ydpi.toFloat()));     
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_NewOrClosePage));
		params6[2] = new Variant(Long.toString(ImageWidth2));
		params6[3] = new Variant(Long.toString(ImageHeight2));
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);    
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);			          		

      	//page size with image size
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_NewOrClosePage));
		params6[2] = new Variant(Long.toString(ImageWidth.toInt()));
		params6[3] = new Variant(Long.toString(ImageHeight.toInt()));
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);    
		mf.invoke("ApcAddImage", params2);	
	
      	//A4
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_NewOrClosePage));
		params6[2] = new Variant("2");
		params6[3] = new Variant("2");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);    
		params6[1] = new Variant(Long.toString(Ap_Set_ImagePosAndSize));
		params6[2] = new Variant("0");
		params6[3] = new Variant("0");
		params6[4] = new Variant(Long.toString(ImageWidth.toInt()));
		params6[5] = new Variant(Long.toString(ImageHeight.toInt()));
		mf.invoke("ApcSetFunction", params6);    
		mf.invoke("ApcAddImage", params2);	
		
      	//custom page size and fit width/height
      	PageWidth = 768;
      	PageHeight = 1024;
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_NewOrClosePage));
		params6[2] = new Variant(Long.toString(PageWidth));
		params6[3] = new Variant(Long.toString(PageHeight));
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6); 	
		
      	zw = (float)(PageWidth / ImageWidth.toFloat());
      	zh = (float)(PageHeight / ImageHeight.toFloat());
      	if(zw < zh){ 
			fZoom = zw;
      	}else{
			fZoom = zh;
	  	}
      	ImageWidth2 = (int)(ImageWidth.toFloat() * fZoom);
      	ImageHeight2 = (int)(ImageHeight.toFloat() * fZoom);
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_ImagePosAndSize));
		params6[2] = new Variant(Long.toString((PageWidth - ImageWidth2)/2) );
		params6[3] = new Variant(Long.toString((PageHeight - ImageHeight2)/2));
		params6[4] = new Variant(Long.toString(ImageWidth2));
		params6[5] = new Variant(Long.toString(ImageHeight2));
		mf.invoke("ApcSetFunction", params6); 	
		mf.invoke("ApcAddImage", params2);	
					          
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));
	}

	//------------------------------------------------------------------------------------------
	//PageLayout
	//------------------------------------------------------------------------------------------	
	out.println("PageLayout. <BR>");
	pPdf = "d:\\test\\PageLayout.pdf";
	nPos = 1;
	PageWidth = 768;
	PageHeight = 1024;

	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){           	  
      	for( i = 0 ; i< length; i++){    
          	//get pages
			params6[0] = new Variant(id.toString());
			params6[1] = new Variant(Long.toString(Ap_Get_ImagePageCount));
			params6[2] = new Variant("0");
			params6[3] = new Variant("0");
			params6[4] = new Variant(pImages[i]);
			params6[5] = new Variant("0");
			pageCount = mf.invoke("ApcGetFunction", params6); 
			       
          	for( beginPage = 1 ; beginPage <= pageCount.toInt(); beginPage++){          
              	switch( nPos % 4 ){
              	case 1:
              		//new page
					params6[0] = new Variant(id.toString());
					params6[1] = new Variant(Long.toString(Ap_Set_NewOrClosePage));
					params6[2] = new Variant(Long.toString(PageWidth));
					params6[3] = new Variant(Long.toString(PageHeight));
					params6[4] = new Variant("0");
					params6[5] = new Variant("0");
					mf.invoke("ApcSetFunction", params6);  
					//set image posion
					params6[1] = new Variant(Long.toString(Ap_Set_ImagePosAndSize));
					params6[2] = new Variant("0");
					params6[3] = new Variant("0");
					params6[4] = new Variant(Long.toString(PageWidth / 2));
					params6[5] = new Variant(Long.toString(PageHeight / 2));
					mf.invoke("ApcSetFunction", params6);  
					params6[1] = new Variant(Long.toString(Ap_Set_MultiPage));
					params6[2] = new Variant(Long.toString(beginPage));
					params6[3] = new Variant("1");
					params6[4] = new Variant("0");
					params6[5] = new Variant("0");
					mf.invoke("ApcSetFunction", params6);  
			  		params2[0] = new Variant(id.toString());
					params2[1] = new Variant(pImages[i]);
					mf.invoke("ApcAddImage", params2);
					break;
              	case 2:
					params6[1] = new Variant(Long.toString(Ap_Set_ImagePosAndSize));
					params6[2] = new Variant(Long.toString(PageWidth / 2));
					params6[3] = new Variant("0");
					params6[4] = new Variant(Long.toString(PageWidth / 2));
					params6[5] = new Variant(Long.toString(PageHeight / 2));
					mf.invoke("ApcSetFunction", params6);  
					params6[1] = new Variant(Long.toString(Ap_Set_MultiPage));
					params6[2] = new Variant(Long.toString(beginPage));
					params6[3] = new Variant("1");
					params6[4] = new Variant("0");
					params6[5] = new Variant("0");
					mf.invoke("ApcSetFunction", params6);  
			  		params2[0] = new Variant(id.toString());
					params2[1] = new Variant(pImages[i]);
					mf.invoke("ApcAddImage", params2);
					break;
              	case 3:
					params6[1] = new Variant(Long.toString(Ap_Set_ImagePosAndSize));
					params6[2] = new Variant("0");
					params6[3] = new Variant(Long.toString(PageHeight / 2));
					params6[4] = new Variant(Long.toString(PageWidth / 2));
					params6[5] = new Variant(Long.toString(PageHeight / 2));
					mf.invoke("ApcSetFunction", params6);  
					params6[1] = new Variant(Long.toString(Ap_Set_MultiPage));
					params6[2] = new Variant(Long.toString(beginPage));
					params6[3] = new Variant("1");
					params6[4] = new Variant("0");
					params6[5] = new Variant("0");
					mf.invoke("ApcSetFunction", params6);  
			  		params2[0] = new Variant(id.toString());
					params2[1] = new Variant(pImages[i]);
					mf.invoke("ApcAddImage", params2);
					break;
              	case 0:
					params6[1] = new Variant(Long.toString(Ap_Set_ImagePosAndSize));
					params6[2] = new Variant(Long.toString(PageWidth / 2));
					params6[3] = new Variant(Long.toString(PageHeight / 2));
					params6[4] = new Variant(Long.toString(PageWidth / 2));
					params6[5] = new Variant(Long.toString(PageHeight / 2));
					mf.invoke("ApcSetFunction", params6);  
					params6[1] = new Variant(Long.toString(Ap_Set_MultiPage));
					params6[2] = new Variant(Long.toString(beginPage));
					params6[3] = new Variant("1");
					params6[4] = new Variant("0");
					params6[5] = new Variant("0");
					mf.invoke("ApcSetFunction", params6);  
			  		params2[0] = new Variant(id.toString());
					params2[1] = new Variant(pImages[i]);
					mf.invoke("ApcAddImage", params2);
					//close page
					params6[1] = new Variant(Long.toString(Ap_Set_NewOrClosePage));
					params6[2] = new Variant("0");
					params6[3] = new Variant("0");
					params6[4] = new Variant("0");
					params6[5] = new Variant("0");
					mf.invoke("ApcSetFunction", params6);  
					break;
				}                      
              	nPos = nPos + 1;
          	} 
      	} 
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));    
	}
	
	//------------------------------------------------------------------------------------------
	//PageOrientation
	//------------------------------------------------------------------------------------------	
	out.println( "PageOrientation. <BR>");
	pImage = "d:\\test\\MultiPage.tif";
	  
	nOrientation = 90;
	pPdf = "d:\\test\\PageOrientation_90.pdf";	
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){ 
		params6[0] = new Variant(id.toString());          	  
		params6[1] = new Variant(Long.toString(Ap_Set_Orientation));
		params6[2] = new Variant(Long.toString(nOrientation));
		params6[3] = new Variant("0");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);  
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);
		Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));    		
	}

	nOrientation = 180;
	pPdf = "d:\\test\\PageOrientation_180.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){ 
		params6[0] = new Variant(id.toString());          	  
		params6[1] = new Variant(Long.toString(Ap_Set_Orientation));
		params6[2] = new Variant(Long.toString(nOrientation));
		params6[3] = new Variant("0");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);  
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);
		Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));    		
	}

	nOrientation = 270;
	pPdf = "d:\\test\\PageOrientation_270.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){ 
		params6[0] = new Variant(id.toString());          	  
		params6[1] = new Variant(Long.toString(Ap_Set_Orientation));
		params6[2] = new Variant(Long.toString(nOrientation));
		params6[3] = new Variant("0");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);  
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);
		Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));    		
	}

	//------------------------------------------------------------------------------------------
	//Watermark
	//------------------------------------------------------------------------------------------	
	out.println( "Watermark. <BR>");
	pPdf = "d:\\test\\Watermark.pdf";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){ 
      	for( i = 0 ; i< length; i++){  
          	//get pages
			params6[0] = new Variant(id.toString());
			params6[1] = new Variant(Long.toString(Ap_Get_ImagePageCount));
			params6[2] = new Variant("0");
			params6[3] = new Variant("0");
			params6[4] = new Variant(pImages[i]);
			params6[5] = new Variant("0");
			pageCount = mf.invoke("ApcGetFunction", params6); 
			       
          	for( beginPage = 1 ; beginPage <= pageCount.toInt(); beginPage++){          
		      	//Get image width and height
				params6[0] = new Variant(id.toString());
				params6[1] = new Variant(Long.toString(Ap_Get_ImageSizeW));
				params6[2] = new Variant("0");
				params6[3] = new Variant("0");
				params6[4] = new Variant(pImages[i]);
				params6[5] = new Variant(Long.toString(beginPage));
				ImageWidth = mf.invoke("ApcGetFunction", params6);    
				params6[1] = new Variant(Long.toString(Ap_Get_ImageSizeH));
				ImageHeight = mf.invoke("ApcGetFunction", params6); 
				   
          		//new page
				params6[0] = new Variant(id.toString());
				params6[1] = new Variant(Long.toString(Ap_Set_NewOrClosePage));
				params6[2] = new Variant(Long.toString(ImageWidth.toInt()));
				params6[3] = new Variant(Long.toString(ImageHeight.toInt()));
				params6[4] = new Variant("0");
				params6[5] = new Variant("0");
				mf.invoke("ApcSetFunction", params6);  
				//set image posion
				params6[1] = new Variant(Long.toString(Ap_Set_ImagePosAndSize));
				params6[2] = new Variant("0");
				params6[3] = new Variant("0");
				params6[4] = new Variant(Long.toString(ImageWidth.toInt()));
				params6[5] = new Variant(Long.toString(ImageHeight.toInt()));
				mf.invoke("ApcSetFunction", params6);  
				params6[1] = new Variant(Long.toString(Ap_Set_MultiPage));
				params6[2] = new Variant(Long.toString(beginPage));
				params6[3] = new Variant("1");
				params6[4] = new Variant("0");
				params6[5] = new Variant("0");
				mf.invoke("ApcSetFunction", params6);  
		  		params2[0] = new Variant(id.toString());
				params2[1] = new Variant(pImages[i]);
				mf.invoke("ApcAddImage", params2);
			
				//set system font and size
				params6[1] = new Variant(Long.toString(Ap_Set_Font));
				params6[2] = new Variant("1");
				params6[3] = new Variant("32");
				params6[4] = new Variant("Arial");
				params6[5] = new Variant("0");
				mf.invoke("ApcSetFunction", params6);  
				//add text
				nPosX = 10;
				nPosY = (int)(ImageHeight.toFloat() / 2);
				params5[0] = new Variant(id.toString());
				params5[1] = new Variant(Long.toString(nPosX));
				params5[2] = new Variant(Long.toString(nPosY));
				params5[3] = new Variant("(c)Copyright XXXXXXXXXX");
				params5[4] = new Variant(Long.toString(16711680));
				mf.invoke("ApcAddTextEx", params5);  
			
				//close page
				params6[1] = new Variant(Long.toString(Ap_Set_NewOrClosePage));
				params6[2] = new Variant("0");
				params6[3] = new Variant("0");
				params6[4] = new Variant("0");
				params6[5] = new Variant("0");
				mf.invoke("ApcSetFunction", params6);  
          
          	} 
      	} 
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));       
	}


	//------------------------------------------------------------------------------------------
	//PDFsecurity
	//------------------------------------------------------------------------------------------	
	out.println( "PDFsecurity. <BR>");
	pPdf = "d:\\test\\Security.pdf";
	pImage = "d:\\test\\TrueColor.tif";
	
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){ 
      	//set password and permission
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_Encrypt));
		params6[2] = new Variant("128");
		params6[3] = new Variant(Long.toString(permission_Print + permission_Copy + permission_Modify));
		params6[4] = new Variant("owner");
		params6[5] = new Variant("user");
		mf.invoke("ApcSetFunction", params6);  
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString())); 
	}


	//------------------------------------------------------------------------------------------
	//PDFInfo
	//------------------------------------------------------------------------------------------	
	out.println( "PDFInfo. <BR>");
	pPdf = "d:\\test\\Information.pdf";
	pImage = "d:\\test\\TrueColor.tif";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){       
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant("Title");
		params6[2] = new Variant("Subject");
		params6[3] = new Variant("Author");
		params6[4] = new Variant("Keywords");
		params6[5] = new Variant("Creator");
		mf.invoke("ApcAddInfo", params6);  
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);
		
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString())); 
	}

	//------------------------------------------------------------------------------------------
	//Bookmark
	//------------------------------------------------------------------------------------------	
	out.println( "Bookmark. <BR>");
	pPdf = "d:\\test\\Bookmark.pdf";
	pImage = "d:\\test\\MultiPage.tif";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){   
      	//get multi page image page count
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Get_ImagePageCount));
		params6[2] = new Variant("0");
		params6[3] = new Variant("0");
		params6[4] = new Variant(pImage);
		params6[5] = new Variant("0");
		pageCount = mf.invoke("ApcGetFunction", params6);

      	for( beginPage = 1 ; beginPage<= pageCount.toInt(); beginPage++){
          	//add bookmark
          	szBookmark = "bookmark " + Long.toString(beginPage);
          
			params6[0] = new Variant(id.toString());
			params6[1] = new Variant(Long.toString(Ap_Set_BookMark));
			params6[2] = new Variant("0");
			params6[3] = new Variant("0");
			params6[4] = new Variant(szBookmark);
			params6[5] = new Variant("0");
			mf.invoke("ApcSetFunction", params6);  

          	//Add page
			params6[0] = new Variant(id.toString());
			params6[1] = new Variant(Long.toString(Ap_Set_MultiPage));
			params6[2] = new Variant(Long.toString(beginPage));
			params6[3] = new Variant("1");
			params6[4] = new Variant("0");
			params6[5] = new Variant("0");
			mf.invoke("ApcSetFunction", params6);  
	  		params2[0] = new Variant(id.toString());
			params2[1] = new Variant(pImage);
			mf.invoke("ApcAddImage", params2);
      	} 
      
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString()));   
	}

	//------------------------------------------------------------------------------------------
	//ViewerPreferences
	//------------------------------------------------------------------------------------------	
	out.println( "ViewerPreferences. <BR>");
	pPdf = "d:\\test\\ViewerPreferences.pdf";
	pImage = "d:\\test\\TrueColor.tif";
	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){ 
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_ViewerPreferences));
		params6[2] = new Variant(Long.toString(4 + 4096));
		params6[3] = new Variant("2");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);  
  		params2[0] = new Variant(id.toString());
		params2[1] = new Variant(pImage);
		mf.invoke("ApcAddImage", params2);		
      	Dispatch.call(myCom,"ApcClose",new Variant(id.toString())); 
	}
	
	//------------------------------------------------------------------------------------------
	//TextAndGraphics
	//------------------------------------------------------------------------------------------	
	out.println( "TextAndGraphics. <BR>");
	pPdf = "d:\\test\\TextAndGraphics.pdf";
	szString = "this is a text! this is a text! this is a text! this is a text!";

	id = Dispatch.call(myCom,"ApcCreate",new Variant(pPdf));
	if( id.toInt() > 0 ){ 
		//create a page for a3
		params6[0] = new Variant(id.toString());
		params6[1] = new Variant(Long.toString(Ap_Set_NewOrClosePage));
		params6[2] = new Variant("1");
		params6[3] = new Variant("1");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);  
		//set system font and size
		params6[1] = new Variant(Long.toString(Ap_Set_Font));
		params6[2] = new Variant("1");
		params6[3] = new Variant("32");
		params6[4] = new Variant("Arial");
		mf.invoke("ApcSetFunction", params6);  
		//add text
		params5[0] = new Variant(id.toString());
		params5[1] = new Variant("300");
		params5[2] = new Variant("300");
		params5[3] = new Variant("this is a text!");
		params5[4] = new Variant(Long.toString(16776960));
		mf.invoke("ApcAddTextEx", params5);  
		
		//set system font and size
		params6[4] = new Variant("Helvetica");
		mf.invoke("ApcSetFunction", params6);  
		//add text
		params5[1] = new Variant("300");
		params5[2] = new Variant("400");
		mf.invoke("ApcAddTextEx", params5);  
		
		//set adobe font and size
		params6[2] = new Variant("203");
		params6[3] = new Variant("32");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");		
		mf.invoke("ApcSetFunction", params6);  
		//add text with rectangle 
		ImageWidth2 = 400;
		ImageHeight2 = 200;
		params9[0] = new Variant(id.toString());
		params9[1] = new Variant("100");
		params9[2] = new Variant("50");
		params9[3] = new Variant(Long.toString(ImageWidth2));
		params9[4] = new Variant(Long.toString(ImageHeight2));
		params9[5] = new Variant(szString);
		params9[6] = new Variant("255");
		params9[7] = new Variant("16711935");
		params9[8] = new Variant(Long.toString(Ap_DT_WORDBREAK + Ap_DT_CENTER));
		mf.invoke("ApcAddText", params9);  
		
		//set system font and size
		params6[1] = new Variant(Long.toString(Ap_Set_Font));
		params6[2] = new Variant("1");
		params6[3] = new Variant("32");
		params6[4] = new Variant("Verdana");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);  
		//add text
		params5[1] = new Variant("300");
		params5[2] = new Variant("500");
		mf.invoke("ApcAddTextEx", params5);  
		
		//create a page for b5
		params6[1] = new Variant(Long.toString(Ap_Set_NewOrClosePage));
		params6[2] = new Variant("4");
		params6[3] = new Variant("4");
		params6[4] = new Variant("0");
		params6[5] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);  
		//add graphic
		params7[0] = new Variant(id.toString());
		params7[1] = new Variant("10");
		params7[2] = new Variant("10");
		params7[3] = new Variant("100");
		params7[4] = new Variant("200");
		params7[5] = new Variant("5");
		params7[6] = new Variant("255");
		mf.invoke("ApcAddLine", params7);  
		params9[0] = new Variant(id.toString());
		params9[1] = new Variant("20");
		params9[2] = new Variant("210");
		params9[3] = new Variant("300");
		params9[4] = new Variant("400");
		params9[5] = new Variant("3");
		params9[6] = new Variant("65280");
		params9[7] = new Variant("1");
		params9[8] = new Variant("16711935");
		mf.invoke("ApcAddRect", params9);  

		//close page
		params6[2] = new Variant("0");
		params6[3] = new Variant("0");
		mf.invoke("ApcSetFunction", params6);  
		
		//add a txt file
		params6[1] = new Variant(Long.toString(Ap_Set_Font));			
		params6[2] = new Variant("200");
		params6[3] = new Variant("16");
		mf.invoke("ApcSetFunction", params6); 
		 
		params12[0] = new Variant(id.toString());
		params12[1] = new Variant("d:\\test\\test.txt");
		params12[2] = new Variant("0");
		params12[3] = new Variant("0");
		params12[4] = new Variant("0");
		params12[5] = new Variant("1");
		params12[6] = new Variant("1");
		params12[7] = new Variant("64");
		params12[8] = new Variant("64");
		params12[9] = new Variant("64");
		params12[10] = new Variant("64");
		params12[11] = new Variant("4");
		mf.invoke("ApcAddTxt", params12);  
		
		Dispatch.call(myCom,"ApcClose",new Variant(id.toString())); 
  
	}
	
	
	//------------------------------------------------------------------------------------------
	out.println("Convert  Success!<BR>");

%>

</body>
</html>
