Help with particular C++ Syntax for obtaining a list of obj

If you are a C/C++/C# developer and have any questions about using our products from your application here is the place to post them.
Post Reply
CodespringsMike
Posts: 8
Joined: Tue Jan 20 2004

Help with particular C++ Syntax for obtaining a list of obj

Post by CodespringsMike »

Hello. I have been evaluating the software and am now ready to start implementation. While evaluating, I was using the following VB Code to retrieve and set particular objects from a page:

Code: Select all

    Dim pdfobj As Object
    Dim objarray As Variant
    
    Set pdfobj = CreateObject("PDFCreactiveX.PDFCreactiveX")
    pdfobj.SetLicenseKey key, lisence
    
    pdfobj.Open "TestIn.pdf", ""
    
    pdfobj.CurrentPage = 1
    objarray = pdfobj.ObjectAttribute("Pages[1]", "Objects")
    For Each obj In objarray
        If obj.Attribute("ObjectType") = 6 Then
            If obj.Attribute("Name") = "acField1" Then
                obj.Attribute("Text") = "This is a test message"
            End If
        End If
    Next

    pdfobj.Save "TestOut.pdf", 0
    
    MsgBox "Done"
    Set pdfobj = Nothing
This works fine for VB but I now need to do the same thing in C++. This is what I have thus far:

Code: Select all

  long r	= 0;
  tagVARIANT objarray;
  IPDFCreactiveX pdfobj;
  COleException *pException = NULL;
	
  LPCTSTR company = "Us";
  LPCTSTR key = "MyKeyNUmber";

  pdfobj.CreateDispatch("PDFCreactiveX.PDFCreactiveX", pException);
  pdfobj.SetLicenseKey(company, key);


  r = pdfobj.Open("TestIn.pdf","");
  pdfobj.SetCurrentPage(1);
	
  objarray = pdfobj.GetObjectAttribute("Pages[1]", "Objects");

This is where I am not sure how to retrieve the values from the tagVARIANT type.

Thanks for any suggestions.
Jose
Amyuni Team
Posts: 553
Joined: Tue Oct 01 2002
Contact:

Post by Jose »

Hello,

Below is the equivalent of the Visual Basic code which was sent to you. Hope this helps?

void CPDFCreatorVer150Dlg::OnBtnGetObjects()
{
m_ctlPDF1.SetLicenseKey("Amyuni Eval Version",
"07EFCDAB010001009192DA7C4AFCB52193D3541522CBD5EF451FFD6245B8B18E2A08EAF4D5F197009712755CC1B5A1EC51D08ED655F45B027CA20F8865B12FF0C20F79EEF7DD6CA1C79A832D74039439F15D4D3CDC1B85F753BEA835EA57FFB3CEC53E10");
m_ctlPDF1.Open ("c:\\temp\\PDF1.PDF", "");



VARIANT arObjects = m_ctlPDF1.GetObjectAttribute("Pages[1]","Objects");


SAFEARRAY* pObjects = V_ARRAY(&arObjects);

LONG lstart, lend;
SafeArrayGetLBound( pObjects, 1, &lstart );
SafeArrayGetUBound( pObjects, 1, &lend );


long rgIndices[1];
LPDISPATCH pObject = NULL;
rgIndices[0] = 0;

::SafeArrayLock(pObjects);
HRESULT iResult = ::SafeArrayGetElement(pObjects, rgIndices, ( void **) & pObject);
CacObject object( pObject );

TRY
{
//Retrieve value of Text attribute
VARIANT text = object.GetAttribute( "Text" );
if ( text.bstrVal )
AfxMessageBox( CString( text.bstrVal ) );
}
CATCH( COleDispatchException, e )
{
}
END_CATCH

::SafeArrayUnlock(pObjects);

}

Thanks
CodespringsMike
Posts: 8
Joined: Tue Jan 20 2004

Sample code not working

Post by CodespringsMike »

Jose,

This snippet is giving me an exception error when I try to use it. When I walk through the debugger, I see the lstart = 0 and lend = -1, telling me that the GetObjectAttribute call is not returning any objects. I verified that the document is being opened successfully. Not sure how to proceed.

Any advice would be appreciated.

Thanks Again,

Mike W.
CodespringsMike
Posts: 8
Joined: Tue Jan 20 2004

Forgot details...

Post by CodespringsMike »

I forgot to tell you where the Access Violation is occurring, when the code gets to the line:

AfxMessageBox( CString( text.bstrVal ) );

The program throws an Unhandled Exception error but I think this is simply because the text.bstrVal has not bee set to equal anything.

Thanks Again,

Mike W.
Jose
Amyuni Team
Posts: 553
Joined: Tue Oct 01 2002
Contact:

Post by Jose »

Hello,

Yes, you are right. That line will throw an error if the does not contain any data. The code sample is just to illustrate how to programmatically retrieve data from a PDF document but of course it can be improved.

Thanks
Post Reply