Somacon.com: Articles on websites & etc.

§ Home > Index > ASP Programming

PDFLib Image Function for ASP VBScript

This vbscript subroutine uses PDFlib to create a letter-size page and insert a specified TIFF image file centered in the page.

<%
' This code is hereby granted to the public domain.
' Author: Shailesh N. Humbad, http://www.somacon.com
'
' CreateCenteredTiffPage Subroutine
' 
' This vbscript subroutine uses PDFlib to create a letter-size page and insert
'   a specified TIFF image file centered in the page.
' It requires a created PDFlib object in document context, 
'   and the filename of the TIFF image.
' The image file should have appropriate permissions, e.g. 
'   IUSR_<machinename> read permission under IIS/ASP.
' This function sets the imagewarning parameter to true.
' Refer to the PDFlib 4.0.3 Reference manual section 3.4.2,
' at www.pdflib.com for more information.
' Example: 
'     CreateCenteredTiffPage oPDF, "c:\images\scan.tif" 
'

Sub CreateCenteredTiffPage(oPDF, strFileName)
    Dim page_y, page_x
    Dim ImageHandle, image_y, image_x
    Dim dpi_x, dpi_y, scale_x, scale_y, pos_x, pos_y

    ' make sure pdf object is declared
    If Not isObject(oPDF) Then
        Err.Raise 424, "CreateCenteredTiffPage Error: " & _
            "oPDF must be a PDFlib object."
    End If

    ' 8.5 x 11 page
    page_y = 792
    page_x = 612
    
    ' turn on image warnings
    oPDF.set_parameter "imagewarning", "true"
    
    ' open specified image
    ImageHandle = oPDF.open_image_file("tiff", strFileName, "", 0)
    
    ' get image size and resolution
    image_y = oPDF.get_value("imageheight", ImageHandle)
    image_x = oPDF.get_value("imagewidth", ImageHandle)
    dpi_x = oPDF.get_value("resx", ImageHandle)
    dpi_y = oPDF.get_value("resy", ImageHandle)
    
    ' calculate the image scale
    If (dpi_x > 0 And dpi_y > 0) Then
        scale_x = 72 / dpi_x
        scale_y = 72 / dpi_y
    Else
        If (dpi_x < 0 And dpi_y < 0) Then
            scale_x = 1
            scale_y = dpi_y / dpi_x
        Else
            scale_x = 1
            scale_y = 1
        End If
    End If
    
    ' if the image is bigger than the page, 
    ' resize the page to exactly fit the image
    If image_y * scale_y > page_y Then
        page_y = image_y * scale_y
    End If
    If image_x * scale_x > page_x Then
        page_x = image_x * scale_x
    End If

    ' create the page
    oPDF.begin_page page_x, page_y
    ' scale the page
    oPDF.[Scale] scale_x, scale_y
    
    ' calculate the position for the centered image
    pos_x = CInt((page_x-image_x * scale_x)/2)
    pos_y = CInt((page_y-image_y * scale_y)/2)
    
    ' place the image in the center of the page
    oPDF.place_image ImageHandle, pos_x, pos_y, 1
    
    ' close the image
    oPDF.close_image ImageHandle
    ' finish the page
    oPDF.end_page
    
End Sub
%>

Created 2005-04-24, Last Modified 2011-07-24, © Shailesh N. Humbad
Disclaimer: This content is provided as-is. The information may be incorrect.