title: Trying to flatten PDF using flattenPages() but nothing happens
body: <p>I'm trying to call the Acrobat Javascript API from VBA to flatten all annotations in a document, using the following code:</p>
  
  <pre><code>Sub flattenPDF()
  
  Dim AcroApp As Acrobat.AcroApp
  Dim AcroDoc As Acrobat.AcroPDDoc
  Dim jso As Object
  Dim path As String
  
  path = "C:\Users\userID\Desktop\thisfile.pdf"
  
  Set AcroApp = CreateObject("AcroExch.App")
  Set AcroDoc = CreateObject("AcroExch.PDDoc")
  AcroDoc.Open path
  Set jso = AcroDoc.GetJSObject
  
  jso.flattenPages
  AcroDoc.Save PDSaveFull, path
  AcroDoc.Close
  AcroApp.Exit 
  
  End Sub
  </code></pre>
  
  <p>The code runs successfully, but then when I open the PDF, all the annotations can still be edited--the flattening should have made them read-only, right?</p>
  
  <p>Edit: I changed the first parameter of <code>AcroDoc.Save</code> from "1" to "PDSaveFull", and now the annotations are flattened if I run the script twice.  Why don't they flatten the first time?</p>
  
  <p>Update:</p>
  
  <p>I modified the script to get the page count and pass it to <code>flattenPages()</code> per joelgaraci's suggestion, as well as passing in the PDF path to the function:</p>
  
  <pre><code>Sub flattenPDF(pdfPath As String)
  
  Dim AcroApp As Acrobat.AcroApp
  Dim AcroDoc As Acrobat.AcroPDDoc
  Dim pageCount As Integer
  Dim jso As Object
  
  Set AcroApp = CreateObject("AcroExch.App")
  Set AcroDoc = CreateObject("AcroExch.PDDoc")
  AcroDoc.Open pdfPath
  pageCount = AcroDoc.GetNumPages
  Set jso = AcroDoc.GetJSObject
  jso.flattenPages 0, pageCount - 1
  AcroDoc.Save PDSaveFull, pdfPath
  AcroDoc.Close
  AcroApp.Exit
  
  End Sub
  </code></pre>
  
  <p>But this got the same result: the annotations only flatten after I run the script twice.</p>