<%@ Page Language="VB" aspcompat=true  %>

<%

	'------------------------------------------------------------------------------------------
	'defined for ApSetFunction
	Const Ap_Set_MultiPage = 101
	Const Ap_Set_ImagePosAndSize = 104
	Const Ap_Set_Encrypt = 105
	Const Ap_Set_Font = 106
	Const Ap_Set_NewOrClosePage = 109
	Const Ap_Set_BookMark = 110
	Const Ap_Set_ViewerPreferences = 121
	Const Ap_Set_Orientation = 122
	Const Ap_Set_Recompress = 123
	Const Ap_Set_Skewcorrect = 126
	Const Ap_Set_JpegQuality = 128
	
	'defined for ApGetFunction
	Const Ap_Get_ImagePageCount = 201
	Const Ap_Get_ImageSizeW = 202
	Const Ap_Get_ImageSizeH = 203
	Const Ap_Get_ImageDpiX = 204
	Const Ap_Get_ImageDpiY = 205
	Const Ap_Get_PdfPageCount = 206
	Const Ap_Get_PdfCurrentPageSize = 209
	Const Ap_Get_StringWidth = 250
	Const Ap_Get_StringHeight = 251
	Const Ap_Get_Recomporess = 210
	Const Ap_Get_ImageBits = 211
	
	'defined for ApAddText
	Const Ap_DT_LEFT = &H0
	Const Ap_DT_CENTER = &H1
	Const Ap_DT_RIGHT = &H2
	Const Ap_DT_WORDBREAK = &H10
	Const Ap_DT_CALCRECT = &H400
	
	
	'defined permission
	Const permission_Print = 1
	Const permission_Copy = 2
	Const permission_Modify = 4
	'------------------------------------------------------------------------------------------

	Dim pImages(20) 
	dim oTest 
	dim id,ret,length,nPos,i,nPages,index ,nOrientation ,nPosX,nPosY As Integer
	dim width,height As Integer
	dim pPdf,pImage,szPDFFile,szBookmark,szString As String
	dim beginPage,pageCount As Integer
	dim PageWidth,PageHeight,ImageWidth,ImageHeight,xdpi,ydpi As Integer
	dim ImageWidth2 ,ImageHeight2 As Integer
	dim zw,zh,fZoom As Double 
	
	oTest = Server.CreateObject("ImagetopdfCOM.imagetopdf")
	Response.write("Create Object Success! <BR>")
	
	'------------------------------------------------------------------------------------------
	'One2Multiple (split image file)
	'------------------------------------------------------------------------------------------
	Response.write("One2Multiple <BR>")
	pPdf = "d:\test\One2Multiple"
	pImage = "d:\test\MultiPage.tif"
	'get multipage image page count
	pageCount = oTest.ApcGetFunction(0, Ap_Get_ImagePageCount, 0, 0, pImage, vbNull)
 
	For beginPage = 1 To pageCount
  
      szPDFFile = pPdf + CStr(beginPage) + ".pdf"
      id = oTest.ApcCreate(szPDFFile)
      If id > 0 Then
          ret = oTest.ApcSetFunction(id, Ap_Set_MultiPage, beginPage, 1, vbNull, vbNull)
          ret = oTest.ApcAddImage(id, pImage)
          oTest.ApcClose( id)
      End If
	Next 
  
	'------------------------------------------------------------------------------------------
	'One2One
	'------------------------------------------------------------------------------------------
	Response.write( "One2One (1)<BR>")
	pPdf = "d:\test\One2One1.pdf"
	pImage = "d:\test\MultiPage.tif"

	id = oTest.ApcCreate(pPdf)
	If id > 0 Then
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If

	Response.write( "One2One (2) <BR>")
	pPdf = "d:\test\One2One2.pdf"
	pImage = "d:\test\test.dcx"

	id = oTest.ApcCreate(pPdf)
	If id > 0 Then
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If
		
	'------------------------------------------------------------------------------------------
	'Multiple2One (merge image file)
	'------------------------------------------------------------------------------------------
	Response.write( "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 = oTest.ApcCreate(pPdf)
	If id > 0 Then
      For i = 0 To length-1    
          ret = oTest.ApcAddImage(id, pImages(i))
      Next 
      oTest.ApcClose( id)
	End If
	
	'------------------------------------------------------------------------------------------
	'AppendPDF	
	'------------------------------------------------------------------------------------------
	Response.write( "AppendPDF <BR>"	)
 	pPdf = "d:\test\Append.pdf"
	For i = 0 To 1
      id = oTest.ApcOpen(pPdf)
      If id <= 0 Then
          id = oTest.ApcCreate(pPdf)
      End If
      ret = oTest.ApcAddImage(id, pImages(i))      
      oTest.ApcClose( id)
	Next 
		
	'------------------------------------------------------------------------------------------
	'RecompressTruecolorFile
	'------------------------------------------------------------------------------------------	
	Response.write( "RecompressTruecolorFile <BR>"	)
	'recompress lzw
	pImage = "d:\test\TrueColor.tif"
	  
	pPdf = "d:\test\TrueColor_lzw.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Recompress, 3, 0, "lzw", vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If
  
	'recompress zip
	pPdf = "d:\test\TrueColor_zip.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Recompress, 3, 0, "zip", vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If
	  
	'recompress packbits
	pPdf = "d:\test\TrueColor_packbits.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Recompress, 3, 0, "packbits", vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If
	  
	'recompress jpeg,quality=100
	pPdf = "d:\test\TrueColor__jpeg_100.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Recompress, 3, 0, "jpeg", vbNull)
      ret = oTest.ApcSetFunction(id, Ap_Set_JpegQuality, 100, 0, vbNull, vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If
	  
	'recompress jpeg,quality=50
	pPdf = "d:\test\TrueColor__jpeg_50.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Recompress, 3, 0, "jpeg", vbNull)
      ret = oTest.ApcSetFunction(id, Ap_Set_JpegQuality, 50, 0, vbNull, vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If


	'------------------------------------------------------------------------------------------
	'RecompressBlackWhiteFile
	'------------------------------------------------------------------------------------------	
	Response.write( "RecompressBlackWhiteFile <BR>")
	pImage = "d:\test\BlackWhite.tif"
	  
	pPdf ="d:\test\BlackWhite_lzw.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Recompress, 1, 0, "lzw", vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If
	  
	pPdf ="d:\test\BlackWhite_zip.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Recompress, 1, 0, "zip", vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If
	  
	pPdf ="d:\test\BlackWhite_g3.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Recompress, 1, 0, "g3", vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If
	  
	pPdf ="d:\test\BlackWhite_g4.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Recompress, 1, 0, "g4", vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If

	'------------------------------------------------------------------------------------------
	'Skewcorrect
	'------------------------------------------------------------------------------------------	
	Response.write( "Skewcorrect <BR>")
	pPdf = "d:\test\Skewcorrect.pdf"
	pImage = "d:\test\Skewcorrect.tif"
	id = oTest.ApcCreate(pPdf)
	If id > 0 Then            
      ret = oTest.ApcAddImage(id, pImage)
      'set skewcorrect
      ret = oTest.ApcSetFunction(id, Ap_Set_Skewcorrect, 1, 0, vbNull, vbNull)
      ret = oTest.ApcAddImage(id, pImage)                 
      oTest.ApcClose( id)
	End If

	'------------------------------------------------------------------------------------------
	'PageSize
	'------------------------------------------------------------------------------------------	
	Response.write( "PageSize <BR>")
	
	pPdf = "d:\test\PageSize.pdf"
	pImage = "d:\test\TrueColor.tif"

	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
	  
      'Get image width and height
      ImageWidth = oTest.ApcGetFunction(id, Ap_Get_ImageSizeW, 0, 0, pImage, vbNull)
      ImageHeight = oTest.ApcGetFunction(id, Ap_Get_ImageSizeH, 0, 0, pImage, vbNull)
      
      'Get image dpi (Resolution)
      xdpi = oTest.ApcGetFunction(id, Ap_Get_ImageDpiX, 0, 0, pImage, vbNull)
      ydpi = oTest.ApcGetFunction(id, Ap_Get_ImageDpiY, 0, 0, pImage, vbNull)

      'keep image dpi (resolution)
      ImageWidth2 = (ImageWidth * (72 / xdpi))
      ImageHeight2 = (ImageHeight * (72 / ydpi))      
      ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, ImageWidth2, ImageHeight2, vbNull, vbNull)
      ret = oTest.ApcAddImage(id, pImage)

      'page size with image size
      ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, CInt(ImageWidth), CInt(ImageHeight), vbNull, vbNull)
      ret = oTest.ApcAddImage(id, pImage)

      'A4
      ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, 2, 2, vbNull, vbNull)
      ret = oTest.ApcSetFunction(id, Ap_Set_ImagePosAndSize, 0, 0, CStr(ImageWidth), CStr(ImageHeight))
      ret = oTest.ApcAddImage(id, pImage)

      'custom page size and fit width/height
      PageWidth = 768
      PageHeight = 1024
      ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, PageWidth, PageHeight, vbNull, vbNull)
      zw = PageWidth / ImageWidth
      zh = PageHeight / ImageHeight
      if(zw < zh) then 
		fZoom = zw
      else
		fZoom = zh
	  end if
      ImageWidth = (ImageWidth * fZoom)
      ImageHeight = (ImageHeight * fZoom)
      ret = oTest.ApcSetFunction(id, Ap_Set_ImagePosAndSize, CInt((PageWidth - ImageWidth)/2), CInt((PageHeight - ImageHeight)/2), CStr(ImageWidth), CStr(ImageHeight))
      ret = oTest.ApcAddImage(id, pImage)
      
      oTest.ApcClose( id  )
	End If
	
	'------------------------------------------------------------------------------------------
	'PageLayout
	'------------------------------------------------------------------------------------------	
	Response.write( "PageLayout <BR>")
	pPdf = "d:\test\PageLayout.pdf"
	nPos = 1
	PageWidth = 768
	PageHeight = 1024

	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      For i = 0 To length - 1
      
          'get pages
          nPages = oTest.ApcGetFunction(id, Ap_Get_ImagePageCount, 0, 0, pImages(i), vbNull)
          
          For index = 1 To nPages
          
              Select Case nPos Mod 4
              Case 1
                      'new page
                      ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, PageWidth, PageHeight, vbNull, vbNull)
                      'set image posion
                      ret = oTest.ApcSetFunction(id, Ap_Set_ImagePosAndSize, 0, 0, CStr(PageWidth / 2), CStr(PageHeight / 2))
                      ret = oTest.ApcSetFunction(id, Ap_Set_MultiPage, index, 1, vbNull, vbNull)
                      ret = oTest.ApcAddImage(id, pImages(i))
              Case 2
                      ret = oTest.ApcSetFunction(id, Ap_Set_ImagePosAndSize, PageWidth / 2, 0, CStr(PageWidth / 2), CStr(PageHeight / 2))
                      ret = oTest.ApcSetFunction(id, Ap_Set_MultiPage, index, 1, vbNull, vbNull)
                      ret = oTest.ApcAddImage(id, pImages(i))
              Case 3
                      ret = oTest.ApcSetFunction(id, Ap_Set_ImagePosAndSize, 0, PageHeight / 2, CStr(PageWidth / 2), CStr(PageHeight / 2))
                      ret = oTest.ApcSetFunction(id, Ap_Set_MultiPage, index, 1, vbNull, vbNull)
                      ret = oTest.ApcAddImage(id, pImages(i))
              Case 0
                      ret = oTest.ApcSetFunction(id, Ap_Set_ImagePosAndSize, PageWidth / 2, PageHeight / 2, CStr(PageWidth / 2), CStr(PageHeight / 2))
                      ret = oTest.ApcSetFunction(id, Ap_Set_MultiPage, index, 1, vbNull, vbNull)
                      ret = oTest.ApcAddImage(id, pImages(i))
                      'close page
                      ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, 0, 0, vbNull, vbNull)
                      
              End Select

              nPos = nPos + 1
              
          Next 
      Next 
      oTest.ApcClose( id)
      
	End If

	'------------------------------------------------------------------------------------------
	'PageOrientation
	'------------------------------------------------------------------------------------------	
	Response.write( "PageOrientation <BR>")
	pImage = "d:\test\MultiPage.tif"
	  
	nOrientation = 90
	pPdf = "d:\test\PageOrientation_90.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Orientation, nOrientation, 0, vbNull, vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose (id)
	End If

	nOrientation = 180
	pPdf = "d:\test\PageOrientation_180.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Orientation, nOrientation, 0, vbNull, vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose (id)
	End If

	nOrientation = 270
	pPdf = "d:\test\PageOrientation_270.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      ret = oTest.ApcSetFunction(id, Ap_Set_Orientation, nOrientation, 0, vbNull, vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose (id)
	End If

	'------------------------------------------------------------------------------------------
	'Watermark
	'------------------------------------------------------------------------------------------	
	Response.write( "Watermark <BR>")
	pPdf = "d:\test\Watermark.pdf"
	id = oTest.ApcCreate(pPdf)
	If (id > 0) Then
      For i = 0 To length - 1
          nPages = oTest.ApcGetFunction(id, Ap_Get_ImagePageCount, 0, 0, pImages(i), vbNull)
          For index = 1 To nPages
          
              'get image width and height
              ImageWidth = oTest.ApcGetFunction(id, Ap_Get_ImageSizeW, 0, 0, pImages(i), CStr(index))
              ImageHeight = oTest.ApcGetFunction(id, Ap_Get_ImageSizeH, 0, 0, pImages(i), CStr(index))
              'new page
              ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, ImageWidth, ImageHeight, vbNull, vbNull)
              'set image pos
              ret = oTest.ApcSetFunction(id, Ap_Set_ImagePosAndSize, 0, 0, CStr(ImageWidth), CStr(ImageHeight))
          
              ret = oTest.ApcSetFunction(id, Ap_Set_MultiPage, index, 1, vbNull, vbNull)
              ret = oTest.ApcAddImage(id, pImages(i))
              
              'set system font and size
              ret = oTest.ApcSetFunction(id, Ap_Set_Font, 1, 32, "Arial", vbNull)
              'add text
              nPosX = 10
              nPosY = ImageHeight / 2
              ret = oTest.ApcAddTextEx(id, nPosX, nPosY, "(c)Copyright XXXXXXXXXX", RGB(255, 0, 0))

              'close page
              ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, 0, 0, vbNull, vbNull)
          
          Next 
      Next 
      oTest.ApcClose (id)
      
	End If

	'------------------------------------------------------------------------------------------
	'PDFsecurity
	'------------------------------------------------------------------------------------------	
	Response.write( "PDFsecurity <BR>")
	pPdf = "d:\test\Security.pdf"
	pImage = "d:\test\TrueColor.tif"
	id = oTest.ApcCreate(pPdf)
	If id > 0 Then
      'set password and permission
      ret = oTest.ApcSetFunction(id, Ap_Set_Encrypt, 128, permission_Print + permission_Copy + permission_Modify, "owner", "user")
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If


	'------------------------------------------------------------------------------------------
	'PDFInfo
	'------------------------------------------------------------------------------------------	
	Response.write( "PDFInfo <BR>")
	pPdf = "d:\test\Information.pdf"
	pImage = "d:\test\TrueColor.tif"
	id = oTest.ApcCreate(pPdf)
	If id > 0 Then
      ret = oTest.ApcAddInfo(id, "Title", "Subject", "Author", "Keywords", "Creator")
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If

	'------------------------------------------------------------------------------------------
	'Bookmark
	'------------------------------------------------------------------------------------------	
	Response.write( "Bookmark <BR>")
	pPdf = "d:\test\Bookmark.pdf"
	pImage = "d:\test\MultiPage.tif"
	id = oTest.ApcCreate(pPdf)
	If id > 0 Then
  
      'get multi page image page count
      pageCount = oTest.ApcGetFunction(id, Ap_Get_ImagePageCount, 0, 0, pImage, vbNull)
      For beginPage = 1 To pageCount
          'add bookmark
          szBookmark = "bookmark " + CStr(beginPage)
          ret = oTest.ApcSetFunction(id, Ap_Set_BookMark, 0, 0, szBookmark, vbNull)
          'Add page
          ret = oTest.ApcSetFunction(id, Ap_Set_MultiPage, beginPage, 1, vbNull, vbNull)
          ret = oTest.ApcAddImage(id, pImage)
      Next 
      
      oTest.ApcClose (id)  
	End If

	'------------------------------------------------------------------------------------------
	'ViewerPreferences
	'------------------------------------------------------------------------------------------	
	Response.write( "ViewerPreferences <BR>")
	pPdf = "d:\test\ViewerPreferences.pdf"
	pImage = "d:\test\TrueColor.tif"
	id = oTest.ApcCreate(pPdf)
	If id > 0 Then
      ret = oTest.ApcSetFunction(id, Ap_Set_ViewerPreferences, 4 + 4096, 2, vbNull, vbNull)
      ret = oTest.ApcAddImage(id, pImage)
      oTest.ApcClose( id)
	End If


	'------------------------------------------------------------------------------------------
	'TextAndGraphics
	'------------------------------------------------------------------------------------------	
	Response.write( "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 = oTest.ApcCreate(pPdf)
	If id > 0 Then
      'create a page for a3
      ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, 1, 1, vbNull, vbNull)
      'set system font and size
      ret = oTest.ApcSetFunction(id, Ap_Set_Font, 1, 32, "Arial", vbNull)
      'add text
      ret = oTest.ApcAddTextEx(id, 300, 300, "this is a text!", RGB(0, 255, 255))

      'set system font and size
      ret = oTest.ApcSetFunction(id, Ap_Set_Font, 1, 32, "Helvetica", vbNull)
      'add text
      ret = oTest.ApcAddTextEx(id, 300, 400, "this is a text!", RGB(0, 255, 255))

      'set adobe font and size
      ret = oTest.ApcSetFunction(id, Ap_Set_Font, 203, 32, vbNull, vbNull)
      'add text with rectangle
      width = 400
      height = 200
      ret = oTest.ApcAddText(id, 100, 50, width, height, szString, RGB(0, 0, 255), RGB(255, 0, 255), Ap_DT_WORDBREAK + Ap_DT_CENTER)

      'set system font and size
      ret = oTest.ApcSetFunction(id, Ap_Set_Font, 1, 32, "Verdana", vbNull)
      'add text
      ret = oTest.ApcAddTextEx(id, 300, 500, "this is a text!", RGB(0, 255, 255))

      'create a page for b5
      ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, 4, 4, vbNull, vbNull)
      'add graphic
      ret = oTest.ApcAddLine(id, 10, 10, 100, 200, 5, RGB(0, 0, 255))
      ret = oTest.ApcAddRect(id, 20, 210, 300, 400, 3, RGB(0, 255, 0), 1, RGB(255, 0, 255))
      'close page
      ret = oTest.ApcSetFunction(id, Ap_Set_NewOrClosePage, 0, 0, vbNull, vbNull)

      'add a txt file
      ret = oTest.ApcSetFunction(id, Ap_Set_Font, 200, 16, vbNull, vbNull)
      ret = oTest.ApcAddTxt(id, "d:\test\test.txt", 0, 0, 0, 1, 1, 64, 64, 64, 64, 4)

      oTest.ApcClose (id)
  
	End If
  
	Response.write( "Convert  Success!")








%>
