Get rectangle annotation after creation

PDF-XChange Viewer SDK for Developer's
(ActiveX and Simple DLL Versions)

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

KarlTH
User
Posts: 57
Joined: Mon Aug 01, 2005 2:33 pm

Get rectangle annotation after creation

Post by KarlTH »

Hello to all,

I am using the PDF-XChange Viewer SDK with VB .NET 4.0 for a proof of concept to show that it can be used with electronic signatures. I use there the rectangle annotation to get the coordinates for a signature field which I am adding with a webservice. At the moment I am catching the event name "Global::OperationsHistoryChanged" and watch for new annotations but it would be better if I can get the rectangle annotation directly after creation.
Here is some code snippet as an explanation:

Code: Select all

    Private Sub butPDFSign_Click(sender As System.Object, e As System.EventArgs) Handles butPDFSign.Click
        Dim oDataIn As Object
        Dim oDataOut As Object
        'e. g. Dim mRectangleAnnot As AnnotRectangle

        oDataIn = 33125
        oDataOut = 0

        AxPDFXViewCtrl.DoVerb("", "ExecuteCommand", oDataIn, oDataOut, 0)
        'After this it would be nice if oDataOut would return the rectangle annotation or if I can
        'query an event if the rectangle has been drawn
        'e.g. mRectangleAnnot = AxPDFXViewCtrl.GetAnnot(oDataOut)
        'e.g. Console.WriteLine(mRectangleAnnot .Left.ToString)
    End Sub
Is there any possibility to get back the rectangle annotation object or at least get the coordinates?


Thanks a lot,

Karl
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2445
Joined: Thu Jun 30, 2005 4:11 pm

Re: Get rectangle annotation after creation

Post by Vasyl - PDF-XChange »

Hi, Karl.

Yes, it is possible, the example(pseudocode):

Code: Select all

int AnnotNameToIndex(int docID, int pageIndex, string annotName)
{
    variant dataIn;
    variant dataOut;  
    string pageName = "Pages[" + pageIndex + "]";  
    pdfViewer.DoDocumentVerb(docID, pageName, "GetAnnotsCount", dataIn, dataOut, 0);
    int annotsCount = (int)dataOut;  
    for (int i = 0; i < annotsCount; i++)
    {
       dataIn = i;
       dataOut = ""; // clear
       pdfViewer.DoDocumentVerb(docID, pageName, "GetAnnotName", dataIn, dataOut, 0);    
       if ((string)dataOut == annotName)
          return i;
    }
    return -1;  
}

yourEventHandler::OnEvent(type, name...)
{
     if ((type == PXCVA_OnNamedNotify) AND (name == "Notifications.NewAnnotAdded"))
     {
          int docID; 
          int pageIndex; 
          int isTemp = 0; 
          string annotName;
          pdfViewer.GetProperty(name + ".Temp", out isTemp);
          if (isTemp == 0) // (isTemp != 0) - the creation of annot isn't finished
          {
               pdfViewer.GetProperty(name + ".DocID", out docID);
               pdfViewer.GetProperty(name + ".PageIndex", out pageIndex);
               pdfViewer.GetProperty(name + ".Name", out annotName);

               variant dataIn;
               variant dataOut;
               dataIn = AnnotNameToIndex(docID, pageIndex, annotName);
               pdfViewer.DoDocumentVerb(docID, "Pages[" + pageIndex + "]", "GetAnnotRect", dataIn, dataOut, 0);

               double left = dataOut[0];
               double top = dataOut[1];
               double right = dataOut[2];
               double bottom = dataOut[3];

               ...... 
          }
     }
}
HTH
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
KarlTH
User
Posts: 57
Joined: Mon Aug 01, 2005 2:33 pm

Re: Get rectangle annotation after creation

Post by KarlTH »

Hello Vasyl,

thank you very much for you fast response and sorry for my late answer :evil: ! I have used your pseudocode but I did not get the event "Notifications.NewAnnotAdded".

This are the events what I get when I open a pdf and create a rectangle annotation:
Global::OperationsHistoryChanged
Global::FocusGained
International.LocaleID
View.Bars["CommentAndMarkup"].Visible
Global::BeginModal
Global::EndModal
Documents.Count
Documents[#4095].Pages.Width
Documents[#4095].Pages.Height
Documents.Active
Global::OperationsHistoryChanged
Documents[#4095].ContentReady
Global::FocusGained
Global::BeginModal
Global::FocusGained
Global::EndModal
Global::FocusGained
Global::FocusGained
Global::FocusGained
Global::FocusGained
Global::FocusGained
Global::FocusGained
Global::FocusGained
Global::FocusGained

May it be possible that I have to activate a notification to get the event "Notifications.NewAnnotAdded"?

Code: Select all

AxPDFXViewCtrl.SetProperty("Notifications.Selection.Filter", "All", 0) 
For me this notification did not worked! Do you have an idea what I am doing wrong?


Thanks,

Karl
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19868
Joined: Mon Jan 12, 2009 8:07 am

Re: Get rectangle annotation after creation

Post by Stefan - PDF-XChange »

Hello Karl,

Can you make sure that you are using build 210 of the Viewer SDK?
Also if you can include the actual code you are using (after reworkign the pseudocode sample Vasyl provided) we will take a look.

Please make sure that there's not Licensing information visible in the source!

Best,
Stefan
KarlTH
User
Posts: 57
Joined: Mon Aug 01, 2005 2:33 pm

Re: Get rectangle annotation after creation

Post by KarlTH »

Hello Stefan,

sometimes it's the simple things :lol:! You were right, I was using build 207 and with build 210 it works!

Here is my VB .NET code, maybe useful to other users:

Code: Select all

    Private Sub AxPDFXViewCtrl_OnEvent(sender As Object, e As AxPDFXCviewAxLib._IPDFXCviewEvents_OnEventEvent) Handles AxPDFXViewCtrl.OnEvent
        Dim oDataIn As Object
        Dim oDataOut As Object
        Dim iPageIndex As Integer
        Dim iAnnotHasBeenCreated As Integer
        Dim strAnnotName As String
        Dim dblPosition As Double() = New Double() {0, 0, 0, 0}


        oDataIn = 0
        oDataOut = Nothing

        If intPDocCurrentID > 0 Then
            If e.type = PDFXCviewAxLib.PXCVA_EventTypes.PXCVA_OnNamedNotify And e.name = "Notifications.NewAnnotAdded" Then

                AxPDFXViewCtrl.GetProperty(e.name + ".Temp", oDataOut)
                iAnnotHasBeenCreated = DirectCast(oDataOut, Integer)
                If iAnnotHasBeenCreated = 0 Then '// (isTemp != 0) - the creation of annot isn't finished
                    'AxPDFXViewCtrl.GetProperty(e.name + ".DocID", oDataOut)
                    'intPDocCurrentID = DirectCast(oDataOut, Integer)

                    AxPDFXViewCtrl.GetProperty(e.name + ".PageIndex", oDataOut)
                    iPageIndex = DirectCast(oDataOut, Integer)

                    AxPDFXViewCtrl.GetProperty(e.name + ".Name", oDataOut)
                    strAnnotName = DirectCast(oDataOut, String)

                    oDataIn = AnnotNameToIndex(intPDocCurrentID, iPageIndex, strAnnotName)
                    AxPDFXViewCtrl.DoDocumentVerb(intPDocCurrentID, "Pages[" + iPageIndex.ToString + "]", "GetAnnotRect", oDataIn, oDataOut, 0)

                    If oDataOut Is Nothing = True Then
                        iSigFieldValues = New Integer() {0, 0, 0, 0}
                    End If
                    If oDataOut.GetType.Name <> "Double[]" Then
                        iSigFieldValues = New Integer() {0, 0, 0, 0}
                    End If


                    dblPosition = TryCast(oDataOut, Double())
                    ReDim iSigFieldValues(dblPosition.Length)
                    For iPosition = 0 To dblPosition.Length - 1
                        iSigFieldValues(iPosition) = Convert.ToInt32(dblPosition(iPosition))
                    Next


                    Console.WriteLine("Positions of signature fields")
                    Console.WriteLine("--------------------------------------")
                    Console.WriteLine("Left:" + iSigFieldValues(0).ToString)
                    Console.WriteLine("Top:" + iSigFieldValues(1).ToString)
                    Console.WriteLine("Right:" + iSigFieldValues(2).ToString)
                    Console.WriteLine("Bottom:" + iSigFieldValues(3).ToString)

                End If
            End If
        End If
        Console.WriteLine(e.name)
    End Sub

    Private Function AnnotNameToIndex(ByVal DocID As Integer, ByVal PageIndex As Integer, ByVal AnnotName As String) As Integer
        Dim oDataIn As Object
        Dim oDataOut As Object
        Dim strPageName As String
        Dim iAnnot As Integer
        Dim iAnnots As Integer

        oDataIn = Nothing
        oDataOut = Nothing

        strPageName = "Pages[" + PageIndex.ToString + "]"
        AxPDFXViewCtrl.DoDocumentVerb(DocID, strPageName, "GetAnnotsCount", oDataIn, oDataOut, 0)
        iAnnots = DirectCast(oDataOut, Integer)
        For iAnnot = 0 To iAnnots - 1
            oDataIn = iAnnot
            oDataOut = ""
            AxPDFXViewCtrl.DoDocumentVerb(DocID, strPageName, "GetAnnotName", oDataIn, oDataOut, 0)

            If DirectCast(oDataOut, String) = AnnotName Then
                Return iAnnot
            End If
        Next

        Return -1
    End Function

Thank you so much!

Karl
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19868
Joined: Mon Jan 12, 2009 8:07 am

Re: Get rectangle annotation after creation

Post by Stefan - PDF-XChange »

Glad to hear that Karl,

And hopefully the code would be useful to someone else!

Cheers,
Stefan