AspPDF is capable of placing arbitrary images on a PDF document.
It supports images in BMP, GIF, JPEG, PNG and TIFF formats. To display an image
on a canvas, the method Canvas.DrawImage should be used. This method
expects two arguments, an instance of the PdfImage object
and a parameter object (or parameter string).
A PdfImage object is created via PdfDocument's
OpenImage method which takes an image path as an argument.
PdfImage can also be created via the OpenImageBinary
method which opens an image from memory. Once
an instance of PdfImage is created, it can be displayed
multiple times across a PDF document.
The following code segment displays the image painting.jpg three times on a page
with various scaling factors:
VBScript |
Set Pdf = Server.CreateObject("Persits.Pdf")
Set Doc = Pdf.CreateDocument
Set Page = Doc.Pages.Add
Set Image = Doc.OpenImage( Server.MapPath( "painting.jpg") )
Set Param = Pdf.CreateParam
For i = 1 To 3
Param("x") = (Page.Width - Image.Width / i) / 2
Param("y") = Page.Height - Image.Height * i / 2 - 200
Param("ScaleX") = 1 / i
Param("ScaleY") = 1 / i
Page.Canvas.DrawImage Image, Param
Next
Filename = Doc.Save( Server.MapPath("image.pdf"), False )
|
C# |
IPdfManager objPdf = new PdfManager();
IPdfDocument objDoc = objPdf.CreateDocument(Missing.Value);
IPdfPage objPage = objDoc.Pages.Add(Missing.Value, Missing.Value, Missing.Value);
IPdfImage objImage = objDoc.OpenImage( Server.MapPath( "painting.jpg"), Missing.Value );
IPdfParam objParam = objPdf.CreateParam(Missing.Value);
for( int i = 1; i <=3; i++ )
{
objParam["x"].Value = (objPage.Width - objImage.Width / i) / 2.0f;
objParam["y"].Value = objPage.Height - objImage.Height * i / 2.0f - 200;
objParam["ScaleX"].Value = 1.0f / i;
objParam["ScaleY"].Value = 1.0f / i;
objPage.Canvas.DrawImage( objImage, objParam );
}
String strFilename = objDoc.Save( Server.MapPath("image.pdf"), false );
|
Here, we pass four arguments to the DrawImage method: X, Y, ScaleX and ScaleY.
X and Y are required: they specify the coordinates of the lower-left corner of the image being displayed.
ScaleX and ScaleY are optional: they specify scaling factors along the X and Y coordinates.
Both arguments are 1 by default, which means the image size on the page (in user coordinates)
will be equal to its pixel size, provided that the image resolution is
the standard 72 dots per inch (dpi).
For example, if a 72dpi image is 360 x 216 pixels and
the ScaleX/ScaleY arguments are not specified, the image will occupy
360 x 216 units of space, or 5" x 3". A 300dpi image with the same
pixel size will only occupy 1.2" x .72".
You can optionally specify a rotation Angle (in degrees) by which the image will
be rotated counter-clockwise around its lower-left corner.
Click on the links below to run this code sample:
http://localhost/asppdf/manual_05/05_image.asp
http://localhost/asppdf/manual_05/05_image.aspx
The Doc.OpenImageBinary method
does the same as OpenImage but it opens an image from a memory array
as opposed to disk. This method can be used if the image being opened
is stored in a database table as a blob:
Set Image = Doc.OpenImageBinary( rs("image_blob").Value )
5.2.1 Image-to-PDF Conversion
The PdfImage properties ResolutionX and ResolutionY
(introduced by AspPDF 1.1)
enable conversion of GIF, JPEG, BMP, PNG and TIFF images
to one-page PDF documents with the size and
resolution of the original image fully preserved. These properties
return the dot-per-inch (DPI) resolutions of an image along
the X and Y coordinates. For GIF, PNG and BMP images, these values are
always 72 dpi, the resolution of JPEG and TIFF images may vary
and usually ranges from 72 to 600 dpi.
The following code fragment converts an arbitrary image
into a one-page PDF document with the page size
calculated based on the size and resolution of the image being converted:
VBScript |
...
Set Image = Doc.OpenImage(Server.MapPath( "atlanticocean.tif" ) )
' Add empty page. Page size is based on resolution and size of image
Width = Image.Width * 72 / Image.ResolutionX
Height = Image.Height * 72 / Image.ResolutionY
Set Page = Doc.Pages.Add( Width, Height )
' Draw image
Page.Canvas.DrawImage Image, "x=0, y=0"
...
|
C# |
...
// Open image from file
IPdfImage objImage = objDoc.OpenImage(Server.MapPath( "atlanticocean.tif" ), Missing.Value );
// Add empty page. Page size is based on resolution and size of image
float fWidth = objImage.Width * 72.0f / objImage.ResolutionX;
float fHeight = objImage.Height * 72.0f / objImage.ResolutionY;
IPdfPage objPage = objDoc.Pages.Add( fWidth, fHeight, Missing.Value );
// Draw image
objPage.Canvas.DrawImage( objImage, "x=0, y=0" );
...
|
http://localhost/asppdf/manual_05/05_convert.asp
http://localhost/asppdf/manual_05/05_convert.aspx