private void centerBookletSizePdf(string new_file_name) { CAcroApp app = new AcroApp(); // Acrobat CAcroPDDoc doc = new AcroPDDoc(); // First document CAcroPDDoc docToAdd = new AcroPDDoc(); // Next documents // use reflection to center the pdf try { var opened = doc.Open(OriginalFile.FullName); if (opened) { object js_object = doc.GetJSObject(); Type js_type = js_object.GetType(); object[] js_param = { }; string script_name = Models.Utilities.AcrobatJS.Javascripts [Models.Utilities.LocalJavascripts.arePagesLargerThanBooklet]; var test = js_type.InvokeMember(script_name, System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, js_object, js_param); if ((bool)test) { script_name = Models.Utilities.AcrobatJS.Javascripts [Models.Utilities.LocalJavascripts.centerCenteredDocOnBookletPages]; 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 if (!doc.Save(1, new_file_name)) { throw new Exception(); } } } 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 void nUpCoverDoc(string new_file_name) { CAcroApp app = new AcroApp(); // Acrobat CAcroPDDoc doc = new AcroPDDoc(); // First document CAcroPDDoc docToAdd = new AcroPDDoc(); // Next documents // use reflection to center the pdf try { var opened = doc.Open(selectedPdfFile.FullName); if (opened) { object js_object = doc.GetJSObject(); Type js_type = js_object.GetType(); object[] js_param = { }; string script_name = Models.Utilities.AcrobatJS.Javascripts [Models.Utilities.LocalJavascripts.nUpCircuitCoverOn8pt5by23]; 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 if (!doc.Save(1, new_file_name)) { throw new Exception(); } } } 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 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; }
private void centerOnBriefPaper() { CAcroApp app = new AcroApp(); CAcroPDDoc doc = new AcroPDDoc(); try { var opened = doc.Open(selectedPdfFile.FullName); if (!opened) { throw new Exception("Unable to open file."); } object js_object = doc.GetJSObject(); Type js_type = js_object.GetType(); object[] js_param = { }; string script_name = string.Empty; if (hasCover) { switch (coverLen) { case 48: default: script_name = Utilities.AcrobatJS.Javascripts [Utilities.LocalJavascripts.center_booklet_48pica]; break; case 49: script_name = Utilities.AcrobatJS.Javascripts [Utilities.LocalJavascripts.center_booklet_49pica]; break; case 50: script_name = Utilities.AcrobatJS.Javascripts [Utilities.LocalJavascripts.center_booklet_50pica]; break; case 51: script_name = Utilities.AcrobatJS.Javascripts [Utilities.LocalJavascripts.center_booklet_51pica]; break; } } else { script_name = Utilities.AcrobatJS.Javascripts [Utilities.LocalJavascripts.center_booklet_no_cover]; } js_type.InvokeMember(script_name, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, js_object, js_param); // save document var test = doc.Save(1, NewFileCreated.FullName); // doc.Save(1, NewFileCreated.FullName); would overwrite original } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); NewFileCreated = null; throw new Exception("Error in attempt to center Pdf file."); } finally { doc.Close(); app.CloseAllDocs(); app.Exit(); doc = null; app = null; GC.Collect(); } }
private void centerOnLetterPaper() { CAcroApp app = new AcroApp(); // Acrobat CAcroPDDoc doc = new AcroPDDoc(); // First document try { // use reflection to center the pdf var opened = doc.Open(selectedPdfFile.FullName); 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 (hasCover) { cover_length = coverLen; } switch (cover_length) { case 48: 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; default: script_name = Models.Utilities.AcrobatJS.Javascripts [Models.Utilities.LocalJavascripts.center_letter_no_cover]; 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, newFileName); } 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; app = null; GC.Collect(); } }