Пример #1
0
        private void ExportAsImage(string strFile, string strExportPath)
        {
            if (!Directory.Exists(strExportPath))
            {
                Directory.CreateDirectory(strExportPath);
            }
            Acrobat.AcroPDDoc pdf = new AcroPDDoc();
            pdf.Open(strFile);

            int pageCount = Convert.ToInt32(txtPage.Text);
            int count     = pdf.GetNumPages();

            for (int i = 0; i < count; i++)
            {
                if (i == pageCount)
                {
                    break;
                }
                listMsg.Items.Add($"正在导出第{i + 1}页(共{count}页)");
                listMsg.TopIndex = listMsg.Items.Count - 1;
                int zoom = 1;
                while (true)
                {
                    try
                    {
                        CAcroPDPage page      = (CAcroPDPage)pdf.AcquirePage(i);
                        CAcroRect   pdfRect   = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
                        CAcroPoint  pdfPoint  = (Acrobat.CAcroPoint)page.GetSize();
                        int         imgWidth  = (int)((double)pdfPoint.x * zoom);
                        int         imgHeight = (int)((double)pdfPoint.y * zoom);

                        //设置裁剪矩形的大小为当前页的大小
                        pdfRect.Left   = 0;
                        pdfRect.right  = (short)imgWidth;
                        pdfRect.Top    = 0;
                        pdfRect.bottom = (short)imgHeight;


                        page.CopyToClipboard(pdfRect, 0, 0, (short)(100 * zoom));
                        Object obj = Clipboard.GetData(DataFormats.Bitmap);
                        using (Bitmap bitmap = (Bitmap)obj)
                        {
                            SavePicture(bitmap, strExportPath, Path.GetFileNameWithoutExtension(strFile), (i + 1).ToString());
                        }
                    }
                    catch (Exception e)
                    {
                        continue;
                    }

                    break;
                }
            }
        }
        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();
            }
        }
Пример #3
0
        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();
            }
        }
        public CAcroPDDoc OpenNewPDDoc(string openPDFPath)
        {
            if (File.Exists(openPDFPath))
            {
                List <CAcroPDDoc> pdDocList = new List <CAcroPDDoc>(PDDocs);

                CAcroPDDoc pDDoc = new AcroPDDoc();
                if (pDDoc.Open(openPDFPath))
                {
                    pdDocList.Add(pDDoc);
                    PDDocs = pdDocList.ToArray();
                }
                else
                {
                    LoadError = AcrobatLoadError.PDFInvalid;
                }
                return(pDDoc);
            }
            else
            {
                LoadError = AcrobatLoadError.FileNotFound;
                return(null);
            }
        }
        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;
        }
Пример #7
0
        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();
            }
        }
Пример #8
0
        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();
            }
        }
Пример #9
0
        public ConvertToPdfa(CockleFilePdf cockleFilePdf, bool center)
        {
            if (null == cockleFilePdf)
            {
                throw new Exception("Original file is null.");
            }
            if (!Models.Utilities.AcrobatJS.AreAcrobatJavascriptsInPlace())
            {
                throw new Exception();
            }

            OriginalCockleFilePdf = cockleFilePdf;


            CAcroApp   app = new AcroApp();
            CAcroPDDoc doc = new AcroPDDoc();

            try
            {
                doc.Open(cockleFilePdf.FullName);

                // first center
                object   js_object   = doc.GetJSObject();
                Type     js_type     = js_object.GetType();
                object[] js_param    = { };
                string   script_name = string.Empty;
                if (cockleFilePdf.CoverLength != null)
                {
                    switch (cockleFilePdf.CoverLength)
                    {
                    case 48:
                    default:
                        script_name = Utilities.AcrobatJS.Javascripts
                                      [Utilities.LocalJavascripts.center_letter_48pica];
                        break;

                    case 49:
                        script_name = Utilities.AcrobatJS.Javascripts
                                      [Utilities.LocalJavascripts.center_letter_49pica];
                        break;

                    case 50:
                        script_name = Utilities.AcrobatJS.Javascripts
                                      [Utilities.LocalJavascripts.center_letter_50pica];
                        break;

                    case 51:
                        script_name = Utilities.AcrobatJS.Javascripts
                                      [Utilities.LocalJavascripts.center_letter_51pica];
                        break;
                    }
                }
                else
                {
                    script_name = Utilities.AcrobatJS.Javascripts
                                  [Utilities.LocalJavascripts.center_letter_no_cover];
                }

                object createCenteredPdf = js_type.InvokeMember(
                    script_name, /* name of js function */
                    BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
                    null, js_object, js_param);

                // then create pdfa
                object createNewPdfADoc = js_type.InvokeMember(
                    /*"convertToPdfA", name of js function */
                    AcrobatJS.Javascripts[LocalJavascripts.convert_pdfa],
                    BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
                    null, js_object, js_param);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                throw new Exception("Error in attempt to convert original to PDF/A file.");
            }
            finally
            {
                doc.Close();
                app.CloseAllDocs();
                app.Exit();

                doc = null;
                app = null;

                GC.Collect();
            }

            try
            {
                var new_file_name = OriginalCockleFilePdf.FullName.Replace(".pdf", "_A1b.pdf");
                while (!System.IO.File.Exists(new_file_name)) /*block*/ } {
                PdfaCockleFilePdf          = cockleFilePdf;
                PdfaCockleFilePdf.FullName = new_file_name.Replace("_A1b.pdf", " pdfa.pdf");

                System.IO.File.Move(new_file_name, PdfaCockleFilePdf.FullName);
                while (!System.IO.File.Exists(PdfaCockleFilePdf.FullName))  /*block*/
                {
                }
        }