private bool insertFoldoutsIntoOriginals(CockleFilePdf parent, CockleFilePdf foldout) { bool insertSuccessful = true; if (parent.PageRange == null || foldout.PageRange == null) { return false; } CAcroApp app = new AcroApp(); // Acrobat CAcroPDDoc doc = new AcroPDDoc(); // First document CAcroPDDoc docToAdd = new AcroPDDoc(); // Next documents try { doc.Open(parent.FullName); docToAdd.Open(foldout.FullName); int numPagesParent = doc.GetNumPages(); int numPagesFoldout = docToAdd.GetNumPages(); // ROTATE IF RECOMMENDED //if (foldout.PageRange.Rotation == PageRangePdf.ROTATE_ENUM.CLOCKWISE) //{ // // reflection to access acrobat javascript // object jso = docToAdd.GetJSObject(); // Type type = jso.GetType(); // object[] getRotatePageParams = { 0, numPagesFoldout - 1, 90 }; // object rotatePage = type.InvokeMember("setPageRotations", // System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, // null, jso, getRotatePageParams); //} //else if (foldout.PageRange.Rotation == PageRangePdf.ROTATE_ENUM.COUNTERCLOCKWISE) //{ // // reflection to access acrobat javascript // object jso = docToAdd.GetJSObject(); // Type type = jso.GetType(); // object[] getRotatePageParams = { 0, numPagesFoldout - 1, 270 }; // object rotatePage = type.InvokeMember("setPageRotations", // System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, // null, jso, getRotatePageParams); //} //else //{ // // do nothing //} if (parent.PageRange.TotalPages == foldout.PageRange.TotalPages) { // check 1: phantom file was never updated if (parent.PageRange.FirstPage == 1 && foldout.PageRange.FirstPage != 1) { // if true, simply swap, page for page doc.ReplacePages(0, docToAdd, 0, parent.PageRange.TotalPages, 0); } // check 2: all page numbers match, just do full replacement else if (parent.PageRange.FirstPage == foldout.PageRange.FirstPage && parent.PageRange.LastPage == foldout.PageRange.LastPage) { doc.ReplacePages(0, docToAdd, 0, parent.PageRange.TotalPages, 0); } // not sure what else to check for here } else { int InsertionPoint = (int)foldout.PageRange.FirstPage - (int)parent.PageRange.FirstPage; if (!doc.DeletePages(InsertionPoint, InsertionPoint + numPagesFoldout - 1)) { insertSuccessful = false; } if (!doc.InsertPages(InsertionPoint - 1, docToAdd, 0, numPagesFoldout, 0)) { insertSuccessful = false; } } docToAdd.Close(); doc.Save(1, parent.FullName); //System.IO.Path.Combine(System.IO.Path.GetDirectoryName(parent.FullName), "testfile.pdf")); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } finally { doc.Close(); app.CloseAllDocs(); app.Exit(); doc = null; docToAdd = null; app = null; GC.Collect(); } return insertSuccessful; }