<%@ Page Language="c#" debug="true" %>
<%@ Import Namespace = "System.Runtime.InteropServices" %>
<script language="c#" runat="server">

//----------------------------------------------------------------------------------------
//defined for ApSetFunction
const int Ap_Set_MultiPage         = 101;
const int Ap_Set_ImagePosAndSize   = 104;
const int Ap_Set_Encrypt           = 105;
const int Ap_Set_Font              = 106;
const int Ap_Set_NewOrClosePage    = 109;
const int Ap_Set_BookMark          = 110;
const int Ap_Set_ViewerPreferences = 121;
const int Ap_Set_Orientation       = 122;
const int Ap_Set_Recompress        = 123;
const int Ap_Set_Skewcorrect       = 126;
const int Ap_Set_JpegQuality       = 128;

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

//defined for ApAddText
const int Ap_DT_LEFT      = 0;
const int Ap_DT_CENTER 	  = 1;
const int Ap_DT_RIGHT     = 2;
const int Ap_DT_WORDBREAK = 10;
const int Ap_DT_CALCRECT  = 1024;


//defined permission
const int permission_Print  = 1;
const int permission_Copy   = 2;
const int permission_Modify = 4;


//----------------------------------------------------------------------------------------
[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 
public static extern int ApCreate(string filename);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 
public static extern int ApOpen(string filename);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 
public static extern void ApClose(int id);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 
public static extern int ApAddInfo(int id,
                                   string title,
                                   string subject,
                                   string author,          
                                   string keywords,
                                   string creator);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 									   									
public static extern int ApSetFunction(int id,
                                       int code,
                                       int para1,
                                       int para2,
                                       string para3,
                                       string para4);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 	
public static extern int ApGetFunction(int id,
                                       int code,
                                       int para1,
                                       int para2,
                                       string para3,
                                       string para4);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 	
public static extern int	ApAddImage(int id,string filename);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 	
public static extern int ApAddImageData(int id,
                                        string data,
                                        int width,
                                        int height,
                                        int color/*1,8,24*/);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 	
public static extern int ApAddTextEx(int id,
                                     int x,
                                     int y,
                                     string str,
                                     int color);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 	
public static extern int ApAddText(int id,
                                   int x,
                                   int y,
                                   int width,
                                   int height, 
                                   string str,
                                   int color,
                                   int bkColor,
                                   int format);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 	
public static extern int ApAddTxt(int id,
                                  string textFileName,
                                  int color,	
                                  int width,
                                  int height,
                                  int autoNewLine,
                                  int autoWidthAdjust, 
                                  int leftMargin,
                                  int rightMargin,
                                  int topMargin,
                                  int bottomMargin,	
                                  int tabSize);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 	
public static extern int ApAddLine(int id,
                                   int sx,
                                   int sy,
                                   int ex,
                                   int ey,
                                   int width,
                                   int color);

[DllImport("imagetopdf",CallingConvention=CallingConvention.StdCall)] 	
public static extern int ApAddRect(int id,
                                   int sx,
                                   int sy,
                                   int ex,
                                   int ey,
                                   int sideWidth,
                                   int sideColor,
                                   int flagFill,
                                   int fillColor);									   									
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
protected void Page_Load(Object Src, EventArgs E) 
{
    string sPdf,sImage,szPDFFile,szBookmark,szString;
    int i,id,index,nPos,nPages,width,height;
    int nPosX,nPosY,beginPage,pageCount,nOrientation;
    int ImageWidth,ImageHeight,ImageWidth2,ImageHeight2,PageWidth,PageHeight,xdpi,ydpi;
    double fZoom,zw,zh;
    
    string[] sImages = {"d:\\test\\MultiPage.tif",
                        "d:\\test\\BlackWhite.tif",
                        "d:\\test\\Gray.tif",
                        "d:\\test\\TrueColor.tif",
                        "d:\\test\\test.jpg",
                        "d:\\test\\test.bmp",
                        "d:\\test\\test.gif",
                        "d:\\test\\test.emf",
                        "d:\\test\\test.wmf",
                        "d:\\test\\test.dcx",
                        "d:\\test\\test.pcx",
                        "d:\\test\\test.psd"};
    int length = sImages.Length;







    //----------------------------------------------------------------------------------------	
    //One2Multiple (split image file)
    //----------------------------------------------------------------------------------------			
    sImage = "d:\\test\\MultiPage.tif";
    sPdf = "d:\\test\\One2Multiple";
    
    //get multipage image page count
    pageCount = ApGetFunction(0, Ap_Get_ImagePageCount, 0, 0, sImage, null);
      
    for(beginPage=1; beginPage<pageCount+1; beginPage++ )
    {
        szPDFFile = sPdf + Convert.ToString(beginPage) + ".pdf";
        id=ApCreate(szPDFFile);
        if( id >0 )
        {
            ApSetFunction(id, Ap_Set_MultiPage, beginPage, 1, null, null);
            ApAddImage(id,sImage);
            ApClose(id);
        }        
    }	
    
    //----------------------------------------------------------------------------------------	
    //One2One
    //----------------------------------------------------------------------------------------
    sImage = "d:\\test\\MultiPage.tif";
    sPdf = "d:\\test\\One2One1.pdf";      
    id=ApCreate(sPdf);
    if( id >0 )
    {
        ApAddImage(id,sImage);
        ApClose(id);
    } 
			
    sImage = "d:\\test\\test.dcx";
    sPdf = "d:\\test\\One2One2.pdf";
    id=ApCreate(sPdf);
    if( id >0 )
    {
        ApAddImage(id,sImage);
        ApClose(id);
    } 
			
    //----------------------------------------------------------------------------------------				
    //Multiple2One (merge image file)
    //----------------------------------------------------------------------------------------		
    sPdf = "d:\\test\\Multiple2One.pdf";
    id=ApCreate(sPdf);
    if( id>0 )
    {
        for(i=0; i<length; i++)
        {
            ApAddImage(id,sImages[i]);
        }
        ApClose(id);
    }	

    //----------------------------------------------------------------------------------------				
    //AppendPDF	
    //----------------------------------------------------------------------------------------			
    sPdf = "d:\\test\\Append.pdf";
    for(i=0; i<2; i++)
    {  
        id=ApOpen(sPdf);
        if( id<=0 )
        {
            id = ApCreate(sPdf);	
        }
        if( id >0 )
        {
            ApAddImage(id, sImages[i]); 				
            ApClose(id);
        } 			
    }	
			
    //----------------------------------------------------------------------------------------
    //RecompressTruecolorFile
    //----------------------------------------------------------------------------------------			
    sImage = "d:\\test\\TrueColor.tif";

    //recompress lzw
    sPdf = "d:\\test\\TrueColor_lzw.pdf"; 
    id = ApCreate(sPdf);
    if( id>0 )
    {
        ApSetFunction(id, Ap_Set_Recompress, 3, 0, "lzw", null);
        ApAddImage(id, sImage);
        ApClose(id);	
    }
    //recompress zip
    sPdf = "d:\\test\\TrueColor_zip.pdf"; 
    id = ApCreate(sPdf);
    if( id>0 )
    {
        ApSetFunction(id, Ap_Set_Recompress, 3, 0, "zip", null);
        ApAddImage(id, sImage);
        ApClose(id);	
    }			
    //recompress packbits	
    sPdf = "d:\\test\\TrueColor_packbits.pdf"; 
    id = ApCreate(sPdf);
    if( id>0 )
    {
        ApSetFunction(id, Ap_Set_Recompress, 3, 0, "packbits", null);
        ApAddImage(id, sImage);
        ApClose(id);	
    }
    //recompress jpeg,quality=100					
    sPdf = "d:\\test\\TrueColor__jpeg_100.pdf"; 
    id = ApCreate(sPdf);
    if( id>0 )
    {
        ApSetFunction(id, Ap_Set_Recompress, 3, 0, "jpeg", null);
        ApSetFunction(id, Ap_Set_JpegQuality, 100, 0, null, null);				
        ApAddImage(id, sImage);
        ApClose(id);	
    }		
    //recompress jpeg,quality=50					
    sPdf = "d:\\test\\TrueColor__jpeg_50.pdf"; 
    id = ApCreate(sPdf);
    if( id>0 )
    {
        ApSetFunction(id, Ap_Set_Recompress, 3, 0, "jpeg", null);
        ApSetFunction(id, Ap_Set_JpegQuality, 50, 0, null, null);				
        ApAddImage(id, sImage);
        ApClose(id);	
    }				
			
    //----------------------------------------------------------------------------------------
    //RecompressBlackWhiteFile
    //----------------------------------------------------------------------------------------			
    sImage = "d:\\test\\BlackWhite.tif";

    sPdf = "d:\\test\\BlackWhite_lzw.pdf"; 
    id = ApCreate(sPdf);
    if( id>0 )
    {
        ApSetFunction(id, Ap_Set_Recompress, 1, 0, "lzw", null);
        ApAddImage(id, sImage);
        ApClose(id);	
    } 

    sPdf = "d:\\test\\BlackWhite_zip.pdf"; 
    id = ApCreate(sPdf);
    if( id>0 )
    {
        ApSetFunction(id, Ap_Set_Recompress, 1, 0, "zip", null);
        ApAddImage(id, sImage);
        ApClose(id);	
    }   		

    sPdf = "d:\\test\\BlackWhite_g3.pdf"; 
    id = ApCreate(sPdf);
    if( id>0 )
    {
        ApSetFunction(id, Ap_Set_Recompress, 1, 0, "g3", null);
        ApAddImage(id, sImage);
        ApClose(id);	
    }   		

    sPdf = "d:\\test\\BlackWhite_g4.pdf"; 
    id = ApCreate(sPdf);
    if( id>0 )
    {
        ApSetFunction(id, Ap_Set_Recompress, 1, 0, "g4", null);
        ApAddImage(id, sImage);
        ApClose(id);	
    }  			
			
    //----------------------------------------------------------------------------------------
    //Skewcorrect
    //----------------------------------------------------------------------------------------			
    sImage = "d:\\test\\Skewcorrect.tif";
    sPdf = "d:\\test\\Skewcorrect.pdf";
      
    id=ApCreate(sPdf);
    if( id >0 )
    {
        ApAddImage(id,sImage);
        //set skewcorrect
        ApSetFunction(id, Ap_Set_Skewcorrect, 1, 0, null, null);
        ApAddImage(id,sImage);				
        ApClose(id);
    }  			
			
			
    //----------------------------------------------------------------------------------------
    //PageSize
    //----------------------------------------------------------------------------------------			
    sImage = "d:\\test\\TrueColor.tif";
    sPdf = "d:\\test\\PageSize.pdf";

    id=ApCreate(sPdf);
    if( id >0 )
    {
        //Get image width and height
        ImageWidth = ApGetFunction(id, Ap_Get_ImageSizeW, 0, 0, sImage, null);
        ImageHeight = ApGetFunction(id, Ap_Get_ImageSizeH, 0, 0, sImage, null);

        //Get image dpi (Resolution)
        xdpi = ApGetFunction(id, Ap_Get_ImageDpiX, 0, 0, sImage, null);
        ydpi = ApGetFunction(id, Ap_Get_ImageDpiY, 0, 0, sImage, null);					

        //keep image dpi (resolution)
        ImageWidth2 = Convert.ToInt32(ImageWidth*(72.0/xdpi));
        ImageHeight2 = Convert.ToInt32(ImageHeight*(72.0/ydpi));      
        ApSetFunction(id, Ap_Set_NewOrClosePage, ImageWidth2, ImageHeight2, null, null);
        ApAddImage(id, sImage);				

        //page size with image size
        ApSetFunction(id, Ap_Set_NewOrClosePage, ImageWidth, ImageHeight, null, null);
        ApAddImage(id, sImage);				

        //A4
        ApSetFunction(id, Ap_Set_NewOrClosePage, 2, 2, null, null);
        ApSetFunction(id, Ap_Set_ImagePosAndSize, 0, 0, Convert.ToString(ImageWidth), Convert.ToString(ImageHeight));
        ApAddImage(id, sImage);				

        //custom page size and fit width/height
        PageWidth = 768;
        PageHeight = 1024;
        ApSetFunction(id, Ap_Set_NewOrClosePage, PageWidth, PageHeight, null, null);
        zw = (PageWidth*1.0)/ImageWidth;
        zh = (PageHeight*1.0)/ImageHeight;
        if(zw < zh)  
        {
            fZoom = zw;
        }else{
            fZoom = zh;
        }

        ImageWidth = Convert.ToInt32(ImageWidth*fZoom);
        ImageHeight = Convert.ToInt32(ImageHeight*fZoom);
        ApSetFunction(id, Ap_Set_ImagePosAndSize, Convert.ToInt32((PageWidth - ImageWidth)/2), Convert.ToInt32((PageHeight - ImageHeight)/2), Convert.ToString(ImageWidth), Convert.ToString(ImageHeight));

        ApAddImage(id, sImage);
        ApClose(id);
    }			
			
    //----------------------------------------------------------------------------------------
    //PageLayout
    //----------------------------------------------------------------------------------------			
    sPdf = "d:\\test\\PageLayout.pdf";
    nPos = 1;
    PageWidth = 768;
    PageHeight = 1024;

    id=ApCreate(sPdf);
    if(id>0)
    {
        for(i=0; i<length; i++)
        {
            nPages = ApGetFunction(id, Ap_Get_ImagePageCount, 0, 0, sImages[i], null);
            for(index=1; index<nPages+1; index++)
            {
                switch(nPos%4)
                {
                case 0:
                    ApSetFunction(id, Ap_Set_ImagePosAndSize, PageWidth/2, PageHeight/2, Convert.ToString(PageWidth/2), Convert.ToString(PageHeight/2));
                    ApSetFunction(id, Ap_Set_MultiPage, index, 1, null, null);
                    ApAddImage(id, sImages[i]);
                    //close page
                    ApSetFunction(id, Ap_Set_NewOrClosePage, 0, 0, null, null);   			
                break;
		
                case 1:
      	            //new page
                    ApSetFunction(id, Ap_Set_NewOrClosePage, PageWidth, PageHeight, null, null);
                    //set image posion
                    ApSetFunction(id, Ap_Set_ImagePosAndSize, 0, 0, Convert.ToString(PageWidth/2), Convert.ToString(PageHeight/2));
                    ApSetFunction(id, Ap_Set_MultiPage, index, 1, null, null);
                    ApAddImage(id, sImages[i]);         			
                break;      			

                case 2:
                    ApSetFunction(id, Ap_Set_ImagePosAndSize, PageWidth/2, 0, Convert.ToString(PageWidth/2), Convert.ToString(PageHeight/2));
                    ApSetFunction(id, Ap_Set_MultiPage, index, 1, null, null);
                    ApAddImage(id, sImages[i]);      			
                break;      				

                case 3:
                    ApSetFunction(id, Ap_Set_ImagePosAndSize, 0, PageHeight/2, Convert.ToString(PageWidth/2), Convert.ToString(PageHeight/2));
                    ApSetFunction(id, Ap_Set_MultiPage, index, 1, null, null);
                    ApAddImage(id, sImages[i]);      			
                break;      				
                }	
                nPos = nPos + 1;
            }
        }	
        ApClose(id);
    }			
			
    //----------------------------------------------------------------------------------------
    //PageOrientation
    //----------------------------------------------------------------------------------------			
    sImage = "d:\\test\\MultiPage.tif";

    nOrientation = 90;
    sPdf = "d:\\test\\PageOrientation_90.pdf";
    id=ApCreate(sPdf);
    if( id >0 )
    {
        ApSetFunction(id, Ap_Set_Orientation, nOrientation, 0, null, null);
        ApAddImage(id,sImage);
        ApClose(id);
    }
			
    nOrientation = 180;
    sPdf = "d:\\test\\PageOrientation_180.pdf";
    id=ApCreate(sPdf);
    if( id >0 )
    {
        ApSetFunction(id, Ap_Set_Orientation, nOrientation, 0, null, null);
        ApAddImage(id,sImage);
        ApClose(id);
    }			
			
    nOrientation = 270;
    sPdf = "d:\\test\\PageOrientation_270.pdf";
    id=ApCreate(sPdf);
    if( id >0 )
    {
        ApSetFunction(id, Ap_Set_Orientation, nOrientation, 0, null, null);
        ApAddImage(id,sImage);
        ApClose(id);
    }				
			
    //----------------------------------------------------------------------------------------
    //Watermark
    //----------------------------------------------------------------------------------------			
    sPdf = "d:\\test\\Watermark.pdf";
    
    id=ApCreate(sPdf);
    if( id >0 )
    {
        for(i=0; i<length; i++)
        {
            nPages = ApGetFunction(id, Ap_Get_ImagePageCount, 0, 0, sImages[i], null);
            for(index=1; index<nPages+1; index++)
            {
                //get image width and height
                ImageWidth = ApGetFunction(id, Ap_Get_ImageSizeW, 0, 0, sImages[i], Convert.ToString(index));
                ImageHeight = ApGetFunction(id, Ap_Get_ImageSizeH, 0, 0, sImages[i], Convert.ToString(index));
                //new page
                ApSetFunction(id, Ap_Set_NewOrClosePage, ImageWidth, ImageHeight, null, null);
                //set image pos
                ApSetFunction(id, Ap_Set_ImagePosAndSize, 0, 0, Convert.ToString(ImageWidth), Convert.ToString(ImageHeight));
          
                ApSetFunction(id, Ap_Set_MultiPage, index, 1, null, null);
                ApAddImage(id, sImages[i]);
              
                //set system font and size
                ApSetFunction(id, Ap_Set_Font, 1, 32, "Arial", null);
                //add text
                nPosX = 10;
                nPosY = ImageHeight/2;
                ApAddTextEx(id, nPosX, nPosY, "(c)Copyright XXXXXXXXXX", 255);

                //close page
                ApSetFunction(id, Ap_Set_NewOrClosePage, 0, 0, null, null);				
            }
        }
        ApClose(id);
    } 			
			
    //----------------------------------------------------------------------------------------
    //PDFsecurity
    //----------------------------------------------------------------------------------------
    sImage = "d:\\test\\TrueColor.tif";
    sPdf = "d:\\test\\Security.pdf";
      
    id=ApCreate(sPdf);
    if( id >0 )
    {
        ApSetFunction(id, Ap_Set_Encrypt, 128, permission_Print + permission_Copy + permission_Modify, "owner", "user");
        ApAddImage(id,sImage);
        ApClose(id);
    } 			
			
    //----------------------------------------------------------------------------------------
    //PDFInfo
    //----------------------------------------------------------------------------------------
    sImage = "d:\\test\\TrueColor.tif";
    sPdf = "d:\\test\\Information.pdf";
      
    id=ApCreate(sPdf);
    if( id >0 )
    {
        ApAddInfo(id, "Title", "Subject", "Author", "Keywords", "Creator");
        ApAddImage(id,sImage);
        ApClose(id);
    } 			

    //----------------------------------------------------------------------------------------
    //Bookmark
    //----------------------------------------------------------------------------------------
    sImage = "d:\\test\\MultiPage.tif";
    sPdf = "d:\\test\\Bookmark.pdf";

    id=ApCreate(sPdf);
    if( id >0 )
    {
        //get multi page image page count
        pageCount = ApGetFunction(id, Ap_Get_ImagePageCount, 0, 0, sImage, null);
        for(beginPage=1; beginPage<pageCount+1; beginPage++)
        {
            //add bookmark
            szBookmark = "bookmark " + Convert.ToString(beginPage);
            ApSetFunction(id, Ap_Set_BookMark, 0, 0, szBookmark, null);
            //Add page
            ApSetFunction(id, Ap_Set_MultiPage, beginPage, 1, null, null);
            ApAddImage(id,sImage);
        }
        ApClose(id);				
    } 

    //----------------------------------------------------------------------------------------
    //ViewerPreferences
    //----------------------------------------------------------------------------------------
    sImage = "d:\\test\\TrueColor.tif";
    sPdf = "d:\\test\\ViewerPreferences.pdf";
      
    id=ApCreate(sPdf);
    if( id >0 )
    {
        ApSetFunction(id, Ap_Set_ViewerPreferences, 4 + 4096, 2, null, null);
        ApAddImage(id,sImage);
        ApClose(id);
    }   

    //----------------------------------------------------------------------------------------
    //TextAndGraphics
    //----------------------------------------------------------------------------------------
    sPdf = "d:\\test\\TextAndGraphics.pdf";
    szString = "this is a text!\nthis is a text!\nthis is a text!\nthis is a text!";
    id=ApCreate(sPdf);
    if( id >0 )
    {
        //create a page for a3
        ApSetFunction(id, Ap_Set_NewOrClosePage, 1, 1, null, null);
        //set system font and size
        ApSetFunction(id, Ap_Set_Font, 1, 32, "Arial", null);
        //add text
        ApAddTextEx(id, 300, 300, "this is a text!", 16776960);

        //set system font and size
        ApSetFunction(id, Ap_Set_Font, 1, 32, "Helvetica", null);
        //add text
        ApAddTextEx(id, 300, 400, "this is a text!", 16776960);

        //set adobe font and size
        ApSetFunction(id, Ap_Set_Font, 203, 32, null, null);
        //add text with rectangle
        width = 400;
        height = 200;
        ApAddText(id, 100, 50, width, height, szString, 16711680, 16711935, Ap_DT_WORDBREAK + Ap_DT_CENTER);

        //set system font and size
        ApSetFunction(id, Ap_Set_Font, 1, 32, "Verdana", null);
        //add text
        ApAddTextEx(id, 300, 500, "this is a text!", 16776960);

        //create a page for b5
        ApSetFunction(id, Ap_Set_NewOrClosePage, 4, 4, null, null);
        //add graphic
        ApAddLine(id, 10, 10, 100, 200, 5, 16711680);
        ApAddRect(id, 20, 210, 300, 400, 3, 65280, 1, 16711935);
        //close page
        ApSetFunction(id, Ap_Set_NewOrClosePage, 0, 0, null, null);

        //add a txt file
        ApSetFunction(id, Ap_Set_Font, 200, 16, null, null);
        ApAddTxt(id, "d:\\test\\test.txt", 0, 0, 0, 1, 1, 64, 64, 64, 64, 4);
			  ApClose(id);
    }  
}
</script>
