示例#1
0
        /* Combine pdf files in myList into the output file */
        int combinePDF(List <string> myList, string outputFile)
        {
            PdfReader       reader          = null;
            Document        sourceDocument  = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage;
            string          outputPdfPath = outputFile;
            string          k             = "abc";
            int             b             = k.Length;

            sourceDocument  = new Document();
            pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

            // Open document
            sourceDocument.Open();
            try
            {
                foreach (string item in myList)
                {
                    reader = new PdfReader(item);
                    int pages = reader.NumberOfPages;

                    //Add pages of current file
                    for (int i = 1; i <= pages; i++)
                    {
                        importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                        pdfCopyProvider.AddPage(importedPage);
                    }

                    reader.Close();
                }

                sourceDocument.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(1);
        }
        // Create 2 variables for your files and pass both as parameter
        private static void MergePDF(string File1, string File2)
        {
            // Add a new string for the output path destintion. You can call it newFile.pdf
            string[] fileArray = new string[3];
            fileArray[0] = File1;
            fileArray[1] = File2;

            // You might need these for the next steps
            PdfReader       reader          = null;
            Document        sourceDocument  = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage;
            string          outputPdfPath = @"D:/newFile.pdf";

            sourceDocument  = new Document();
            pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

            // Open the document
            sourceDocument.Open();


            // Create a for loop for your files
            // Create a variable to count all the pages of your PDF and put it into a new PDF reader
            for (int f = 0; f < fileArray.Length - 1; f++)
            {
                int pages = TotalPageCount(fileArray[f]);

                reader = new PdfReader(fileArray[f]);
                // Create a for loop to add the pages in your new file
                // Afterwards, use the variables as mentionned above
                for (int i = 1; i <= pages; i++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);
                }

                reader.Close();
            }
            // Save (close) the document
            sourceDocument.Close();
        }
        public void Add(IPDFEditor sheet)
        {
            PdfReader x = new PdfReader(sheet.Result);

            if (preserveEdit)
            {
                x.RemoveUsageRights();
            }
            if (copy == null)
            {
                document = new Document(x.GetPageSizeWithRotation(1));
                copy     = new PdfCopy(document, output);
                document.Open();
            }
            for (int i = 1; i <= x.NumberOfPages; i++)
            {
                copy.SetPageSize(x.GetPageSize(i));
                copy.AddPage(copy.GetImportedPage(x, i));
            }
            x.Close();
        }
 private void AddPdfDocument(PdfCopy copy, byte[] document, string documentName)
 {
     try
     {
         PdfReader.unethicalreading = true;
         using (PdfReader reader = new PdfReader(document))
         {
             PdfReader.unethicalreading = true;
             int n = reader.NumberOfPages;
             for (int page = 0; page < n;)
             {
                 copy.AddPage(copy.GetImportedPage(reader, ++page));
             }
         }
     }
     catch (Exception e)
     {
         Controller.HandleError("Unable to add PDF document " + Path.GetFileName(documentName));
         throw e;
     }
 }
示例#5
0
 public static void SplitAndSaveInterval(string pdfFilePath, string outputPath, int startPage, int interval, string pdfFileName)
 {
     using (PdfReader reader = new PdfReader(pdfFilePath))
     {
         var document = new Document();
         var copy     = new PdfCopy(document, new FileStream($"{outputPath}\\{pdfFileName}.pdf", FileMode.Create));
         document.Open();
         for (int pagenumber = startPage; pagenumber < (startPage + interval); pagenumber++)
         {
             if (reader.NumberOfPages >= pagenumber)
             {
                 copy.AddPage(copy.GetImportedPage(reader, pagenumber));
             }
             else
             {
                 break;
             }
         }
         document.Close();
     }
 }
示例#6
0
        // Funcionalidad para Hacer Merge de varios archivos PDF
        private static bool MergeArhivosPDF(List <string> archivos, string EnRuta)
        {
            try
            {
                PdfReader       reader          = null;
                Document        sourceDocument  = null;
                PdfCopy         pdfCopyProvider = null;
                PdfImportedPage importedPage;

                sourceDocument  = new Document();
                pdfCopyProvider = new PdfCopy(sourceDocument, new FileStream(EnRuta, FileMode.Create));

                //output file Open
                sourceDocument.Open();

                //files list wise Loop
                for (int f = 0; f < archivos.Count; f++)
                {
                    int pages = TotalPageCount(archivos[f]);

                    reader = new PdfReader(archivos[f]);
                    //Add pages in new file
                    for (int i = 1; i <= pages; i++)
                    {
                        importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                        pdfCopyProvider.AddPage(importedPage);
                    }

                    reader.Close();
                }
                //save the output file
                sourceDocument.Close();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
示例#7
0
        /// <summary>
        /// 每页一张发票
        /// </summary>
        /// <param name="files"></param>
        /// <param name="outputFile"></param>
        public static void Merge1(IEnumerable <string> files, string outputFile)
        {
            Document document = new Document();

            try
            {
                using (FileStream newFileStream = new FileStream(outputFile, FileMode.Create))
                {
                    PdfCopy writer = new PdfCopy(document, newFileStream);
                    if (writer == null)
                    {
                        return;
                    }

                    document.Open();

                    foreach (string file in files)
                    {
                        PdfReader reader = new PdfReader(file);
                        reader.ConsolidateNamedDestinations();

                        for (int i = 1; i <= reader.NumberOfPages; i++)
                        {
                            PdfImportedPage page = writer.GetImportedPage(reader, i);
                            writer.AddPage(page);
                        }
                        reader.Close();
                    }
                    writer.Close();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                document.Close();
            }
        }
示例#8
0
        /// <summary>
        /// 分割PDF文件
        /// </summary>
        /// <param name="sourcePdfPath"></param>
        /// <param name="outputPdfPath"></param>
        /// <param name="startPage"></param>
        /// <param name="endPage"></param>
        public static Boolean ExtractPages(string sourcePdfPath, string outputPdfPath,
                                           int startPage, int endPage)
        {
            PdfReader       reader          = null;
            Document        sourceDocument  = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage    = null;

            try
            {
                // Intialize a new PdfReader instance with the contents of the source Pdf file:
                reader = new PdfReader(sourcePdfPath);

                // For simplicity, I am assuming all the pages share the same size
                // and rotation as the first page:
                sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));

                // Initialize an instance of the PdfCopyClass with the source
                // document and an output file stream:
                pdfCopyProvider = new PdfCopy(sourceDocument,
                                              new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

                sourceDocument.Open();

                // Walk the specified range and add the page copies to the output file:
                for (int i = startPage; i <= endPage; i++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);
                }
                sourceDocument.Close();
                reader.Close();
            }
            catch (Exception ex)
            {
                NCLogger.GetInstance().WriteExceptionLog(ex);
                return(false);
            }
            return(true);
        }
示例#9
0
        private void PDFSplit(string PathFileName_Inp, string PathFileName_Out, List <int> Page, string flag)   //สร้างไฟล์แบบแยก
        {
            //------------ สร้างไฟล์ --------------///

            int CountPage = 1;

            if (flag == "covid_attached")
            {
                CountPage = 5;
            }
            PdfReader       reader          = null;
            Document        document        = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage    = null;

            reader = new PdfReader(PathFileName_Inp);
            string outputPdfPath = PathFileName_Out;

            // Capture the correct size and orientation for the page:
            document = new Document(reader.GetPageSizeWithRotation(1));

            // Initialize an instance of the PdfCopyClass with the source
            // document and an output file stream:
            pdfCopyProvider = new PdfCopy(document,
                                          new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

            document.Open();

            // Extract the desired page number:

            for (int i = 0; i < CountPage; i++)
            {
                importedPage = pdfCopyProvider.GetImportedPage(reader, Page[0]);
                pdfCopyProvider.AddPage(importedPage);
                Page[0]++;
            }
            document.Close();
            pdfCopyProvider.Close();
            reader.Close();
        }
示例#10
0
        public void Verify_Concatenate_CanBeCreated()
        {
            var pdfFiles = new List <byte[]>
            {
                createSamplePdfFile("Hello World!"),
                createSamplePdfFile("Hello DNT!")
            };

            var pdfFilePath = TestUtils.GetOutputFileName();
            var stream      = new FileStream(pdfFilePath, FileMode.Create);

            // step 1
            var document = new Document();

            // step 2
            var pdfCopy = new PdfCopy(document, stream);

            // step 3
            document.AddAuthor(TestUtils.Author);
            document.Open();

            // step 4
            foreach (var pdf in pdfFiles)
            {
                var reader = new PdfReader(pdf);
                var n      = reader.NumberOfPages;
                for (var page = 0; page < n;)
                {
                    pdfCopy.AddPage(pdfCopy.GetImportedPage(reader, ++page));
                }
                reader.Close();
            }

            document.Close();
            pdfCopy.Close();
            stream.Dispose();

            TestUtils.VerifyPdfFileIsReadable(pdfFilePath);
        }
示例#11
0
        /// <summary>
        /// 拆分PDF
        /// </summary>
        public static void PDFSplit(string inFile, string[] outFileArray)
        {
            if (outFileArray.Count() != new PdfReader(inFile).NumberOfPages)
            {
                throw new System.Exception("所选择的PDF页数与图号、图名数量不符, 请检查后重试");
            }

            using (var reader = new PdfReader(inFile))
            {
                // 注意起始页是从1开始的
                for (int i = 1; i <= new PdfReader(inFile).NumberOfPages; i++)
                {
                    using (var sourceDocument = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(i)))
                    {
                        var pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outFileArray[i - 1], System.IO.FileMode.Create));
                        sourceDocument.Open();
                        var importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                        pdfCopyProvider.AddPage(importedPage);
                    }
                }
            }
        }
示例#12
0
        private static void MergePDF(List <string> fileArray, string outputPdfPath)
        {
            PdfImportedPage importedPage;

            Document sourceDocument  = new Document();
            PdfCopy  pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

            sourceDocument.Open();

            foreach (var fil in fileArray)
            {
                PdfReader reader = new PdfReader(fil);
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);
                }

                reader.Close();
            }
            sourceDocument.Close();
        }
示例#13
0
        public FileStreamResult GetCombinedResult()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (Document document = new Document())
                {
                    using (PdfCopy copy = new PdfCopy(document, ms))
                    {
                        document.Open();

                        for (int i = 0; i < _pdf.Count; ++i)
                        {
                            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(_pdf[i]);
                            // loop over the pages in that document
                            int n = reader.NumberOfPages;
                            for (int page = 0; page < n;)
                            {
                                copy.AddPage(copy.GetImportedPage(reader, ++page));
                            }
                        }
                    }
                }

                System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
                {
                    FileName = _fileName,
                    Inline   = _cd // false = prompt the user for downloading;  true = browser to try to show the file inline
                };

                _context.HttpContext.Response.ContentType = "application/pdf";
                _context.HttpContext.Response.AddHeader("content-disposition", cd.ToString());
                _context.HttpContext.Response.Buffer = true;
                _context.HttpContext.Response.Clear();
                _context.HttpContext.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
                _context.HttpContext.Response.OutputStream.Flush();
                _context.HttpContext.Response.End();
            }
            return(new FileStreamResult(_context.HttpContext.Response.OutputStream, "application/pdf"));
        }
示例#14
0
        /// <summary>
        /// Merge pdf files.
        /// </summary>
        /// <param name="sourceFiles">PDF files being merged.</param>
        /// <returns></returns>
        public static byte[] MergeFiles(List <byte[]> sourceFiles)
        {
            Document document = new Document();

            using (MemoryStream ms = new MemoryStream())
            {
                PdfCopy copy = new PdfCopy(document, ms);
                document.Open();
                int documentPageCounter = 0;
                // Iterate through all pdf documents
                for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
                {
                    // Create pdf reader
                    PdfReader reader        = new PdfReader(sourceFiles[fileCounter]);
                    int       numberOfPages = reader.NumberOfPages;
                    // Iterate through all pages
                    for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                    {
                        documentPageCounter++;
                        PdfImportedPage   importedPage = copy.GetImportedPage(reader, currentPageIndex);
                        PdfCopy.PageStamp pageStamp    = copy.CreatePageStamp(importedPage);
                        // Write header
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                                                   new Phrase("PDF Merger by Helvetic Solutions"), importedPage.Width / 2, importedPage.Height - 30,
                                                   importedPage.Width < importedPage.Height ? 0 : 1);
                        // Write footer
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                                                   new Phrase(String.Format("Page {0}", documentPageCounter)), importedPage.Width / 2, 30,
                                                   importedPage.Width < importedPage.Height ? 0 : 1);
                        pageStamp.AlterContents();
                        copy.AddPage(importedPage);
                    }
                    copy.FreeReader(reader);
                    reader.Close();
                }
                document.Close();
                return(ms.GetBuffer());
            }
        }
示例#15
0
        private void SplitAndSaveInterval(PdfReader reader, string outputPath, int startPage, int interval, string pdfFileName)
        {
            var document = new Document();

            var copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));

            document.Open();

            for (int pageNumber = startPage; pageNumber < (startPage + interval); pageNumber++)
            {
                if (reader.NumberOfPages >= pageNumber)
                {
                    copy.AddPage(copy.GetImportedPage(reader, pageNumber));
                }
                else
                {
                    break;
                }
            }

            document.Close();
        }
示例#16
0
        public static void CombineMultiplePDFs(string[] fileNames, string outFile)
        {
            // step 1: creation of a document-object
            Document document = new Document();

            // step 2: we create a writer that listens to the document
            PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));

            if (writer == null)
            {
                return;
            }
            // step 3: we open the document
            document.Open();
            foreach (string fileName in fileNames)
            {
                // we create a reader for a certain document
                PdfReader reader = new PdfReader(fileName);
                reader.ConsolidateNamedDestinations();

                // step 4: we add content
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    PdfImportedPage page = writer.GetImportedPage(reader, i);
                    writer.AddPage(page);
                }

                PRAcroForm form = reader.AcroForm;
                if (form != null)
                {
                    //writer.CopyAcroForm(reader);
                    writer.CopyDocumentFields(reader);
                }
                reader.Close();
            }
            // step 5: we close the document and writer
            writer.Close();
            document.Close();
        }
        static void ExtracaoPDFMetodo()
        {
            PdfReader pdfReader = new PdfReader(@"C:\projetos-cs\EstudosConceitosTecnicas\UtilizandoOiTextSharp\iTextSharp\dir\Proposta De Projeto.pdf");
            Document  document  = new Document();

            if (pdfReader.NumberOfPages > 0)
            {
                for (int i = 1; i < pdfReader.NumberOfPages; i++)
                {
                    PdfCopy pdfCopy = new PdfCopy(document, new FileStream(Path.Combine(@"C:\projetos-cs\EstudosConceitosTecnicas\UtilizandoOiTextSharp\iTextSharp\dir\extraidos", string.Format("pagina_{0}.pdf", i)), FileMode.Create));

                    document.Open();

                    pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfReader, i));
                }
                document.Close();
            }
            else
            {
                return;
            }
        }
示例#18
0
        // ---------------------------------------------------------------------------
        public void Write(Stream stream)
        {
            // use one of the previous examples to create a PDF
            MovieTemplates mt = new MovieTemplates();

            // Create a reader
            byte[]    pdf    = Utility.PdfBytes(mt);
            PdfReader reader = new PdfReader(pdf);
            // loop over all the pages in the original PDF
            int n = reader.NumberOfPages;

            using (ZipFile zip = new ZipFile())
            {
                for (int i = 0; i < n;)
                {
                    string dest = string.Format(RESULT, ++i);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        // We'll create as many new PDFs as there are pages
                        // step 1
                        using (Document document = new Document())
                        {
                            // step 2
                            using (PdfCopy copy = new PdfCopy(document, ms))
                            {
                                // step 3
                                document.Open();
                                // step 4
                                copy.AddPage(copy.GetImportedPage(reader, i));
                            }
                        }
                        zip.AddEntry(dest, ms.ToArray());
                    }
                }
                zip.AddEntry(Utility.ResultFileName(mt.ToString() + ".pdf"), pdf);
                zip.Save(stream);
            }
        }
示例#19
0
// ---------------------------------------------------------------------------
        public void Write(Stream stream)
        {
            MovieLinks1   ml  = new MovieLinks1();
            MovieHistory  mh  = new MovieHistory();
            List <byte[]> pdf = new List <byte[]>()
            {
                Utility.PdfBytes(ml),
                Utility.PdfBytes(mh)
            };

            string[] names = { ml.ToString(), mh.ToString() };
            using (ZipFile zip = new ZipFile()) {
                using (MemoryStream ms = new MemoryStream()) {
                    // step 1
                    using (Document document = new Document()) {
                        // step 2
                        using (PdfCopy copy = new PdfCopy(document, ms)) {
                            // step 3
                            document.Open();
                            // step 4
                            for (int i = 0; i < pdf.Count; ++i)
                            {
                                zip.AddEntry(Utility.ResultFileName(names[i] + ".pdf"), pdf[i]);
                                PdfReader reader = new PdfReader(pdf[i]);
                                // loop over the pages in that document
                                int n = reader.NumberOfPages;
                                for (int page = 0; page < n;)
                                {
                                    copy.AddPage(copy.GetImportedPage(reader, ++page));
                                }
                            }
                        }
                    }
                    zip.AddEntry(RESULT, ms.ToArray());
                }
                zip.Save(stream);
            }
        }
示例#20
0
文件: PSPDF.cs 项目: nethomtz/DGCCH
        private void Button7_Click(object sender, EventArgs e, string sourcePdfPath, string outputPdfPath,
                                   int startPage, int endPage)
        {
            PdfReader       reader          = null;
            Document        sourceDocument  = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage    = null;

            try
            {
                // Intialize a new PdfReader instance with the contents of the source Pdf file:
                reader = new PdfReader("Guia_CV_2016.pdf");

                // For simplicity, I am assuming all the pages share the same size
                // and rotation as the first page:
                sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));

                // Initialize an instance of the PdfCopyClass with the source
                // document and an output file stream:
                pdfCopyProvider = new PdfCopy(sourceDocument,
                                              new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

                sourceDocument.Open();

                // Walk the specified range and add the page copies to the output file:
                for (int i = startPage; i <= endPage; i++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);
                }
                sourceDocument.Close();
                reader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#21
0
文件: PSPDF.cs 项目: nethomtz/DGCCH
        public void ExtractPages(string sourcePdfPath, string outputPdfPath, int[] extractThesePages) //Extraiga varias páginas no contiguas de PDF existente a un nuevo archivo
        {
            PdfReader       reader          = null;
            Document        sourceDocument  = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage    = null;

            try
            {
                // Intialize a new PdfReader instance with the
                // contents of the source Pdf file:
                reader = new PdfReader(sourcePdfPath);

                // For simplicity, I am assuming all the pages share the same size
                // and rotation as the first page:
                sourceDocument = new Document(reader.GetPageSizeWithRotation(extractThesePages[0]));

                // Initialize an instance of the PdfCopyClass with the source
                // document and an output file stream:
                pdfCopyProvider = new PdfCopy(sourceDocument,
                                              new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

                sourceDocument.Open();

                // Walk the array and add the page copies to the output file:
                foreach (int pageNumber in extractThesePages)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, pageNumber);
                    pdfCopyProvider.AddPage(importedPage);
                }
                sourceDocument.Close();
                reader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#22
0
        private void CreatePDF(PdfReader reader, int pagenumber, string finalOutputPath, string filename)
        {
            Document document = new Document();

            filename += ".pdf";
            try
            {
                using (PdfCopy copy = new PdfCopy(document, new FileStream(finalOutputPath + "\\" + filename, FileMode.Create)))
                {
                    document.Open();

                    copy.AddPage(copy.GetImportedPage(reader, pagenumber));
                }
            }
            catch (Exception ex)
            {
                ErrorLogging.Call_Log(ex, false);
            }
            finally
            {
                document.Close();
            }
        }
示例#23
0
        public void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage)
        {
            PdfReader       reader          = null;
            Document        sourceDocument  = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage    = null;

            try
            {
                reader          = new PdfReader(sourcePdfPath);
                sourceDocument  = new Document(reader.GetPageSizeWithRotation(startPage));
                pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
                sourceDocument.Open();
                for (int i = startPage; i <= endPage; i++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);
                }
                sourceDocument.Close();
                reader.Close();
            }
            catch (Exception ex) { throw ex; }
        }
示例#24
0
 public void splitAndSave(string pdfFilePath, string outputPath, int startPage, int interval, string pdfFileName)
 {
     using (PdfReader reader = new PdfReader(pdfFilePath))
     {
         Document document = new Document();
         using (PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create)))
         {
             document.Open();
             for (int pagenumber = startPage; pagenumber < (startPage + interval); pagenumber++)
             {
                 if (reader.NumberOfPages >= pagenumber)
                 {
                     copy.AddPage(copy.GetImportedPage(reader, pagenumber));
                 }
                 else
                 {
                     break;
                 }
             }
         }
         document.Close();
     }
 }
示例#25
0
        private static void Concat2PDFs(string File1, string File2, string OutputFile)
        {
            string[] fileArray = new string[3];
            fileArray[0] = File1;
            fileArray[1] = File2;

            PdfReader reader = null;

            Document        sourceDocument  = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage;

            sourceDocument  = new Document();
            pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(OutputFile, System.IO.FileMode.Create));

            //output file Open
            sourceDocument.Open();


            //files list wise Loop
            for (int g = 0; g < fileArray.Length - 1; g++)
            {
                int pages = TotalPageCount(fileArray[g]);

                reader = new PdfReader(fileArray[g]);
                //Add pages in new file
                for (int i = 1; i <= pages; i++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);
                }

                reader.Close();
            }
            //save the output file
            sourceDocument.Close();
        }
示例#26
0
        public static string MergePDF(List <string> _ListPDF, string OutFile)
        {
            if (File.Exists(OutFile))
            {
                File.Delete(OutFile);
            }
            foreach (var item in _ListPDF)
            {
                using (FileStream stream = new FileStream(OutFile, FileMode.Create))
                    using (Document doc = new Document())
                        using (PdfCopy pdf = new PdfCopy(doc, stream))
                        {
                            doc.Open();

                            PdfReader       reader = null;
                            PdfImportedPage page   = null;

                            //fixed typo
                            _ListPDF.ForEach(file =>
                            {
                                reader = new PdfReader(file);

                                for (int i = 0; i < reader.NumberOfPages; i++)
                                {
                                    page = pdf.GetImportedPage(reader, i + 1);
                                    pdf.AddPage(page);
                                }

                                pdf.FreeReader(reader);
                                reader.Close();
                                //File.Delete(file);
                            });
                        }
            }

            return(OutFile);
        }
示例#27
0
        private void ExtractPages(string inputfilename, string outputfilename, List <int> pages)
        {
            PdfReader       reader          = null;
            Document        sourceDocument  = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage    = null;

            try
            {
                // Intialize a new PdfReader instance with the
                // contents of the source Pdf file:
                reader = new PdfReader(inputfilename);


                sourceDocument = new Document();

                // Initialize an instance of the PdfCopyClass with the source
                // document and an output file stream:
                pdfCopyProvider = new PdfCopy(sourceDocument,
                                              new System.IO.FileStream(outputfilename, System.IO.FileMode.Create));

                sourceDocument.Open();

                // Walk the array and add the page copies to the output file:
                foreach (int pageNumber in pages)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, pageNumber);
                    pdfCopyProvider.AddPage(importedPage);
                }
                sourceDocument.Close();
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occured: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
示例#28
0
        public static int MergePdfFun(string outputPath, List <string> inputPathList)
        {
            if (inputPathList.Count == 0)
            {
                return(1);
            }
            PdfReader       reader          = null;
            Document        sourceDocument  = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage;

            sourceDocument  = new Document();
            pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPath, System.IO.FileMode.Create));

            //output file Open
            sourceDocument.Open();

            //files list wise Loop
            for (int f = 0; f < inputPathList.Count; f++)
            {
                int pages = TotalPageCount(inputPathList[f]);

                reader = new PdfReader(inputPathList[f]);
                //Add pages in new file
                for (int i = 1; i <= pages; i++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);
                }

                reader.Close();
            }

            //save the output file
            sourceDocument.Close();
            return(0);
        }
示例#29
0
        private static void MergePDF(string File1, string File2)
        {
            string[] fileArray = new string[3];
            fileArray[0] = File1;
            fileArray[1] = File2;

            PdfReader       reader          = null;
            Document        sourceDocument  = null;
            PdfCopy         pdfCopyProvider = null;
            PdfImportedPage importedPage;
            string          outputPdfPath = @"d:/temp/mergeFile.pdf";

            sourceDocument  = new Document();
            pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

            //output file Open
            sourceDocument.Open();


            //files list wise Loop
            for (int f = 0; f < fileArray.Length - 1; f++)
            {
                int pages = TotalPageCount(fileArray[f]);

                reader = new PdfReader(fileArray[f]);
                //Add pages in new file
                for (int i = 1; i <= pages; i++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);
                }

                reader.Close();
            }
            //save the output file
            sourceDocument.Close();
        }
示例#30
0
        private bool GerarArquivo(PdfReader reader, ContribuinteDTO contribuinte, int pagina)
        {
            var cpfCnpj = contribuinte.CpfCnpj.Replace(".", String.Empty).
                          Replace("-", String.Empty).Replace("/", String.Empty);

            contribuinte.Arquivo.NomeArquivo     = cpfCnpj;
            contribuinte.Arquivo.ExtensaoArquivo = ".pdf";

            var pathArquivo = String.Format(@"{0}\{1}_TASK{2}{3}", contribuinte.Arquivo.CaminhoArquivo, cpfCnpj, Task.CurrentId, ".pdf");

            if (File.Exists(pathArquivo))
            {
                int i = 1;
                pathArquivo = String.Format(@"{0}\{1}_TASK{2}_{3}{4}", contribuinte.Arquivo.CaminhoArquivo, cpfCnpj, Task.CurrentId, i, ".pdf");

                while (File.Exists(pathArquivo))
                {
                    i++;
                    pathArquivo = String.Format(@"{0}\{1}_TASK{2}_{3}{4}", contribuinte.Arquivo.CaminhoArquivo, cpfCnpj, Task.CurrentId, i, ".pdf");
                }
            }

            var document = new Document(reader.GetPageSizeWithRotation(pagina));

            var provider     = new PdfCopy(document, new FileStream(pathArquivo, FileMode.Create));
            var importedPage = provider.GetImportedPage(reader, pagina);

            document.Open();

            provider.AddPage(importedPage);

            document.Close();

            provider.Close();

            return(true);
        }