Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

DynaPDF.InsertImage

The function inserts an image from a container.

Component Version macOS Windows Linux Server iOS SDK
DynaPDF 3.1 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "DynaPDF.InsertImage"; PDF; Container; PosX; PosY; ScaleWidth; ScaleHeight { ; Index } )   More

Parameters

Parameter Description Example Flags
PDF The PDF reference returned from DynaPDF.New. $pdf
Container The image container value. Pass the container field or a variable with the container content. $image
PosX The X position (from left of page) 0
PosY The Y position (from bottom of page) 0
ScaleWidth The scaled width of the image. $width
ScaleHeight The scaled height of the image. $height
Index The index of the image for multi image files. 1 Optional

Result

Returns image handle number or error message.

Description

The function inserts an image from a container.
Same as DynaPDF.InsertImageFile, but reading from container. A specific image of a multi-page image can be selected with the parameter Index; the first image is denoted by the index 1. If the image file is not a multi-page image, the parameter Index is ignored. To determine the number of images stored in an image file use the function DynaPDF.GetImageCount. The usage of the function is described in detail at DynaPDF.InsertImageFile.

Please use DynaPDF.SetCompressionFilter to define the compression of your images. Use "flate" there for no jpeg compression. Use DynaPDF.SetJPEGQuality to define JPEG compression quality. also use DynaPDF.SetSaveNewImageFormat with parameter 0 to embed JPEGs to PDF without recompression. Also you should turn off transparency by key color using DynaPDF.SetUseTransparency.
You can use DynaPDF.SetAlpha to set fill transparency to affect how images gain extra transparency.

Coordinates in PDF are by default in a 72dpi raster with lower left being 0/0 point. You can query page size by DynaPDF.GetPageWidth and DynaPDF.GetPageHeight functions.

See also InsertImage function in DynaPDF manual.

Examples

Set a few options and insert image from file:

MBS( "DynaPDF.SetCompressionFilter"; $PDF; "flate" )
MBS( "DynaPDF.SetSaveNewImageFormat"; $PDF; 0 )
MBS( "DynaPDF.SetResolution"; $PDF; 300 )
MBS( "DynaPDF.SetUseTransparency"; $PDF; 0 )
MBS( "DynaPDF.InsertImage"; $PDF; MyDatabase::ImageContainer; 72; 72; 300; 200; 1 )

Insert and reduce qualty to save space:

MBS( "DynaPDF.SetJPEGQuality"; $PDF; 75 )
MBS( "DynaPDF.SetCompressionFilter"; $PDF; "jpeg" )
MBS( "DynaPDF.SetSaveNewImageFormat"; $PDF; 1 )
MBS( "DynaPDF.SetResolution"; $PDF; 100 )
MBS( "DynaPDF.SetUseTransparency"; $PDF; 0 )
MBS( "DynaPDF.InsertImage"; $PDF; MyDatabase::ImageContainer; 72; 72; 300; 200; 1 )

Loop over all pages and insert an image on each page:

# Initialize DynaPDF if needed
If [ MBS("DynaPDF.IsInitialized") ≠ 1 ]
    Perform Script [ Specified: From list ; “InitDynaPDF” ; Parameter: ]
End If
# Start new PDF environment
Set Variable [ $pdf ; Value: MBS("DynaPDF.New") ]
# Load PDF from container
Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromContainer"; $pdf; Watermark pages::InputPDF) ]
# import a page
Set Variable [ $r ; Value: MBS("DynaPDF.ImportPDFFile"; $pdf) ]
# now edit all pages
Set Variable [ $count ; Value: MBS("DynaPDF.GetPageCount"; $pdf) ]
Set Variable [ $index ; Value: 1 ]
Loop
    Set Variable [ $r ; Value: MBS("DynaPDF.EditPage"; $pdf; $index) ]
    Set Variable [ $index ; Value: $index + 1 ]
    # Options for saving image
    Set Variable [ $r ; Value: MBS("DynaPDF.SetSaveNewImageFormat"; $pdf; 1) ]
    Set Variable [ $r ; Value: MBS("DynaPDF.SetCompressionFilter"; $pdf; "jpeg") ]
    # Insert image
    Set Variable [ $w ; Value: 200 ]
    Set Variable [ $h ; Value: 200 ]
    Set Variable [ $x ; Value: (MBS("DynaPDF.GetPageWidth"; $pdf) - $w)/2 ]
    Set Variable [ $y ; Value: (MBS("DynaPDF.GetPageHeight"; $pdf) - $h)/2 ]
    Set Variable [ $r ; Value: MBS("DynaPDF.InsertImage"; $pdf; Watermark pages::WatermarkPicture; $x; $y; $w; $h) ]
    # End page
    Set Variable [ $r ; Value: MBS("DynaPDF.EndPage"; $pdf) ]
    Exit Loop If [ $count < $index ]
End Loop
# Set title
Set Variable [ $r ; Value: MBS("DynaPDF.SetDocInfo"; $pdf; "Title"; "Test PDF") ]
# Save
Set Variable [ $PDFData ; Value: MBS("DynaPDF.Save"; $pdf; "hello.pdf") ]
Set Variable [ $r ; Value: MBS("DynaPDF.Release"; $pdf) ]
# Put in Container
Set Field [ Watermark pages::OutputPDF ; $PDFData ]

Place an image centered on the page with border:

Set Variable [ $r ; Value: MBS("DynaPDF.AppendPage"; $pdf) ]
# put a background in
Set Variable [ $r ; Value: MBS("DynaPDF.SetFillColor"; $pdf; ,8; ,8; ,8) ]
Set Variable [ $r ; Value: MBS("DynaPDF.Rectangle"; $pdf; 0; 0; MBS( "DynaPDF.GetPageWidth"; $PDF ); MBS( "DynaPDF.GetPageHeight"; $PDF ); "fill") ]
# options for saving image
Set Variable [ $r ; Value: MBS("DynaPDF.SetSaveNewImageFormat"; $pdf; 0) ]
Set Variable [ $r ; Value: MBS("DynaPDF.SetResolution"; $pdf; 300) ]
# read size of image
Set Variable [ $r ; Value: MBS("DynaPDF.ReadImageFormat"; $pdf; Merge PDFs::InputImage) ]
Set Variable [ $ImageWidth ; Value: GetAsNumber (LeftValues ( $r ; 1 )) ]
Set Variable [ $imageHeight ; Value: GetAsNumber (MiddleValues ( $r ; 2; 1 )) ]
# The destination area where to put PDF
Set Variable [ $TargetX ; Value: 50 ]
Set Variable [ $TargetY ; Value: 50 ]
Set Variable [ $TargetWidth ; Value: MBS( "DynaPDF.GetPageWidth"; $PDF ) - 100 ]
Set Variable [ $TargetHeight ; Value: MBS( "DynaPDF.GetPageHeight"; $PDF ) - 100 ]
# calculate scale factor
Set Variable [ $factor ; Value: Min( $TargetHeight / $ImageHeight; $TargetWidth / $ImageWidth) ]
# calculate output size
Set Variable [ $DestWidth ; Value: $ImageWidth * $factor ]
Set Variable [ $DestHeight ; Value: $ImageHeight * $factor ]
# Center in target area
Set Variable [ $DestX ; Value: $TargetX + ($TargetWidth-$DestWidth) / 2 ]
Set Variable [ $DestY ; Value: $TargetY + ($TargetHeight-$DestHeight) / 2 ]
# insert the image
Set Variable [ $r ; Value: MBS("DynaPDF.InsertImage"; $pdf; Merge PDFs::InputImage; $DestX; $DestY; $DestWidth; $DestHeight) ]
# close page and move to next record
Set Variable [ $r ; Value: MBS("DynaPDF.EndPage"; $pdf) ]

See also

Release notes

Example Databases

Blog Entries

This function checks for a license.

Created 18th August 2014, last changed 13th July 2023


DynaPDF.InsertBookmarkEx - DynaPDF.InsertImageFile