0 Replies Latest reply on Sep 28, 2015 10:12 PM by arasank86029633

    Export pdf script not working

    arasank86029633

      Hi Expert,

       

      Please any one help this is my script i am writing for export pdf.

       

      #targetengine main

       

      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

       

      var jOptions = ["PRINTER", "EXTRAIT PDF", "LOWRES PDF", "WEB PDF"];

       

      var actDoc;

      var arr = [];

      var fArr = [];

      var tfFntArr = [];

      var lArr = [];

      var mFntArr = [];

      var ovstData = [];

      var errArr = [];

      var pdfprst;

      var count;

      var pWidth;

      var myJrnl;

      var jrnlType;

      var myCheck = 0;

      var myProgsPanel;

      var myProgsText;

      var hiddenText;

       

      Array.prototype.contains = function(k) {

          for(p in this) {

              if(this[p].split(",")[0] === k)

                  return true;

                  if (p == this.length-1)

                      break;

                  }

          return false;

      }

       

       

      checkPresets(jOptions);

       

      if (app.documents.length != 0) {

          try {

              actDoc = app.activeDocument;

              }

          catch(_){

              while (app.documents.length != 0)

                  app.documents[0].close (SaveOptions.NO);

              alert ('Please run the tool once again!', "SMART_EXPORTER");

              endFunction();

              }

          }

      else {

          if (app.books.length != 0) {

              if (confirm ("Do you wish to create PDF for '" + app.activeBook.name + "' book document?", "", "SMART_EXPORTER")) {

                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

                  prosBookDocs (app.activeBook);

                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

                  endFunction();

                  }

              else

                  endFunction();

              }

          else {

              alert ("No documents are kept open!", "EXPORTER");

              endFunction();

          }

      }

       

       

      checkFonts(actDoc);

      checkLinks(actDoc);

      checkOverset(actDoc);

      hiddenText = getHiddenText(actDoc);

       

      if (fArr.length > 0 || lArr.length > 0)

          errArr.push ("Missing Fonts / Links found in this document!\rFont(s): " + fArr.length + "\r" + fArr.join("\r") + "\rLink(s): " + lArr.length + "\r" + lArr.join("\r"));

      if (tfFntArr.length > 0)

          errArr.push ("TrueType fonts found in this document!\rFont(s): " + tfFntArr.length + "\r" + tfFntArr.join("\r"));

      if (hiddenText != null)

          errArr.push ("Hidden text identified page(s) Nos.: " + hiddenText);

      if (ovstData.length > 0)

          errArr.push ("Overset text identified page(s) Nos.: " + ovstData.join(", "));

      if (errArr.length != 0) {

          alert (errArr.join("\r\r"), "SMART_EXPORTER");

          endFunction();

          }

       

      else

      {

         

             

          }

       

       

      function checkFonts(doc){

          var dFonts = doc.fonts;

          for (f=0; f<dFonts.length; f++){

              if (dFonts[f].status != FontStatus.INSTALLED){

                  fArr.push ("\t" + dFonts[f].name.replace(/\t/gi, " - "));

                  }

              else if (dFonts[f].fontType == FontTypes.TRUETYPE || dFonts[f].fontType == FontTypes.OPENTYPE_TT){

                  if (dFonts[f].name.replace(/\t/gi, " - ") != "Euclid Symbol - Regular")

                      tfFntArr.push ("\t" + dFonts[f].name.replace(/\t/gi, " - "));

                  }

              }

          return {mf: fArr, tf: tfFntArr};

      }

       

       

       

      function checkLinks(doc){

          var dLinks = doc.links;

          for (l=0; l<dLinks.length; l++){

              if (dLinks[l].status != LinkStatus.NORMAL){

                  lArr.push ("\t" + dLinks[l].name);

                  }

              }

          return lArr;

      }

       

       

      function checkPresets(){

          var mArr = [];

          var pdfPrsts = app.pdfExportPresets;

          for (p=0; p<jOptions.length; p++){

              if (pdfPrsts.item(jOptions[p]) == null && pdfPrsts.item("[" + jOptions[p] + "]") == null)

                  mArr.push(jOptions[p]);

          }

       

          if (mArr.length > 0) {

              alert ("Please load the following PDF preset(s) and try again!\r\r" + mArr.join("\r"), "EXPORTER");

              endFunction();

              }

      }

       

       

       

      //HiddenText

       

      function getHiddenText (doc){

          var actPage = doc.layoutWindows[0].activePage;

          var docPages = doc.pages;

          var myObjArr = [];

          var tmpObj = [];

          var myTObjArr = [];

          var hiddenWrds = [];

          var temp;

          doc.zeroPoint = [0,0];

       

          Array.prototype.intersects = function(/*bounds*/pib)

          //----------------------------

          // Return TRUE if this page intersects the bounds

          // <pib> == [top,left,bottom,right]

          {

          var b = this;

          return b[0]<=pib[2] && pib[0]<=b[2] &&

              b[1]<=pib[3] && pib[1]<=b[3];

          }

       

          for (pg=0; pg<docPages.length; pg++){

              var docPageItems = docPages[pg].pageItems;

              for (dp=0; dp<docPageItems.length; dp++){

                  app.select (docPageItems[dp]);

                  if (app.selection.length == 0)

                      continue;

                  var pSel = app.selection[0];

                  if (pSel.itemLayer.visible && pSel.textWrapPreferences.textWrapMode == TextWrapModes.NONE && pSel.constructor.name.match(/Group|Rectangle/gi) != null && isOpqObj (pSel, pSel.constructor.name)) {

                      tmpObj.push (pSel);

                      tmpObj.push (pSel.geometricBounds);

                      myObjArr.push (tmpObj);

                      tmpObj = [];

                      }

                  else if (pSel.constructor.name == "TextFrame" && pSel.itemLayer.visible && pSel.contents != "") {

                      myTObjArr.push (pSel);

                     }

                 }

       

              for (cp=0; cp<myTObjArr.length; cp++){

                  var tLines = myTObjArr[cp].texts[0].lines;

                  var tBounds = myTObjArr[cp].geometricBounds;

                  try {

                      bPoint:

                      for (X in myObjArr) {

                          if (tBounds.intersects(myObjArr[X][1])) {

                              for (tp=0; tp<tLines.length; tp++){

                                  var lcInsertion = (tLines[tp].characters[-1].insertionPoints[-1].lines[0] != tLines[tp]) ? tLines[tp].characters[-1].insertionPoints[-2] : tLines[tp].characters[-1].insertionPoints[-1];

                                  if ([(tLines[tp].baseline - tLines[tp].ascent),tLines[tp].insertionPoints[0].horizontalOffset,tLines[tp].baseline,lcI nsertion.horizontalOffset].intersects(myObjArr[X][1])) {

                                      app.select (tLines[tp]);

                                      var tWords = tLines[tp].characters;

                                      for (tw=0; tw<tWords.length; tw++) {

                                          var wcInsertion = (tWords[tw].characters[-1].insertionPoints[-1].lines[0] != tLines[tp]) ? tWords[tw].characters[-1].insertionPoints[-2] : tWords[tw].characters[-1].insertionPoints[-1];

                                          if ([(tWords[tw].baseline - tWords[tw].ascent),tWords[tw].insertionPoints[0].horizontalOffset,tWords[tw].baseline,wcI nsertion.horizontalOffset].intersects(myObjArr[X][1])) {

                                              app.activeDocument = doc;

                                              app.select (myObjArr[X][0]);

                                              myTObjArr[cp].select(SelectionOptions.ADD_TO);

                                              app.copy();

                                              var temp = app.documents.add();

                                              app.activeDocument = temp;

                                              app.paste();

                                              app.select (temp.textFrames[0]);

                                              if (app.menuActions.itemByID(11283).enabled)

                                                  hiddenWrds.push (tWords[tw].parentTextFrames[0].parentPage.name);

                                              temp.close(SaveOptions.NO);

                                              break bPoint;

                                          }

                                      }

                                  }

                              }

                          }

                      }

                  }

              catch(_){/*alert (_.toString())*/}

              }

              myTObjArr = [];

              myObjArr = [];

          }

          app.selection = null;

          doc.layoutWindows[0].activePage = actPage;

          if (hiddenWrds.length > 0) {

              return RemoveDuplicates (hiddenWrds).join(", ");

              }

          else

              return null;

      }

       

       

      function isOpqObj (obj, objType){

          var oCheck = 0;

          if (objType == "Group") {

              var gpItems = obj.allPageItems;

              for (gp=0; gp<gpItems.length; gp++) {

                  if (getColorProps (gpItems[gp]) || (gpItems[gp].constructor.name == "Rectangle" && gpItems[gp].allGraphics.length != 0))

                      oCheck = 1;

                  }

              return (oCheck == 1) ? true : false;

              }

          else if (objType == "Rectangle") {

              if (getColorProps (obj) || obj.allGraphics.length != 0)

                  return true;

              else

                  return false;

              }

          }

       

       

      function getColorProps (cObj){

          try {

              cObj.fillColor.colorValue;

              return true;

              }

          catch (_){ return false; }

          }

       

       

      function RemoveDuplicates(arr)

      {

          //get sorted array as input and returns the same array without duplicates.

          var result=new Array();

          var lastValue="";

          for (var i=0; i<arr.length; i++)

          {

              var curValue=arr[i];

              if (curValue != lastValue)

              {

                  result[result.length] = curValue;

              }

              lastValue=curValue;

          }

          return result;

      }

       

       

      //Overset

       

      function checkOverset(doc){

         

          var docStories = doc.stories;

          for (sv=0; sv<docStories.length; sv++){

              if (docStories[sv].overflows == true){

                  try {

                      var ovstPage = getOvstPage(docStories[sv]);

                      if (ovstPage.parent instanceof Spread)

                          ovstData.push(ovstPage.name);

                      }

                  catch(_){}

                  }

             

              for (tv=0; tv<docStories[sv].tables.length; tv++){

                  if (docStories[sv].tables[tv].parent.constructor.name == "TextFrame" || docStories[sv].tables[tv].parent.constructor.name == "Story"){

                      for (cv=0; cv<docStories[sv].tables[tv].cells.length; cv++){

                          if (docStories[sv].tables[tv].cells[cv].overflows == true){

                              try {

                                  var ovstCellPage = getOvstPage(docStories[sv].tables[tv].cells[cv]);

                              if (ovstCellPage.parent instanceof Spread)
                                          ovstData.push(ovstCellPage.name);

                                  }

                              catch(_){}

                              }

                          }

                      }

                  }

              }

          }

       

       

      function getOvstPage(theObj) {

          if (theObj.hasOwnProperty("baseline"))

              theObj = theObj.parentTextFrames[0];

       

          while (theObj != null) {

              if (theObj.hasOwnProperty ("parentPage")) return theObj.parentPage;

              var whatIsIt = theObj.constructor.name;

              switch (whatIsIt) {

                  case "Page": return theObj;

                  //case Spread : return "PB";

                  case "Character": theObj = theObj.parentTextFrames[0]; break;

                  case "Footnote":; // drop through

                  case "Cell": theObj = theObj.insertionPoints[0].parentTextFrames[0]; break;

                  case "Note": theObj = theObj.storyOffset; break;

                  case "Story": theObj = theObj.textContainers[theObj.textContainers.length-1]; break;

                  case "Application": return null;

                  }

              if (theObj.hasOwnProperty ("parentPage")) return theObj.parentPage;

              if (theObj == null) return null;

              theObj = theObj.parent;

           }

           return theObj;

      }

       

      thanks,

      arasan