private void createCombinedPdf_noFoldouts( IOrderedEnumerable<CockleFilePdf> files_no_foldouts, TypeOfCombinedPdf typeOfCombinedPdf, bool centerPdf = false) { CAcroApp app = new AcroApp(); // Acrobat CAcroPDDoc doc = new AcroPDDoc(); // First document CAcroPDDoc docToAdd = new AcroPDDoc(); // Next documents try { int numPages = 0, numPagesToAdd = 0; foreach (var f in files_no_foldouts) { if (f.Rank == 0) // both 0 { doc.Open(f.FullName); numPages = doc.GetNumPages(); } else if (f.Rank != 0 && numPages == 0) { doc.Open(f.FullName); numPages = doc.GetNumPages(); } else { if (!docToAdd.Open(f.FullName)) { break; } numPagesToAdd = docToAdd.GetNumPages(); if (!doc.InsertPages(numPages - 1, docToAdd, 0, numPagesToAdd, 0)) { break; } if (!docToAdd.Close()) { break; } numPages = doc.GetNumPages(); } } doc.Save(1, CombinedPdfFilename); doc.Close(); // use reflection to center the pdf if (centerPdf) { var opened = doc.Open(CombinedPdfFilename); if (opened) { object js_object = doc.GetJSObject(); Type js_type = js_object.GetType(); object[] js_param = { }; string script_name = string.Empty; int? cover_length = null; if (typeOfCombinedPdf == TypeOfCombinedPdf.Appendix && files_no_foldouts.All(x => x.CoverLength == null)) { script_name = Models.Utilities.AcrobatJS.Javascripts [Models.Utilities.LocalJavascripts.center_letter_no_cover]; } else if (files_no_foldouts.All(x => x.CoverLength == null)) { script_name = Models.Utilities.AcrobatJS.Javascripts [Models.Utilities.LocalJavascripts.center_letter_no_cover]; } else { cover_length = files_no_foldouts.Where(x => x.FileType == SourceFileTypeEnum.Cover).FirstOrDefault().CoverLength; switch (cover_length) { case 48: default: script_name = Models.Utilities.AcrobatJS.Javascripts [Models.Utilities.LocalJavascripts.center_letter_48pica]; break; case 49: script_name = Models.Utilities.AcrobatJS.Javascripts [Models.Utilities.LocalJavascripts.center_letter_49pica]; break; case 50: script_name = Models.Utilities.AcrobatJS.Javascripts [Models.Utilities.LocalJavascripts.center_letter_50pica]; break; case 51: script_name = Models.Utilities.AcrobatJS.Javascripts [Models.Utilities.LocalJavascripts.center_letter_51pica]; break; } } js_type.InvokeMember(script_name, System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, js_object, js_param); // test that the file exists doc.Save(1, CombinedPdfFilename); } else { throw new Exception("Could not center 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(); } }
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; }