How to access bookmarks using C#

Ask a Question related to Adobe Acrobat SDK, Design and Development.

  1. #1

    Default How to access bookmarks using C#

    Hi,

    I'm trying to access the list of bookmarks in a PDF. I've written my application in C#, and I'm having trouble figuring out how to access this part of a PDF document.

    From reading the documentation, this forum, and a few other sources - my understanding is that the collection of bookmarks may only be access using C++, or JavaScript. (I'd love to be proven wrong here though...)

    If I want to access the bookmarks from within my C# app, it seems that the advice is to use the JSObject from within my app.

    I've seen a few examples around for VB, and lots for VBS, but I can't get them to work for C#.

    Basically, as far as I get is:

    AcroAVDoc acroAVDoc = new AcroAVDoc();
    acroAVDoc.Open(inputFile, "");
    CAcroPDDoc acroDoc = (CAcroPDDoc)acroAVDoc.GetPDDoc();
    Object jsObj = pdDoc.GetJSObject();

    This is where I get stuck. I don't know how to call into the javascript library from here. Looking at a few examples for VB, the answer seems to be something like:

    bookMarkRoot = jsObj.BookmarkRoot

    But that won't compile for C#. (Neither will bookMarkRoot = jsObj.BookmarkRoot();)

    What am I missing here?

    Thanks in advance for the help.
    holmesd@adobeforums.com Guest

  2. Similar Questions and Discussions

    1. Bookmarks
      Where do I locate the IE bookmarks in system X? and what do they call them in that file? Thanks
    2. asp.net and bookmarks
      I need to be able to add bookmarks programmatically via an asp.net web page. Is it feasible? If so, what would be the best way? I am new to Adobe...
    3. PDF to PDF & bookmarks
      We are creating big pdf files from framemaker. 1st printfiles in high resolution. Then we need the same pdf files in low resolution. Both pdf...
    4. Bookmarks in 6 are bad!
      I have made many pdf's with Acrobat version 5, and I commonly use bookmarks that are just single digits (ie: 1 2 3 ...). When I open these pdf's in...
    5. Bookmarks?
      I created a website that works great. However, I have a page that I would like to include bookmarks so that users can select an item from a table...
  3. #2

    Default Re: How to access bookmarks using C#



    But that won't compile for C#.




    Yup. Because the JSObject is late-bound in C# you need to use the InvokeMember helper method.
    PDL@adobeforums.com Guest

  4. #3

    Default Re: How to access bookmarks using C#

    Thanks PDL. That was tremendously helpful.
    I have two more related questions now.

    First, is there anyway to execute the javascript without opening a new Acrobat window? (The code I have forces Acrobat to load.)

    Second, I think I'm still missing something when calling the javascript.

    I now have:

    AcroAVDoc acroAVDoc = new AcroAVDoc();
    acroAVDoc.Open(inputFile, "");
    CAcroPDDoc acroDoc = (CAcroPDDoc)acroAVDoc.GetPDDoc();
    Object jsObj = pdDoc.GetJSObject();

    object[] param = new object[1];
    param[0] = "";

    object ret =
    JsObj.GetType().InvokeMember("this.bookmarkRoot.ch ildren[0]",
    System.Reflection.BindingFlags.InvokeMethod, null, jsObj,param);

    However, this returns an unknown member exception (or something similar). I'm fairly certain that I should be able to call bookmarkRoot.children[0]. I suspect that I'm not correctly referencing the document though. I've tried this.bookmarkRoot, my.bookmarkRoot, doc.bookmarkRoot and just bookmartRoot, but none of them are found. Is there some other way to get a handle on the default pdf?

    Thanks again in advance.

    (Aandi, your suggestion is probably perfectly valid, but if I did that, I would be using COM, C#, VB and Javascript in a single app... perhaps a bit much for me. :) )
    holmesd@adobeforums.com Guest

  5. #4

    Default Re: How to access bookmarks using C#

    You need to use just "bookmarkRoot.children[0]", but you're using the InvokeMethod binding flag - this is not a method, it is a property, so use the GetProperty binding flag instead.

    Here is a list of the available binding flags:
    <http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx>
    PDL@adobeforums.com Guest

  6. #5

    Default Re: How to access bookmarks using C#

    You may want to simplify things by making use of document-level functions. Write your JS in a document-level function like:

    function GetFirstBookmark( oDoc )
    {
    return oDoc.bookmarkRoot.children[0];
    }

    Then you can call this from your C#:

    object[] param = new object[1];
    param[0] = jsObj; // This is your JavaScript Doc object

    object ret =
    jsObj.GetType().InvokeMember("GetFirstBookmark",
    System.Reflection.BindingFlags.InvokeMethod, null, jsObj,param);
    PDL@adobeforums.com Guest

  7. #6

    Default Re: How to access bookmarks using C#

    The JavaScript interface is for VB. You might want to create a VB
    application as a bridge.

    Aandi Inston
    Aandi_Inston@adobeforums.com Guest

  8. #7

    Default Re: How to access bookmarks using C#

    ...... jsObj.GetType().InvokeMember("GetFirstBookmark", ....

    That may become very cumbersome if you have a nested bm-tree.
    (Is that correct english?)

    I would check if you can use: ExecuteThisJavaScript (in Acroabt FORMS reference).
    Then you can write the AJS-code "dump all bookmarks" (in JS-Helpfile) to a C# variable and execute the AJS-code. Read the AJS variable for the bm-tree via jso into C# and do whatever you want with that.

    HTH, Reinhard
    Reinhard_Franke@adobeforums.com Guest

  9. #8

    Default Re: How to access bookmarks using C#



    (Is that correct english?)




    Yup! :)

    I was only providing the "GetFirstBookmark" function as an example. My intent was for them to have all the JS in document-level functions, then just invoke those functions as necessary. Much less cumbersome than doing the whole thing through either InvokeMember or ExecuteThisJavaScript.
    PDL@adobeforums.com Guest

  10. #9

    Default Re: How to access bookmarks using C#

    Huzzah!!!

    It took me a while to figure out how to actually return a value back to the C# app. Getting a method to run was really easy, but actually getting a value back from a property took me a bit. I ended up with the following code.

    AcroAVDoc acroAVDoc = new AcroAVDoc();
    acroAVDoc.Open(inputFile, "");
    CAcroPDDoc acroDoc = (CAcroPDDoc)acroAVDoc.GetPDDoc();

    // Create a IAFormApp object. Required in order to create
    // a Fields object.
    IAFormApp formApp = new AFormAppClass();

    // Get the IFields object associated with the form
    IFields acrofields = (IFields)formApp.Fields;

    // Values are returned to the app through the event. You
    // must assign the return value to event.value in order
    // to get something other than a null value.
    String ret = acrofields.ExecuteThisJavascript(
    "event.value = this.bookmarkRoot.children[0].name;");
    Console.Out.WriteLine("Bookmark: " + ret.ToString());
    acroAVDoc.Close(0);

    Thanks PDL, Reinhard.
    holmesd@adobeforums.com Guest

  11. #10

    Default How to access bookmarks using C#

    What are the references needed to use iField?
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139