Programatic manipulation of annotations

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

AndreaCCS
User
Posts: 51
Joined: Wed Jun 16, 2010 2:54 pm

Programatic manipulation of annotations

Post by AndreaCCS »

I have been asked to implement adding a toolbar to support a limited set of annotations. The functions I need are Callout (#33201), Highlight (#33221), Black Filled box and Delete. The first 2 are easy I can execute the command number in shown in brackets.

So I have two questions:
  • How can I create a command to make a black filled box annotation where the user draws it.
  • The delete command (I have tried #33136) always says "Command is disabled currently". I have set the state to enabled but I am not sure if the annotation is un-selected when I click on my toolbar. How can I enable it so I can get it to delete the (last) selected annotation. Or is there another way to delete the selected annotation.
Finally in the Comments Pane can I disable / hide the tools.
Andrea
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Programatic manipulation of annotations

Post by Stefan - PDF-XChange »

Hello Andrea,

Are you adding this toolbar inside the Viewer AX or in the main body of your application?
If it's in the body of the application - you can call a small JS that will place the desired annotation where the user clicked, and if it's a toolbar as available inside the Viewer control - then simply create the desired custom toolbar in the end user Viewer, then export the settings to a data file. You can then later load those settings as the first thing after the Viewer AX loads, and lock all toolbars and comments.

Regards,
Stefan
AndreaCCS
User
Posts: 51
Joined: Wed Jun 16, 2010 2:54 pm

Re: Programatic manipulation of annotations

Post by AndreaCCS »

It is a toolbar in my application. Can you give me some sample JS for the delete and also for how to do the custom text box. With the text box you need to change the cursor etc to show that they are drawing a box annotation so not sure how to do that.
Andrea
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Programatic manipulation of annotations

Post by Vasyl - PDF-XChange »

Hi, Andrea.

To create new annotation you may use JS:

Code: Select all

var pageIndex = this.pageNum;
var annot = this.addAnnot({
page: pageIndex,
type: "Square",
name: "myBlackRect99",
rect: [400, 400, 550, 500],
fillColor: color.black,
strokeColor: color.black});
To delete any annotation you can use JS:

Code: Select all

// by name
var a = this.getAnnot(pageIndex, "myBlackRect99");
if (a != null)
   a.destroy();
// or by [pageIndex, annotIndexOnPage]:
var aa = this.getAnnots(pageIndex);
if ((aa.length != 0) && (annotIndexOnPage >= 0) && (annotIndexOnPage < aa.length))
     aa[annotIndexOnPage].destroy();
To select annotation and delete it:

Code: Select all

// select by name(unique):
pdfViewer.DoDocumentVerb(docID, "", "SelectAnnot", dataIn("myBlackRect99"), null, PXCVA_Sync);
// or select by [pageIndex, annotIndexOnPage]
pdfViewer.DoDocumentVerb(docID, "", "SelectAnnot", dataIn(pageIndex, annotIndexOnPage), null, PXCVA_Sync);
// delete the selection:
pdfViewer.DoVerb("", "ExecuteCommand", dataIn("Delete"), null, PXCVA_Sync);
Finally in the Comments Pane can I disable / hide the tools.
- you may disable any UI-command item by:

Code: Select all

pdfViewer.SetProperty("Commands[#CmdID].State", dataIn("Offline"), 0);
// or by name
pdfViewer.SetProperty("Commands["CmdName"].State", dataIn("Offline"), 0);
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.
AndreaCCS
User
Posts: 51
Joined: Wed Jun 16, 2010 2:54 pm

Re: Programatic manipulation of annotations

Post by AndreaCCS »

Thank you for these answers.

How can you tell which is the currently selected annotation as it might not be the annotation you last put on so you won't know the name.
Andrea
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Programatic manipulation of annotations

Post by Vasyl - PDF-XChange »

You may use the:

Code: Select all

object dataOut;
pdfViewer.DoDocumentVerb(docID, "", "GetSelectedAnnot", dataIn, out dataOut, PXCVA_Sync);
int pageIndex = dataOut[0];
int annotIndexOnPage = dataOut[1];
// OR - get name of selected annot (note: exists unnamed annots)
pdfViewer.DoDocumentVerb(docID, "", "GetSelectedAnnotName", dataIn, out dataOut, PXCVA_Sync);
string annotName = dataOut;
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.
AndreaCCS
User
Posts: 51
Joined: Wed Jun 16, 2010 2:54 pm

Re: Programatic manipulation of annotations

Post by AndreaCCS »

Thank you again I will try it tomorrow as it is late here in the UK now.

By the way is there a date for the new version of the SDK viewer/editor
Andrea
User avatar
John - Tracker Supp
Site Admin
Posts: 5223
Joined: Tue Jun 29, 2004 10:34 am

Re: Programatic manipulation of annotations

Post by John - Tracker Supp »

Not at this time - we are currently stabilising the End User release and once this is satisfactorily done we will start to work on the SDK documentation and examples - this will for sure not be before the years end...
If posting files to this forum - you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded - thank you.

Best regards
Tracker Support
http://www.tracker-software.com
AndreaCCS
User
Posts: 51
Joined: Wed Jun 16, 2010 2:54 pm

Re: Programatic manipulation of annotations

Post by AndreaCCS »

Having issues getting the delete selected annotation to work. Can you get the currently selected annotation in Javascript? Then I could destroy it.
Andrea
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Programatic manipulation of annotations

Post by Stefan - PDF-XChange »

Hi Andrea,

Yes using JS should allow that:

Code: Select all

var aAnnots = this.selectedAnnots;
for (var i=0; i < aAnnots.length; i++)
console.println(aAnnots[i].contents);
Regards,
Stefan
AndreaCCS
User
Posts: 51
Joined: Wed Jun 16, 2010 2:54 pm

Re: Programatic manipulation of annotations

Post by AndreaCCS »

I have a test document with a lot of annotations on multiple pages. The GetSelectedAnnot returns the correct pageIndex but the annotIndexOnPage returns a number that is higher than the number of annotations on the page. I am returning the contents so I know it was the correct one selected or the number of annots on the page. The annotIndexOnPage is coming back as more than the number of annots on the page! Can you confirm if the GetSelectedAnnot is returning the wrong number

Code: Select all

            object dataOut;

                axCoPDFXcview64.DoVerb("Documents[0]", "GetSelectedAnnot", null, out dataOut, (int)Interop.PDFXCviewAxLib64.PXCVA_Flags.PXCVA_Sync);

            int pageIndex = ((int[])dataOut)[0];
            int annotIndexOnPage = ((int[])dataOut)[1];

            string res = "";
            if (pageIndex != -1 && annotIndexOnPage > 0)
            {
                        string script = "var annots = this.getAnnots(" + pageIndex + "); " + 
                                "var res = annots.length; " +
                                "if ( (annots != null) && (" + annotIndexOnPage + " < annots.length)) " + 
                                "{ " + 
                                    "var sel= annots[" + annotIndexOnPage +"]; " +
                                    "res = sel.contents; " +
                                    "sel.destroy();" +
                                "}" +
                                " this.Results=res ";
                try
                {
                        axCoPDFXcview64.RunJavaScript(script, out res, 0, (int)Interop.PDFXCviewAxLib64.PXCVA_Flags.PXCVA_NoUI);
                }
                catch (Exception ex)
                {
                    ShowErrorMessage(System.Runtime.InteropServices.Marshal.GetHRForException(ex), ex);
                }
            }
Andrea
AndreaCCS
User
Posts: 51
Joined: Wed Jun 16, 2010 2:54 pm

Re: Programatic manipulation of annotations

Post by AndreaCCS »

Thanks just in case anyone else is having the same issue this javascript works to delete the annotation

Code: Select all

            string script = "var annots = this.selectedAnnots; " +
                            "if (annots != null) " +
                            " for (var i = 0; i < annots.length; i++) " +
                            "{ " +
                                "annots[i].destroy();" +
                            "}";
Andrea
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Programatic manipulation of annotations

Post by Stefan - PDF-XChange »

Glad you found the solution AndreaCCS!

Cheers,
Stefan
AndreaCCS
User
Posts: 51
Joined: Wed Jun 16, 2010 2:54 pm

Re: Programatic manipulation of annotations

Post by AndreaCCS »

The last problem I have with this is knowing when the user has an annotation selected so that I can enable / disable my delete annotation button. Any help in achieving this will be appreciated.
Andrea
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Programatic manipulation of annotations

Post by Stefan - PDF-XChange »

Hi Andrea,

Will this help:
https://help.pdf-xchange.com/DEV/de ... _selection

Regards,
Stefan
AndreaCCS
User
Posts: 51
Joined: Wed Jun 16, 2010 2:54 pm

Re: Programatic manipulation of annotations

Post by AndreaCCS »

Looks good will try and get it to work.
Andrea
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Programatic manipulation of annotations

Post by Vasyl - PDF-XChange »

Hi, Andrea.
The GetSelectedAnnot returns the correct pageIndex but the annotIndexOnPage returns a number that is higher than the number of annotations on the page.
It occurs because JS-document's method 'getAnnots' returns you the list of markups only, but page can contain also hyperlinks that are annotations too but not markups (markups: rectangle, circle, line etc.). So, to remove correctly the selected annotation(of any type):

Code: Select all

axCoPDFXcview.DoDocumentVerb(0, "", "GetSelectedAnnot", null, out dataOut, (int)Interop.PDFXCviewAxLib64.PXCVA_Flags.PXCVA_Sync);
int pageIndex = ((int[])dataOut)[0];
if (pageIndex >= 0) // we have selected annot
{
   try
   {
        object dataIn = "Delete";
        axCoPDFXcview.DoVerb("", "ExecuteCommand", dataIn, 0, (int)Interop.PDFXCviewAxLib64.PXCVA_Flags.PXCVA_Sync);
   }
   catch (Exception ex)
   {
       ;;
   }
}
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.
AndreaCCS
User
Posts: 51
Joined: Wed Jun 16, 2010 2:54 pm

Re: Programatic manipulation of annotations

Post by AndreaCCS »

Another related issue. I want to be able to show the properties box for the selected annotation. I have tried executing command 33244 but this does not seem to work. I have tried it in your full demo and can't get it to work from there either.
Andrea
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Programatic manipulation of annotations

Post by Vasyl - PDF-XChange »

Use the command ID=33116 ("ToolProperties") instead..
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.
vzgherea
User
Posts: 23
Joined: Mon Sep 09, 2013 8:38 am

Re: Programatic manipulation of annotations

Post by vzgherea »

Hello,

How to disable manipulating of annotations? (marking all document as readonly is not a solution as it should be possible to rotate pages)

Thank you.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Programatic manipulation of annotations

Post by Stefan - PDF-XChange »

Hi vzgherea,

You could disable any command you do not want your users to have access to and leave active only the ones you want.
Please check the How to Disable a Command? section of the manual:
https://help.pdf-xchange.com/DEV/de ... _a_command

Regards,
Stefan