1 Reply Latest reply on May 8, 2014 1:16 PM by gkaiseril

    Insert pdf into a Portfolio via Javascript

    Ark619

      Hi,

       

       

      I'm hoping someone can help me, as I am struggling to find an answer for this. Admittedly, I am new to Acrobat JS. I've checked the Adobe Javascript API, and I can't seem to find out how to do it. The only collection methods all relate to fields, and the properties are fields, initialDoc and initialview. I can't seem to see how to add a doc at all.

       

       

      In my searches, I did find this utility: New Feature added to Joel’s PDF Portfolio Utilities: Version 1.7 – The PDF Developer Junkie Blog 

       

       

      He does provide an interesting function in javascript: "Combine all open PDF portfolio's"

       

       

      Checking the source code on that I see the following:

       

       

      function ADBE_JFG_CombineCollections()

      {

        //get an array of all of the open documents

        var docs = trustedActiveDocs();

        if (docs.length > 0)

        {

         //Create a new empty portfolio

         newPortfolio = trustedNewCollection();

         //create a thermometer

         t = app.thermometer;

         t.duration = docs.length;

         t.begin();

         i=0

         //loop through each open document

         for each (doc in docs)

         {

         t.value = i

         //if the document is a Portfolio... (we won't add single PDFs)

         if (doc.collection)

         {

        //first add all the fields that we need

        for each (f in doc.collection.fields)

        {

        try

        {

        newPortfolio.collection.addField({name:f.name, text:f.text, type:f.type});

        console.println("Field named "+f.name+" added");

        }

        catch (err)

        {

        console.println("Field named "+f.name+" already present.");

        }

        }

        for each (item in doc.dataObjects)

        {

        t.text = "Inserting file "+item.name;

        newPortfolio.createDataObject(item.name, "");

        newPortfolio.setDataObjectContents(item.name, doc.getDataObjectContents(item.name));

        for each (f in doc.collection.fields)

        {

        newPortfolio.getDataObject(item.name).setFieldValue(f.name, item.getFieldValue(f.name));

        }

       

        }

        }

         }

        }

      t.end()

      }

       

       

      The problem is, his code will only work on portfolio's that exist already. I tried to modify it as a test to see if I could cobble something together to fit my needs and did the following:

       

       

      function portTest()

      {

        var docs = trustedActiveDocs();

       

        newPortfolio = trustedNewCollection();

       

        //loop through all open docs

        if (docs.length > 0 )

        {

        t = app.thermometer

        t.duration = docs.length;

        t.begin();

        i=0

        for each (doc in docs)

        {

        t.value = i

        t.text = "Inserting file "+doc.documentFileName;

        newPortfolio.createDataObject(doc.documentFileName, "");

        newPortfolio.setDataObjectContents(doc.documentFileName, doc.getDataObjectContents(doc.documentFileName));

        }

        }

        t.end();

      }

       

       

      This was unsuccessful, causing adobe to raise an internal error if attempted as a script, and the debugger gives me an error as well. If I comment out this line:

       

       

      newPortfolio.setDataObjectContents(doc.documentFileName, doc.getDataObjectContents(doc.documentFileName));

       

       

      The script runs, creating a new portfolio, and inserting an entry for a new doc, but that entry has no contents. As far as the API seems to document, getDataObjectContents() seems to be used to read external files (which I assume Acrobat treats docs in a portfolio/collection as an attachment and that's why Joel's code works? I am unclear on this though). At this point, I am lost... how can I get the doc(s) I want to be inserted into a portfolio? I thought it would be a lot easier than what this has turned out to be, and unfortunately there isn't a plethora of resources on the subject. Sorry, I've been working on this for four days, and keep spinning my wheels. Thank you in advance for any help or pointers you could offer.

        • 1. Re: Insert pdf into a Portfolio via Javascript
          gkaiseril MVP & Adobe Community Professional

          You probably are too new to ObjectOrientated languages and JavaScript.

           

          The sample code you provided does not include the other functions that Joel is using to populate an an array variable whose elements or cells contain the object name for the opened PDF files.

           

          The line of code:

           

          var docs = trustedActiveDocs();

           

          Calls the "trustedActiceDocs" function. This function uses Acrobat's app.activeDoc object array to populate the "docs" variable.

           

          And there are even more supporting functions that Joel has declared as part of the entire script file.