« EngageU - FileMaker C… | Home | MBS FileMaker Plugin,… »

Place image in PDF

Today we look into drawing a picture into a PDF document. If you develop in Xojo, you may first look into PDFDocument class. There you can use DrawPicture and pass a picture to draw. This stores the image inside the PDF and shows it on a page.

There are different things to consider when placing an image:

  • Does the image come from picture or from file or data in memory?
  • What resolution does the image has and what resolution should it have in the PDF document?
  • Does the image gets compressed into the PDF?
  • What colorspace has the image and in what colorspace should it be stored in the PDF?

Using PDFDocument class

In Xojo's PDFDocument class, we can draw a picture into the PDF page. We scale down the image by passing a rectangle for the output area and the source area in the picture. This will add the picture to the PDF.

var g as Graphics Var f As FolderItem // we place an image today var imageFile as new FolderItem("IMG_4601.jpeg") Var pic As Picture = Picture.Open(imageFile) // try Xojo and make a new PDF Var doc As New PDFDocument // and get graphics for drawing to the page g = doc.Graphics f = new FolderItem("test2.pdf") // we pick a font, which supports all the characters g.FontName = "Helvetica" g.FontSize = 20 g.DrawText "Xojo " + XojoVersionString, 50, 100 // place the image and scale down Var w As Integer = g.Width var h as integer = w * (pic.Height / pic.Width) g.DrawPicture pic, 0, 150, w, h, 0, 0, pic.Width, pic.Height doc.Save f // show file f.Launch

This will create a PDF with the full resolution of the given image. The resulting PDF file is over 22 MB big.

Using DynaPDFMBS class

Let's do the same with our graphics class integration in MBS Xojo DynaPDF Plugin. There you can use DrawPicture method to draw the picture on the PDF page. We have plenty of other methods to insert images.

The DrawPicture with a picture object will insert a RGB image.

// now try with DynaPDF Var pdf As New MyDynapdfMBS // For this example you can use a Starter, Lite, Pro or Enterprise License pdf.SetLicenseKey "Starter" // write to this file f = new FolderItem("test1.pdf") Call pdf.CreateNewPDF f // new page Call pdf.Append // if you need higher resolution than 150 dpi Call pdf.SetResolution(600) // we use graphics class to draw, so we can use same commands as above for Xojo g = pdf.PageGraphics g.FontName = "Helvetica" g.FontSize = 20 g.DrawText "DynaPDF " + DynaPDFMBS.GetDynaPDFVersion, 50, 100 // place the image and scale down g.DrawPicture pic, 0, 150, w, h, 0, 0, pic.Width, pic.Height // close page and file Call pdf.EndPage Call pdf.CloseFile // show file f.Launch

By default DynaPDF will scale down images to 150 dpi if needed. This avoids bloated PDF files by default. If you need a higher maximum resolution, you can call SetResolution() with the desired number. Like 600 dpi for later printing on a laser printer.

If you like to change the color space, you can do that with DynaPDF using SetColorspace() function. Or change how images get compressed with SetCompressionFilter() to specify whether you like JPEG or Flate compression.

Results

For the test we use a JPEG file with 4 MB. The resulting PDF sizes are:

PDFDocument 22.2 MB
DynaPDF with 150 dpi 224 KB
DynaPDF with 600 dpi 1.8 MB

As you see you have more control for compression with our MBS Xojo DynaPDF Plugin. You can get a license for DynaPDF Starter with OmegaBundle.

11 08 25 - 10:40