« Merge documents with … | Home | First OmegaBundle ord… »

Insert image options with DynaPDF

You tried MBS Xojo DynaPDF Plugin and wonder what options you can use for inserting images?

Same image at various resolutions.

With DynaPDF you have full control about how images are placed. By default the plugin does what most people need: insert images with 150 dpi and JPEG compression into a PDF. The resolution is calculated based on how big the image is shown on a page.

But you can decide:

  • Whether to pass through JPEG images.
  • What maximum resolution to use, e.g. 300 dpi.
  • Which compression filter to use for compressing images, e.g. JPEG.
  • Whether to mask white pixels with SetUseTransparency.
  • What compression quality to use for JPEG.
  • Whether to use image colorspace or convert to current colorspace.
  • Whether to allow duplicate image check or disable it (with SetGStateFlags).

Let's go through various combination in sample code below by starting with this code:

Dim d As New MyDynapdfMBS dim file as FolderItem=SpecialFolder.Desktop.Child("PDF with picture.pdf") d.SetLicenseKey "Starter" // For this example you can use a Starter, Lite, Pro or Enterprise License Dim ImageFile As FolderItem = SpecialFolder.Desktop.Child("test.jpg") Call d.CreateNewPDF file

Before you insert an image, you may want to read the size with ReadImageFormat function. It will return an error, if DynaPDF can't read the image format. On success, you learn what pixel size the image has, what bit depth and whether it uses zip compression. Bits per pixel may be useful to decide compression algorithm. For 1 bit images, you may do different one than for 24 bit RGB images or 32 bit RGBA images. Let's check image format here:

// DynaPDF can insert BMP, GIF, JPEG, TIFF, JP2, JPC, JPX, PBM, PGM, PNM, PPM, PNG and preview images from PSD files. // size in pixels Dim imageWidth As Integer Dim imageHeigth As Integer // bits per pixel, e.g. 24 bit for RGB Dim bitsPerPixel As Integer // whether this is zip compressed Dim imageUseZip As Boolean If d.ReadImageFormat(ImageFile, imageWidth, imageHeigth, bitsPerPixel, imageUseZip) Then // okay Else // can't read end if

Let us simply place an image. You specify which file to be placed to what X/Y coordinate on the page and what size to use as rectangle to show the image. You can pass -1 for width or height to use original value from the file. Passing zero for width or height can ask DynaPDF to calculate the other value proportional. The pixel size is calculated based on this.

// the image is inserted with default settings // 150 dpi and converted to current color space Call d.Append Call d.InsertImage(100, 100, 200, 200, ImageFile) Call d.EndPage // -> 417 by 417 pixel image as JPEG in PDF, about 8 KBytes

If you place the same image again, DynaPDF can detect the duplicate and just add a reference internally.

// same image on second page. // DynaPDF will detect a duplicate and only put image once into PDF, then references it on both pages Call d.Append Call d.InsertImage(100, 100, 200, 200, ImageFile) Call d.EndPage

You can request images to be passed through if possible in JPEG image and keep original resolution. If DynaPDF doesn't save the image in a new format, it keeps the original data. This is of course not available for some image formats as source, but JPEG can be passed through as well as a lot of tiff images.

And here we also disable transparency of white pixels. That is a great way to quickly mask images and remove whitespace around the image content.

// now we ask for pass through if possible for the image // will keep original resolution // avoid turning white to transparent with SetUseTransparency(false) Call d.Append Call d.SetSaveNewImageFormat(False) Call d.SetUseTransparency(False) Call d.InsertImage(100, 100, 200, 200, ImageFile) Call d.EndPage

If you set color space for the page to CMYK, DynaPDF will convert the image to CMYK color space for you:

// now we ask for CMYK conversion, but limit to 300 dpi and JPEG compressed if possible // we ask for 80% quality on JPEG Call d.Append Call d.SetColorSpace(d.kcsDeviceCMYK) Call d.SetResolution(300) Call d.SetUseTransparency(False) Call d.SetCompressionFilter(d.kcfJPEG) Call d.SetJPEGQuality(80) Call d.InsertImage(100, 100, 200, 200, ImageFile) Call d.EndPage // -> CMYK image at 833 by 833 pixels JPEG compressed.

But if you use gfUseImageColorSpace flag, the image can be placed with keeping its own colorspace and stays RGB, while the page is CMYK:

// same, but we request to keep image color space, so no colorspace conversion. // and DynaPDF can calculate the height proportional as we pass zero Call d.Append Call d.SetColorSpace(d.kcsDeviceCMYK) Call d.SetGStateFlags(d.kgfUseImageColorSpace, False) Call d.SetResolution(300) Call d.SetUseTransparency(False) Call d.SetCompressionFilter(d.kcfJPEG) Call d.InsertImage(100, 100, 200, 0, ImageFile) Call d.EndPage // -> sRGB image at 833 by 833 pixels JPEG compressed.

Instead of a file, you can of course use InsertImageFromBuffer function and pass image data as String or MemoryBlock. Since Xojo loves to complain about ambiguous parameters, we need to add .0 to the constants to make them doubles or use double variables to pass them.

// now we insert from buffer // duplicate check applies again // we pass coordinates with .0 to avoid Xojo complaining about call not matching signatures. Dim ImageStream As BinaryStream = BinaryStream.Open(ImageFile) Dim ImageBuffer As String = ImageStream.Read(ImageStream.Length) Call d.Append Call d.InsertImageFromBuffer(100.0, 100.0, 200.0, 0.0, ImageBuffer) Call d.EndPage

Once we are done, let's close the file and open it in a PDF Viewer

Call d.CloseFile file.Launch

Let us know if you have questions. You can get a DynaPDF Starter license with OmegaBundle 2023.

07 07 23 - 12:10