Save picture from PDF: UnsupportedOperationException

I try to use the following code, from documentation page: Picture — Xojo documentation

Var imageData As String
Var bs As BinaryStream
Var f As FolderItem
If ImageWell1.Image <> Nil Then
  ' Get a temporary file to save the image to
  If Picture.IsExportFormatSupported(Picture.Formats.JPEG) Then
    f = SpecialFolder.Temporary.Child("TempImage.jpg")

    ' Save the image out to the file
    ImageWell1.Image.Save(f, Picture.Formats.JPEG)
  End If

  ' Open the file as a BinaryStream and read the data in
  bs = BinaryStream.Open(f, False)
  If bs <> Nil Then
    imageData = bs.Read(bs.Length)
    bs.Close
  End If

  ' delete the temporary file if it exists
  If f.Exists Then
    f.Delete
  End If
End If

I need to adapt this code, my image comes from a pdf file. When I execute this code I have an exception on the save line:

UnsupportedOperationException Error: Saving a multi-representation Picture is not supported

Var p As Picture
Var tmp As FolderItem
p = Picture.Open(file)

ImageViewer1.Image = p

If Picture.IsExportFormatSupported(Picture.Formats.JPEG) Then
  tmp = SpecialFolder.Temporary.Child("TempImage.jpg")
  'tmp = SpecialFolder.Desktop.Child("TEST.jpg")
  
  ' Save the image out to the file
  Try
    p.Save(tmp, Picture.Formats.JPEG)
    
  Catch error As UnsupportedOperationException
    MessageBox("UnsupportedOperationException Error: " + error.Message)
  End Try
End If

Any idea?

You can use Picture.ImageAt to pull out a single picture from a multiple representation picture.

https://documentation.xojo.com/api/graphics/picture.html#picture-imageat

Like the docs say though

The individual pictures aren’t necessarily stored/returned in ascending scalefactor order. Do not rely on a particular order for the pictures.

So you’ll need to iterate through the representations and figure out which one has the highest resolution, if that’s what you’re after.

Or maybe you could use Picture.BestRepresentation and pass in the width and height of the picture with 2.0 as the scale.

https://documentation.xojo.com/api/graphics/picture.html#picture-bestrepresentation

I try the 2 solutions with the same error.

Var pic As  Picture
'pic = p.BestRepresentation(100, 100, 32.0)
pic = p.ImageAt(0)

If Picture.IsExportFormatSupported(Picture.Formats.JPEG) Then
  Var tmp As FolderItem = SpecialFolder.Temporary.Child("FirstPage.jpg")
  Try
    pic.Save(tmp, Picture.Formats.JPEG)
    MessageBox("PDF first page saved as JPEG in: " + tmp.NativePath)
  Catch error As UnsupportedOperationException
    MessageBox("Save failed: " + error.Message)
  End Try
Else
  MessageBox("JPEG export is not supported on this platform.")
End If

I made this test, and imageViewer show a valid image as expected.

Var p As Picture
p = Picture.Open(file)
ImageViewer1.Image = p.BestRepresentation(100, 100, 32.0)

I also made this test, and got ant OutOfBoundsException p.image.Count return 0 in the console…

System.DebugLog(p.ImageCount.ToString)
ImageViewer1.Image = p.ImageAt(0)

Is file your PDF by chance? Xojo doesn’t actually support reading PDFs, which one of the benefits of @Christian_Schmitz DynaPDF plugin.

1 Like

This isn’t quite true. It’s more accurate to say “Xojo’s image support mirrors that of the underlying operating system,” because macOS actually does support directly loading a PDF. The following code, run on macOS, loads a PDF into a Picture object and images it onto another Picture object:

dim p as picture

dim f as FolderItem

f=FolderItem.ShowOpenFileDialog("")

p=picture.Open(f)

dim o as picture

o=new picture(p.Width, p.Height)

o.Graphics.DrawPicture p, 0, 0

break
1 Like

Solution Code from @Eric_Williams, thank you Eric

Var origin As Picture
origin = Picture.Open(file)

If origin= Nil Then
  MessageBox("Error: Failed to open PDF. PDF to Picture is likely unsupported on this platform.")
  Return
End If

Var destination as Picture
destination = New Picture(origin.Width, origin.Height)
destination.Graphics.DrawPicture (origin, 0, 0)
ImageViewer1.Image = destination

If Picture.IsExportFormatSupported(Picture.Formats.JPEG) Then
  'Var tmp As FolderItem = SpecialFolder.Temporary.Child("tmp_img.jpg")
  Var tmp As FolderItem = SpecialFolder.Desktop.Child("tmp_img.jpg")
  Try
    destination.Save(tmp, Picture.Formats.JPEG)
    MessageBox("PDF first page saved as JPEG in: " + tmp.NativePath)
  Catch error As UnsupportedOperationException
    MessageBox("Save failed: " + error.Message)
  End Try
Else
  MessageBox("JPEG export is not supported on this platform.")
End If

As mentioned in the other thread, I err on the side of safety.

I am always learning, and love to find out when things now work :slight_smile: