Jump to content

Pass an array as Object to the COM function


Recommended Posts

I am stucked with an example trying to use a function from the well known free pdfforge library that comes with the PDFCreator package.

Here is the function ilustrated in an example:

;Create PDFForge.dll instance
$PdfForge = ObjCreate("pdfforge.pdf.pdf")

;Declare Array
Global $imageFilenames[3]

;Filling the values
$imageFilenames[0]=@WorkingDir &"\9697.tif"
$imageFilenames[1]=@WorkingDir &"\9730.tif"
$imageFilenames[2]=@WorkingDir &"\9743.tif"

;Calling the function
$PdfForge.Images2PDF_2($imageFilenames, "output.pdf", 1)

When it reaches the last line throws an error:

Variable must be of type "Object"
$PdfForge.Images2PDF_2($imageFilenames, "output.pdf", 1)

The question is how to pass the array as an object to the function?

From the pdfforge library help :

pdfforge.dll help file

Images2PDF Method (Object[], String, Int32)

Parameters

____________

sourceFilenames

Object[]

An array of image source filenames.

destinationFilename

String

Name of the pdf destination file.

scaleMode

Int32

0: The pdf page has the same size like the image. 1: The image will be fitted on an A4 page.

Return Value

0: If there was any error. Otherwise a positive number of the created pages.

And the example in VBScript:

Dim imageFilenames(2), tools, pdf
imageFilenames(0) = "image-red.png"
imageFilenames(1) = "image-green.png"
imageFilenames(2) = "image-blue.png"
Set tools = WScript.CreateObject("pdfforge.tools")
tools.CreateTestImage imageFilenames(0), 255, 0, 0
tools.CreateTestImage imageFilenames(1), 0, 255, 0
tools.CreateTestImage imageFilenames(2), 0, 0, 255
Set pdf = WScript.CreateObject("pdfforge.pdf.pdf")
pdf.Images2PDF_2 (imageFilenames), "output1.pdf", 0
pdf.Images2PDF_2 (imageFilenames), "output2.pdf", 1
Set tools = Nothing
Set pdf = Nothing

Learn, learn and ... learn

Link to comment
Share on other sites

After: $PdfForge = ObjCreate("pdfforge.pdf.pdf")

What is the value of $PdfForge? And what is @error?

Your script 100% worked perfectly for me so that suggests maybe your object isn't initially getting created...

Add: In addition, verify that your source TIF files/images exist.

Edited by MrMitchell
Link to comment
Share on other sites

After: $PdfForge = ObjCreate("pdfforge.pdf.pdf")

What is the value of $PdfForge? And what is @error?

Your script 100% worked perfectly for me so that suggests maybe your object isn't initially getting created...

Add: In addition, verify that your source TIF files/images exist.

It´s very strange ...

I have this (modified the code because I work in various places)

;Create PDFForge.dll instance
$PdfForge = ObjCreate("pdfforge.pdf.pdf")
If IsObj($PdfForge) Then MsgBox(0, "Info", "Is an object")

;Declare Array
Global $imageFilenames[2]

;Filling the values
$imageFilenames[0]=@WorkingDir &"\Img2.tif"
$imageFilenames[1]=@WorkingDir &"\Img3.tif"


;Calling the function
$PdfForge.Images2PDF_2($imageFilenames, "output.pdf", 1)

The error thrown by AutoIt:

D:\PDFFORGE.au3 (14) : ==> The requested action with this object has failed.:
$PdfForge.Images2PDF_2($imageFilenames, "output.pdf", 1)
$PdfForge.Images2PDF_2($imageFilenames, "output.pdf", 1)^ ERROR
->07:09:18 AutoIT3.exe ended.rc:1
>Exit code: 1    Time: 6.123

The files are there. The object has been created well. But ...

And anothe strange thing. This code (original by paz modified by me) works very well in the uncompiled version but when I compile it throws me :

Line 2950: (File: "D:\Tiff2PDF.exe"):
Error: The requested action with this object has failed.

The Code:

#include <WinAPI.au3>
;~ #include <AutoItErrorHandler_UDF_NoStd.au3>
;Open a dialog to choose the TIF file
$input = FileOpenDialog("Choose a TIFF file", @ScriptDir, "TIFF files (*.TIF)")
;Create and initialize a var. with the name of the output file
$output = StringTrimRight($input,4) & "_"
;Store in a var. the full path and name to FreeImage library
$FI_DLL = @ScriptDir & "\FreeImage.dll"
;Create the PdfForge library instance
$PdfForge = ObjCreate("pdfforge.pdf.pdf")
;Declare the TIF constant
Const $FIF_TIFF = 18
If FileExists($input) then
    If FileExists($FI_DLL) then
    ;Opens the FreeImage DLL
        $FI = DllOpen($FI_DLL)
    ;Initialize the library (mandatory according to the documentation0
        DllCall($FI,"none","_FreeImage_Initialise@4","dword",1)
    ;Opens the input TIFF file.
        $TIFF_IMAGE = DllCall($FI,"long_ptr","_FreeImage_OpenMultiBitmap@24","dword", $FIF_TIFF, "str", $input, "dword", 0, "dword", 1, "dword", 0, "dword", 0)
    ;Gets the number of pages from the TIFF image
        $PAGE_COUNT = DllCall($FI, "int", "_FreeImage_GetPageCount@4", "long_ptr", $TIFF_IMAGE[0])
    ;Page index are 0-based. For a loop, I'll need to go from 0 to (PAGE_COUNT -1)
        $PAGE_COUNT = $PAGE_COUNT[0] -1
    ;Array to put the images ref.
        Dim $imageFilenames[$PAGE_COUNT+1]
    ;The way FreeImage lib works, you need to "lock" a single page at a time to work with it.
        For $i = 0 To $PAGE_COUNT
        ;Get a page from the multi-page TIFF file
            $BITMAP = DllCall($FI, "long_ptr", "_FreeImage_LockPage@8", "long_ptr", $TIFF_IMAGE[0], "dword", $i)
        ;Create random name Var
            $Name = Random(999, 9999, 1)
        ;Save it as a JPG file
            $ret = DllCall($FI, "dword", "_FreeImage_Save@16","dword", $FIF_TIFF, "long_ptr", $BITMAP[0], "str", $name &".tiff", "dword", 0)
        ;Save the name into array
            $imageFilenames[$i] = @ScriptDir &"\" &$name &".tiff"
        ;Unlock the page.
            $ret = DllCall($FI, "none", "_FreeImage_UnlockPage@12", "long_ptr", $TIFF_IMAGE[0], "long_ptr", $BITMAP[0], "dword", 0)
        ;Free the RAM?
            $BITMAP = 0
        Next
    ;Integrate images to new PDF
        $aRes = $PdfForge.Images2PDF_2($imageFilenames, StringTrimRight($input, 4) &".pdf", 1)
;~      ConsoleWrite('Images2PDF_2 = ' & $aRes & @crlf & '>Error code: ' & @error & @crlf)
        MsgBox(0,"",'Images2PDF_2 = ' & $aRes & @crlf & '>Error code: ' & @error & @crlf)
    ;Close the TIFF file
        $ret = DllCall($FI,"long","_FreeImage_CloseMultiBitmap@8","long_ptr", $TIFF_IMAGE[0], "dword", 0)
    ;Again, if I do not reassign some variables like this, I usually have a crash when the program ends.
        $TIFF_IMAGE = 0
    ;This will "DeInitialize" the FreeImage lib
        DllCall($FI,"none","_FreeImage_DeInitialise@0")
    ;The process has been finished
        MsgBox(64,"Info","Process is done." &StringTrimRight($input, 4) &".pdf has been created with " &$PAGE_COUNT+1 &" pages from " &StringTrimRight($input, 4) &" file")
    Else
    ;We didn´t find the FreeImage library
        MsgBox(16,"FreeImage DLL not found","The FreeImage DLL was not found in the script directory." & @crlf & "(" & $FI_DLL & ")" & @crlf & @crlf & "You can get if from this website:" & @crlf & "http://freeimage.sourceforge.net/download.html")
    EndIf
Else
;We didn´t find the TIF file
    MsgBox(16,"File not found","You should have a TIFF file named " & $input & " in this script directory. Check this and try again")
EndIf
    ;We delete the TEMP pages
        FileDelete(@ScriptDir &"\*.tiff")

And when I modify it (delete the winapi.au3 declaration and it´s functions from my script) the error goes to the line 26.

Modified Code:

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Version=beta
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Run_Obfuscator=y
#AutoIt3Wrapper_Run_cvsWrapper=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
;#include <WinAPI.au3>
;~ #include <AutoItErrorHandler_UDF_NoStd.au3>
;Open a dialog to choose the TIF file
$input = FileOpenDialog("Choose a TIFF file", @ScriptDir, "TIFF files (*.TIF)")
;Create and initialize a var. with the name of the output file
$output = StringTrimRight($input, 4) & "_"
;Store in a var. the full path and name to FreeImage library
$FI_DLL = @ScriptDir & "\FreeImage.dll"
;Create the PdfForge library instance
$PdfForge = ObjCreate("pdfforge.pdf.pdf")
;Declare the TIF constant
Const $FIF_TIFF = 18
If FileExists($input) Then
    If FileExists($FI_DLL) Then
        ;Opens the FreeImage DLL
        $FI = DllOpen($FI_DLL)
        ;Initialize the library (mandatory according to the documentation0
        DllCall($FI, "none", "_FreeImage_Initialise@4", "dword", 1)
        ;Opens the input TIFF file.
        $TIFF_IMAGE = DllCall($FI, "long_ptr", "_FreeImage_OpenMultiBitmap@24", "dword", $FIF_TIFF, "str", $input, "dword", 0, "dword", 1, "dword", 0, "dword", 0)
        ;Gets the number of pages from the TIFF image
        $PAGE_COUNT = DllCall($FI, "int", "_FreeImage_GetPageCount@4", "long_ptr", $TIFF_IMAGE[0])
        ;Page index are 0-based. For a loop, I'll need to go from 0 to (PAGE_COUNT -1)
        $PAGE_COUNT = $PAGE_COUNT[0] - 1
        ;Array to put the images ref.
        Dim $imageFilenames[$PAGE_COUNT + 1]
        ;The way FreeImage lib works, you need to "lock" a single page at a time to work with it.
        For $i = 0 To $PAGE_COUNT
            ;Get a page from the multi-page TIFF file
            $BITMAP = DllCall($FI, "long_ptr", "_FreeImage_LockPage@8", "long_ptr", $TIFF_IMAGE[0], "dword", $i)
            ;Create random name Var
            $Name = Random(999, 9999, 1)
            ;Save it as a JPG file
            $ret = DllCall($FI, "dword", "_FreeImage_Save@16", "dword", $FIF_TIFF, "long_ptr", $BITMAP[0], "str", $Name & ".tiff", "dword", 0)
            ;Save the name into array
            $imageFilenames[$i] = @ScriptDir & "\" & $Name & ".tiff"
            ;Unlock the page.
            $ret = DllCall($FI, "none", "_FreeImage_UnlockPage@12", "long_ptr", $TIFF_IMAGE[0], "long_ptr", $BITMAP[0], "dword", 0)
            ;Free the RAM?
            $BITMAP = 0
        Next
        ;Integrate images to new PDF
        $aRes = $PdfForge.Images2PDF_2($imageFilenames, StringTrimRight($input, 4) & ".pdf", 1)
;~      ConsoleWrite('Images2PDF_2 = ' & $aRes & @crlf & '>Error code: ' & @error & @crlf)
        ;MsgBox(0, "", 'Images2PDF_2 = ' & $aRes & @CRLF & '>Error code: ' & @error & @CRLF)
        ;Close the TIFF file
        $ret = DllCall($FI, "long", "_FreeImage_CloseMultiBitmap@8", "long_ptr", $TIFF_IMAGE[0], "dword", 0)
        ;Again, if I do not reassign some variables like this, I usually have a crash when the program ends.
        $TIFF_IMAGE = 0
        ;This will "DeInitialize" the FreeImage lib
        DllCall($FI, "none", "_FreeImage_DeInitialise@0")
        ;The process has been finished
        MsgBox(64, "Info", "Process is done." & StringTrimRight($input, 4) & ".pdf has been created with " & $PAGE_COUNT + 1 & " pages from " & StringTrimRight($input, 4) & " file")
    Else
        ;We didn´t find the FreeImage library
        MsgBox(16, "FreeImage DLL not found", "The FreeImage DLL was not found in the script directory." & @CRLF & "(" & $FI_DLL & ")" & @CRLF & @CRLF & "You can get if from this website:" & @CRLF & "http://freeimage.sourceforge.net/download.html")
    EndIf
Else
    ;We didn´t find the TIF file
    MsgBox(16, "File not found", "You should have a TIFF file named " & $input & " in this script directory. Check this and try again")
EndIf
;We delete the TEMP pages
FileDelete(@ScriptDir &"\*.tiff")
Edited by realshyfox

Learn, learn and ... learn

Link to comment
Share on other sites

Are we still working on Images2PDF or are we now moving on to FreeImage?

I compiled the Images2PDF script and ran it and got no errors. Whether compiled or not the only way I can reproduce your exact error is to relocate the compiled exe (therefore the script won't find the TIF files) or remove/rename one of the TIF files.

Link to comment
Share on other sites

I post my exe here:

img2pdf.rar

And here is the script:

img2pdf.au3

Well ... I don´t know. :alien: If this won´t work, I shall use a3x format but I don´t know if I can pass params to it as it´s only a compiled version of the source file. ;)

It´s strange that source file works and the exe throws me errors.

Very, very strange... :huh2:

Learn, learn and ... learn

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...