IT-Consultant: José Roca (PBWIN 10+/PBCC 6+) (Archive only) > Graphics and Multimedia

Using GDI+ to add image to a PDF document

(1/3) > >>

Rick Kelly:
I have my own class that creates PDF files and want to add image support. I can use PB binary reads and read the entire file and parse through it and was interested in getting away from that as well as being able to use the GDI+ functions to load an image of various types, interrogate the image for something I can understand and then inserting the image properly into the PDF document stream.

Is this something the GDI+ support include files can handle?

Rick Kelly

Edwin Knoppert:
Not sure what exact filetypes pdf handles but should at least be jpg.
I forgot but when text's are inserted binary they usually inserted as zlib compressed string buffer (stream) which is easy to do.
Maybe images are handled similarly.

Would you like to share your class to generate the pdf as it is right now, just for my own use?
I know the basics how pdf works but it is some work to prepare code.

Rick Kelly:

--- Quote from: Edwin Knoppert on May 05, 2014, 11:37:21 AM ---Not sure what exact filetypes pdf handles but should at least be jpg.
I forgot but when text's are inserted binary they usually inserted as zlib compressed string buffer (stream) which is easy to do.
Maybe images are handled similarly.

Would you like to share your class to generate the pdf as it is right now, just for my own use?
I know the basics how pdf works but it is some work to prepare code.

--- End quote ---

I'm in the cleanup/polishing up phase. My interpretation is mainly for what I need - things such as text, lines and images so doesn't have all the bells and whistles.

Here is how I am capturing image data to use later on when generating the PDF file stream. PB10 and, of course, Jose's includes ;D


--- Code: ---TYPE ImageAttributes
    HorizontalResolution    AS SINGLE
    VerticalResolution      AS SINGLE
    ImagePixelHeight        AS LONG
    ImagePixelWidth         AS LONG
    ImageHeight             AS DOUBLE
    ImageWidth              AS DOUBLE
    ImageSize               AS LONG
    ImageDescriptorObject   AS LONG
END TYPE

%PDF_MEASUREMENT                    = 72

INSTANCE ImageDescriptor            AS IPOWERCOLLECTION
INSTANCE ImageStream                AS IPOWERCOLLECTION 

    LET ImageDescriptor = CLASS "PowerCollection"
    LET ImageStream = CLASS "PowerCollection"




------------------------------

   METHOD AddImage(BYVAL sImageFile AS STRING) AS STRING

' Add a jpeg image
' If return string is blank, an error occurred

   LOCAL sImageID           AS WSTRING
   LOCAL hStatus            AS LONG
   LOCAL nPixelFormat       AS LONG
   LOCAL hToken             AS DWORD
   LOCAL StartupInput       AS GdiplusStartupInput
   LOCAL pImage             AS DWORD
   LOCAL szImageFile        AS WSTRINGZ * %MAX_PATH
   LOCAL ImageGUID          AS GUID
   LOCAL uImageAttributes   AS ImageAttributes
   LOCAL nFile              AS INTEGER
   LOCAL nFileSize          AS LONG
   LOCAL sFileContents      AS STRING
   LOCAL nTotalImages       AS LONG
   LOCAL nWidth             AS LONG
   LOCAL nHeight            AS LONG

    sImageID = ""
    szImageFile = sImageFile

' Startup GDI+

    StartupInput.GdiplusVersion = 1
    hStatus = GdiplusStartup(hToken, StartupInput, BYVAL %NULL)

    IF hStatus <> %S_OK THEN

       MSGBOX "Unable to start graphics subsystem."
       METHOD = ""
       EXIT METHOD

    END IF

' Check if image file is valid

    IF NOT ISFILE(szImageFile) THEN

       MSGBOX szImageFile + " not found."
       METHOD = ""
       EXIT METHOD

    END IF

' Load Image file

    hStatus = GdipLoadImageFromFile(szImageFile, pImage)

    IF hStatus <> %S_OK THEN

       MSGBOX szImageFile + " is not a recognized image file."
       GOTO AddImageExit

    END IF

' Check if image format if JPEG

    hStatus = GdipGetImageRawFormat(pImage,ImageGUID)

    IF ImageGUID <> $ImageFormatJPEG THEN

       MSGBOX szImageFile + " is not a JPEG image file."
       GdipDisposeImage pImage
       GOTO AddImageExit

    END IF

' Check color depth

    hStatus = GdipGetImagePixelFormat(pImage,nPixelFormat)

    IF nPixelFormat <> %PixelFormat24bppRGB THEN

       MSGBOX "JPEG color depth is not supported."
       GdipDisposeImage pImage
       GOTO AddImageExit

    END IF

    GdipGetImageHorizontalResolution(pImage,uImageAttributes.HorizontalResolution)
    GdipGetImageVerticalResolution(pImage,uImageAttributes.VerticalResolution)
    GdipGetImageWidth(pImage,uImageAttributes.ImagePixelWidth)
    GdipGetImageHeight(pImage,uImageAttributes.ImagePixelHeight)
    uImageAttributes.ImageWidth = uImageAttributes.ImagePixelWidth * %PDF_MEASUREMENT / uImageAttributes.HorizontalResolution
    uImageAttributes.ImageHeight = uImageAttributes.ImagePixelHeight * %PDF_MEASUREMENT / uImageAttributes.VerticalResolution

' Release GDI+ Image

    GdipDisposeImage pImage

' Get file size and raw stream

    nFile = FREEFILE
    OPEN szImageFile FOR BINARY AS nFile

    uImageAttributes.ImageSize = LOF(nFile)
    GET$ nFile, uImageAttributes.ImageSize, sFileContents

    CLOSE nFile

' Save Image for reference

    nTotalImages = ImageDescriptor.Count
    INCR nTotalImages

    sImageID = "I" + FORMAT$(nTotalImages)

    ImageDescriptor.Add(sImageID,uImageAttributes AS STRING)
    ImageStream.Add(sImageID,sFileContents)

    AddImageExit:

' Shutdown GDI+

    GdiplusShutdown hToken

    METHOD = sImageID

   END METHOD

--- End code ---

The image object is then created and looks like this:

6 0 obj
<<
/Type /XObject
/Subtype /Image
/Filter /DCTDecode
/ColorSpace /DeviceRGB
/Width 133
/Height 129
/BitsPerComponent 8
/Length 9782
>>
stream
????? Raw JPEG stream
endstream
endobj

All my images are XOBJECT items and the PDF Procedure set then looks like (includes any referenced fonts as well):

8 0 obj
<< /ProcSet [ /PDF /Text /ImageC]
/XObject <<
/I1 6 0 R
>>
/Font <<
/F1 5 0 R
>>
>>
endobj


I'll post at attachment with the class when I'm done testing and am ready to begin using it myself.

Rick Kelly

Rick Kelly:
I decided if you wanted to take a look at a "work in progress", I have no real issue with that. There is still a lot of work I've got to do. There are two includes and I combined them into one file.

Rick Kelly

Edwin Knoppert:
stream
?? Raw JPEG stream
endstream

If so you can try the zlib string (compress)ion method (and thus not the other, zip file creation, functions).
Imo the output of that is placed 1:1 into the pdf.
I don't know and i am not going to investigate further :)

I have messed with text streams this to remove the trail message from a pdf :)

Navigation

[0] Message Index

[#] Next page

Go to full version