title: How to programmatically remove PDF layers?
body: <p>I know about <a href="http://help.adobe.com/livedocs/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Acrobat9_HTMLHelp&amp;file=JS_API_AcroJS.88.482.html" rel="nofollow noreferrer"><code>Doc.SetOCGOrder()</code></a> and the likes, but apparently the Acrobat API does not provide means to remove layers (aka optional content groups, ocgs), only to hide them from users while leaving them accessible via scripting.</p>
  
  <p>I'm not forced to use Acrobat, in fact I'd prefer a FOSS-solution (or at least freeware) using e.g. <a href="/questions/tagged/pypdf2" class="post-tag" title="show questions tagged &#39;pypdf2&#39;" rel="tag">pypdf2</a> or <a href="/questions/tagged/pdftk" class="post-tag" title="show questions tagged &#39;pdftk&#39;" rel="tag">pdftk</a>. <strong>edit</strong> There is <a href="https://stackoverflow.com/a/2498883/321973">another post</a> using <a href="/questions/tagged/abcpdf" class="post-tag" title="show questions tagged &#39;abcpdf&#39;" rel="tag">abcpdf</a>, which is unfortunately not free. Is there any way to port that behaviour to Acrobat's JS or <a href="/questions/tagged/pypdf2" class="post-tag" title="show questions tagged &#39;pypdf2&#39;" rel="tag">pypdf2</a>? Essentially the answer seems to loop through every page's content, which seems to include the layers.</p>
  
  <p>(Concerning merging, I notice <code>pdftk</code> does remove layers when merging multiple files, so one can simple <code>pdftk A=input.pdf B=input.pdf cat A1-end output output.pdf</code>.)</p>