Insert image options with DynaPDF
You tried MBS Xojo DynaPDF Plugin and wonder what options you can use for inserting images?
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:
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:
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.
If you place the same image again, DynaPDF can detect the duplicate and just add a reference internally.
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.
If you set color space for the page to CMYK, DynaPDF will convert the image to CMYK color space for you:
But if you use gfUseImageColorSpace flag, the image can be placed with keeping its own colorspace and stays RGB, while the page is CMYK:
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.
Once we are done, let's close the file and open it in a PDF Viewer
Let us know if you have questions. You can get a DynaPDF Starter license with OmegaBundle 2023.