AspPDF.NET 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 an overloaded version of OpenImage
which opens an image from a byte array. 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:
C# |
void Page_Load(Object Source, EventArgs E)
{
PdfManager objPdf = new PdfManager();
// Create empty document
PdfDocument objDoc = objPdf.CreateDocument();
// Add a new page
PdfPage objPage = objDoc.Pages.Add();
// Open image
PdfImage objImage = objDoc.OpenImage( Server.MapPath( "painting.jpg") );
// Create empty param object
PdfParam objParam = objPdf.CreateParam();
for( int i = 1; i <=3; i++ )
{
objParam["x"] = (objPage.Width - objImage.Width / i) / 2.0f;
objParam["y"] = objPage.Height - objImage.Height * i / 2.0f - 200;
objParam["ScaleX"] = 1.0f / i;
objParam["ScaleY"] = 1.0f / i;
objPage.Canvas.DrawImage( objImage, objParam );
}
// Save document, the Save method returns generated file name
string strFilename = objDoc.Save( Server.MapPath("image.pdf"), false );
lblResult.Text = "Success! Download your PDF file <A TARGET=_new HREF=" + strFilename + ">here</A>";
}
|
VB.NET |
Sub Page_Load(Source As Object, E As EventArgs)
Dim objPdf As PdfManager = New PdfManager()
' Create empty document
Dim objDoc As PdfDocument = objPdf.CreateDocument()
' Add a new page
Dim objPage As PdfPage = objDoc.Pages.Add()
' Open image
Dim objImage As PdfImage = objDoc.OpenImage( Server.MapPath( "painting.jpg") )
' Create empty param object
Dim objParam As PdfParam = objPdf.CreateParam()
For i As integer = 1 To 3
objParam("x") = (objPage.Width - objImage.Width / i) / 2.0f
objParam("y") = objPage.Height - objImage.Height * i / 2.0f - 200
objParam("ScaleX") = 1.0f / i
objParam("ScaleY") = 1.0f / i
objPage.Canvas.DrawImage( objImage, objParam )
Next
' Save document, the Save method returns generated file name
Dim strFilename As String = objDoc.Save( Server.MapPath("image.pdf"), false )
lblResult.Text = "Success! Download your PDF file <A TARGET=_new HREF=" + strFilename + ">here</A>"
End Sub
|
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.net/manual_05/05_image.cs.aspx
http://localhost/asppdf.net/manual_05/05_image.vb.aspx
The overloaded OpenImage(byte[]) method
opens an image from a byte array
as opposed to disk. This method can be used if the image being opened
is stored in a database table as a blob:
PdfImage objImage = objDoc.OpenImage( rs["image_blob"] );
5.1.1 Image-to-PDF Conversion
The PdfImage properties ResolutionX and ResolutionY
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:
C# |
...
// Open image from file
PdfImage objImage = objDoc.OpenImage(Server.MapPath( "atlanticocean.tif" ) );
// 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;
PdfPage objPage = objDoc.Pages.Add( fWidth, fHeight );
// Draw image
objPage.Canvas.DrawImage( objImage, "x=0, y=0" );
...
|
VB.NET |
...
' Open image from file
Dim objImage As PdfImage = objDoc.OpenImage(Server.MapPath( "atlanticocean.tif" ) )
' Add empty page. Page size is based on resolution and size of image
Dim fWidth As Single = objImage.Width * 72.0f / objImage.ResolutionX
Dim fHeight As Single = objImage.Height * 72.0f / objImage.ResolutionY
Dim objPage As PdfPage = objDoc.Pages.Add( fWidth, fHeight )
' Draw image
objPage.Canvas.DrawImage( objImage, "x=0, y=0" )
...
|
Click on the links below to run this code sample:
http://localhost/asppdf.net/manual_05/05_convert.cs.aspx
http://localhost/asppdf.net/manual_05/05_convert.vb.aspx