Пример #1
0
        private void addFilePages(Stream fileStream)
        {
            PdfReader reader = null;

            try
            {
                _fileNumber++;
                _attachmentsCount += copyAttachments(fileStream);
                fileStream         = fileStream.ReopenForReading();
                reader             = new PdfReader(fileStream);
                reader.ConsolidateNamedDestinations();

                addBookmark(reader);

                int numberOfPages = reader.NumberOfPages;
                for (int pageNumber = 1; pageNumber <= numberOfPages; pageNumber++)
                {
                    var size = reader.GetPageSizeWithRotation(pageNumber);
                    _document.SetPageSize(size);
                    _document.NewPage();
                    _overallPageNumber++;

                    var page = _writer.GetImportedPage(reader, pageNumber);
                    addContentToPage(reader, size, page);
                    _writer.AddPage(page);
                }
            }
            finally
            {
                if (reader != null)
                {
                    _writer.FreeReader(reader);
                }
            }
        }
Пример #2
0
        private void MergeFile()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.FilterIndex = 1;
            saveFileDialog.Filter      = "Pdf file(.pdf)|*.pdf|All Files (*.*)|*.*";
            bool?result = saveFileDialog.ShowDialog();

            if (result == true)
            {
                PdfReader.unethicalreading = true;
                Document document = new Document();
                PdfCopy  pdfCopy  = new PdfCopy(document, new System.IO.FileStream(saveFileDialog.FileName, System.IO.FileMode.Create));
                document.Open();

                foreach (var file in MergeFiles)
                {
                    using (PdfReader reader = new PdfReader(file.Path))
                    {
                        reader.ConsolidateNamedDestinations();

                        for (var i = 1; i <= reader.NumberOfPages; i++)
                        {
                            PdfImportedPage importedPage = pdfCopy.GetImportedPage(reader, i);
                            pdfCopy.AddPage(importedPage);
                        }
                        reader.Close();
                    }
                }
                document.Close();
            }
        }
Пример #3
0
        private void btnCombinar_Click(object sender, EventArgs e)
        {
            Document document = new Document();

            PdfCopy writer = new PdfCopy(document, new FileStream(@"C:\Users\agaspari\Documents\Temp\telecurso\matematica_1.pdf", FileMode.Create));

            if (writer == null)
            {
                return;
            }

            document.Open();

            foreach (string fileName in Directory.GetFiles(@"C:\Users\agaspari\Documents\Temp\telecurso\matematica_1", "*.pdf", SearchOption.TopDirectoryOnly))
            {
                PdfReader reader = new PdfReader(fileName);
                reader.ConsolidateNamedDestinations();

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

                reader.Close();
            }

            writer.Close();
            document.Close();
        }
Пример #4
0
        /// <summary>
        /// Is to use the library ITextSharp.
        /// The method creates a new file <paramref name="OutputFile"/> using
        /// the filestream by copying the files entered here <paramref name="PDFfileNames"/>.
        /// "https://www.c-sharpcorner.com/article/merge-multiple-pdf-files-into-single-pdf-using-itextsharp-in-c-sharp/"
        /// </summary>
        /// <param name="PDFfileNames"></param>
        /// <param name="OutputFile"></param>
        public static void CreatePdf(string[] PDFfileNames, string OutputFile)
        {
            // Create document object
            Document PDFdoc = new Document();

            // Create a object of FileStream which will be disposed at the end
            using FileStream MyFileStream = new FileStream(OutputFile, FileMode.Create);
            // Create a PDFwriter that is listens to the Pdf document
            PdfCopy PDFwriter = new PdfCopy(PDFdoc, MyFileStream);

            if (PDFwriter == null)
            {
                return;
            }
            // Open the PDFdocument
            PDFdoc.Open();
            foreach (string fileName in PDFfileNames)
            {
                // Create a PDFreader for a certain PDFdocument
                PdfReader PDFreader = new PdfReader(fileName);
                PDFreader.ConsolidateNamedDestinations();
                // Add content for page
                for (int i = 1; i <= PDFreader.NumberOfPages; i++)
                {
                    PDFwriter.AddPage(PDFwriter.GetImportedPage(PDFreader, i));
                }

                // Close PDFreader
                PDFreader.Close();
            }
            // Close the PDFdocument and PDFwriter
            PDFwriter.Close();
            PDFdoc.Close();
        }
Пример #5
0
        public static void ConcatenatePdfs(List<string> inputPdfFilepaths, Stream outputStream)
        {
            Document document = null;
            PdfCopy writer = null;
            int fileIndex = 0;
            foreach (string inputFile in inputPdfFilepaths)
            {
                PdfReader reader = new PdfReader(inputFile);                
                reader.ConsolidateNamedDestinations();
                int pageCount = reader.NumberOfPages;

                if (fileIndex == 0)
                {
                    document = new Document(reader.GetPageSizeWithRotation(1));
                    writer = new PdfCopy(document, outputStream);
                    document.Open();
                }

                PdfImportedPage page;
                for (int p = 0; p < pageCount; p++)
                {
                    ++p;
                    page = writer.GetImportedPage(reader, p);
                    writer.AddPage(page);
                }
                PRAcroForm form = reader.AcroForm;
                if (form != null)
                {
                    writer.CopyAcroForm(reader);
                }
                fileIndex++;
            }
            document.Close();            
        }
Пример #6
0
        public void Append(PdfReader reader, string title, PageRange[] pageRanges, PdfConcatenatorOption option)
        {
            reader.ConsolidateNamedDestinations();
            pageRanges = NormalizePageRanges(reader, pageRanges);

            var thePageRangess = new List <PageRange[]>();

            for (int i = 0; i < pageRanges.Length; i++)
            {
                var thePageRanges = new List <PageRange>();
                var prev          = new PageRange(0, 0);
                int j;
                for (j = i; j < pageRanges.Length; j++)
                {
                    if (prev.EndPage < pageRanges[j].StartPage)
                    {
                        thePageRanges.Add(prev = pageRanges[j]);
                    }
                    else
                    {
                        j--;
                        break;
                    }
                }
                i = j;
                thePageRangess.Add(thePageRanges.ToArray());
            }
            foreach (var prs in thePageRangess)
            {
                AppendMain(reader, title, prs, option);
            }
        }
Пример #7
0
        private void merge_files(string[] files, string output_file)
        {
            Document document = new Document();
            PdfCopy  writer   = new PdfCopy(document, new FileStream(output_file, FileMode.Create));

            if (writer == null)
            {
                return;
            }

            document.Open();

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

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

                reader.Close();
            }

            writer.Close();
            document.Close();
        }
Пример #8
0
        public static byte[] Merge(string[] documentPaths)
        {
            byte[] mergedDocument;

            using (MemoryStream memoryStream = new MemoryStream())
                using (Document document = new Document())
                {
                    PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document, memoryStream);
                    document.Open();

                    foreach (string docPath in documentPaths)
                    {
                        PdfReader reader = new PdfReader(docPath);
                        try
                        {
                            reader.ConsolidateNamedDestinations();
                            int numberOfPages = reader.NumberOfPages;
                            for (int page = 0; page < numberOfPages;)
                            {
                                PdfImportedPage pdfImportedPage = pdfSmartCopy.GetImportedPage(reader, ++page);
                                pdfSmartCopy.AddPage(pdfImportedPage);
                            }
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }

                    document.Close();
                    mergedDocument = memoryStream.ToArray();
                }

            return(mergedDocument);
        }
Пример #9
0
        public static bool CombineMultiplePDFs(string[] fileNames, string TargetFile)
        {
            try
            {
                string outFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SamplePDFFolder");
                if (!Directory.Exists(outFile))
                {
                    Directory.CreateDirectory(outFile);
                }
                outFile = Path.Combine(outFile, "MergedPdf.pdf");
                // 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(false);
                }

                // 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);
                    }

                    reader.Close();
                }

                // step 5: we close the document and writer
                writer.Close();
                document.Close();

                File.Copy(outFile, TargetFile, true);
                File.Delete(outFile);
                return(true);
            }
            catch (Exception ex)
            {
                Log.This(ex);
                return(false);
            }
        }
Пример #10
0
        protected void btnMerge_Click(object sender, EventArgs e)
        {
            Stream         myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Title            = "Select PDFFile";
            saveFileDialog1.Filter           = "PDF(*.pdf)|*.pdf";
            saveFileDialog1.FilterIndex      = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    // Code to write the stream goes here.
                    myStream.Close();
                }
            }
            string pdfFile1 = Path.GetFullPath(textBoxPdfFile1Path.Text);

            string[] filenames      = { pdfFile1 };
            string   outputFileName = System.IO.Path.GetRandomFileName() + ".pdf";;
            string   outputPath     = saveFileDialog1.FileName;

            FileInfo fileInfo = new FileInfo(outputPath);

            Console.WriteLine(fileInfo.DirectoryName);


            Document doc    = new Document();
            PdfCopy  writer = new PdfCopy(doc, new FileStream(outputPath, FileMode.Create));

            if (writer == null)
            {
                return;
            }
            doc.Open();
            foreach (var filename in listBox1.Items)
            {
                PdfReader reader = new PdfReader(filename.ToString());
                reader.ConsolidateNamedDestinations();
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    PdfImportedPage page = writer.GetImportedPage(reader, i);
                    writer.AddPage(page);
                }

                reader.Close();
            }

            writer.Close();
            doc.Close();
            System.Diagnostics.Process.Start(outputPath);
        }
Пример #11
0
        private static void MergePdfFiles(MemoryStream outputPdf, Stream[] sourcePdfs)
        {
            PdfReader       reader      = null;
            Document        document    = new Document();
            PdfImportedPage page        = null;
            PdfCopy         pdfCpy      = null;
            int             n           = 0;
            int             totalPages  = 0;
            int             page_offset = 0;
            List <Dictionary <string, object> >  bookmarks = new List <Dictionary <string, object> >();
            IList <Dictionary <string, object> > tempBookmarks;

            for (int i = 0; i <= sourcePdfs.GetUpperBound(0); i++)
            {
                reader = new PdfReader(sourcePdfs[i]);
                reader.ConsolidateNamedDestinations();
                n             = reader.NumberOfPages;
                tempBookmarks = SimpleBookmark.GetBookmark(reader);

                if (i == 0)
                {
                    document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
                    pdfCpy   = new PdfCopy(document, outputPdf);
                    document.Open();
                    SimpleBookmark.ShiftPageNumbers(tempBookmarks, page_offset, null);
                    page_offset += n;
                    if (tempBookmarks != null)
                    {
                        bookmarks.AddRange(tempBookmarks);
                    }
                    totalPages = n;
                }
                else
                {
                    SimpleBookmark.ShiftPageNumbers(tempBookmarks, page_offset, null);
                    if (tempBookmarks != null)
                    {
                        bookmarks.AddRange(tempBookmarks);
                    }

                    page_offset += n;
                    totalPages  += n;
                }

                for (int j = 1; j <= n; j++)
                {
                    page = pdfCpy.GetImportedPage(reader, j);
                    pdfCpy.AddPage(page);
                }
                reader.Close();
            }
            pdfCpy.Outlines = bookmarks;
            document.Close();
        }
Пример #12
0
        virtual public void testGetLink2()
        {
            string    testFile = TestResourceUtils.GetResourceAsTempFile(TEST_RESOURCES_PATH, "getLinkTest2.pdf");
            string    filename = testFile;
            PdfReader rdr      = new PdfReader(new RandomAccessFileOrArray(filename), new byte[0]);

            // this one works: PdfReader rdr = new PdfReader(filename);
            rdr.ConsolidateNamedDestinations(); // does not help
            rdr.GetLinks(1);

            rdr.Close();
        }
Пример #13
0
        public static void CombineMultiplePdFs(string targetDirectory, string outFileLocation)
        {
            var filesPresent = CheckIfDirectoryIsEmpty(targetDirectory);

            //check if any files in the PDF merge source folder
            if (filesPresent)
            {
                // step 1: creation of a document-object
                Document document = new Document();

                //Get files in specific directory
                string[] fileEntries = Directory.GetFiles(targetDirectory);

                var mergedFileName = "\\Merged -" + Path.GetFileName(fileEntries[0]);
                //Create name for the outfile
                var outFile = outFileLocation + mergedFileName;
                //create newFileStream object which will be disposed at the end
                using (FileStream newFileStream = new FileStream(outFile, FileMode.Create))
                {
                    // step 2: we create a writer that listens to the document
                    PdfCopy writer = new PdfCopy(document, newFileStream);
                    // step 3: we open the document
                    document.Open();

                    foreach (string fileName in fileEntries)
                    {
                        ProcessFile(fileName);
                        // 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);
                        }

                        reader.Close();
                    }

                    // step 5: we close the document and writer
                    writer.Close();
                    document.Close();
                }
            }
            else
            {
                Printer.Print($"No files found in {targetDirectory}");
            }
        }
Пример #14
0
        void ProcessPdf(string path)
        {
            using (var reader = new PdfReader(path))
            {
                // need this call to parse page numbers
                reader.ConsolidateNamedDestinations();

                var bookmarks = ParseBookMarks(SimpleBookmark.GetBookmark(reader));
                for (int i = 0; i < bookmarks.Count; ++i)
                {
                    int page     = bookmarks[i].PageNumberInteger;
                    int nextPage = i + 1 < bookmarks.Count
                                   // if not top of page will be missing content
                        ? bookmarks[i + 1].PageNumberInteger - 1

                                   /* alternative is to potentially add redundant content:
                                    * ? bookmarks[i + 1].PageNumberInteger
                                    */

                        : reader.NumberOfPages;
                    string range = string.Format("{0}-{1}", page, nextPage);

                    // DEMO!
                    if (i < 1000)
                    {
                        var outputPath = Path.Combine(OUTPUT_DIR, bookmarks[i].GetFileName());
                        using (var readerCopy = new PdfReader(reader))
                        {
                            var number = bookmarks[i].Number;
                            readerCopy.SelectPages(range);
                            using (FileStream stream = new FileStream(outputPath, FileMode.Create))
                            {
                                using (var document = new Document())
                                {
                                    using (var copy = new PdfCopy(document, stream))
                                    {
                                        document.Open();
                                        int n = readerCopy.NumberOfPages;
                                        for (int j = 0; j < n;)
                                        {
                                            copy.AddPage(copy.GetImportedPage(readerCopy, ++j));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #15
0
        public static byte[] CombineMultiplePDFs(List <byte[]> pdfS)
        {
            // step 1: creation of a document-object
            Document document = new Document();

            //create newFileStream object which will be disposed at the end
            using var ms = new MemoryStream();
            // step 2: we create a writer that listens to the document
            PdfCopy writer = new PdfCopy(document, ms);

            if (writer == null)
            {
                return(null);
            }

            // step 3: we open the document
            document.Open();

            foreach (var pdf in pdfS)
            {
                if (pdf != null && pdf.Length > 0)
                {
                    // we create a reader for a certain document
                    PdfReader reader = new PdfReader(pdf);
                    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);
                    }

                    reader.Close();
                }
            }

            // step 5: we close the document and writer
            writer.Close();
            document.Close();
            return(ms.ToArray());
            //disposes the newFileStream object
        }
Пример #16
0
        private void Form1_Load(object sender, EventArgs e)
        {
            String SecondPage = AppDomain.CurrentDomain.BaseDirectory + "TranscriptRequest.pdf";
            String FirstPage  = AppDomain.CurrentDomain.BaseDirectory + "FirstPage.pdf";

            String Merged = AppDomain.CurrentDomain.BaseDirectory + "TranscriptMerged.pdf";


            Document document = new Document();

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


                document.Open();
                PdfReader reader = new PdfReader(FirstPage);

                reader.ConsolidateNamedDestinations();


                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    PdfImportedPage page = writer.GetImportedPage(reader, i);
                    writer.AddPage(page);
                }
                reader.Close();


                PdfReader readers = new PdfReader(SecondPage);
                PdfReader.unethicalreading = true;
                readers.ConsolidateNamedDestinations();

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

                writer.Close();
                document.Close();
            }
        }
Пример #17
0
        /// <summary>
        /// Unisce gli stream pdf contenuti nella lista passata in un unico stream pdf
        /// </summary>
        /// <param name="files">lista stream pdf</param>
        /// <returns></returns>
        public static byte[] MergePDFs(System.Collections.Generic.List <byte[]> files)
        {
            MemoryStream ms = null;

            byte[]    result = null;
            ArrayList master = null;

            ms     = new MemoryStream();
            master = new ArrayList();
            int      f        = 0;
            Document document = null;
            PdfCopy  writer   = null;

            while (f < files.Count)
            {
                PdfReader reader = new PdfReader(files[f]);
                reader.ConsolidateNamedDestinations();
                int n = reader.NumberOfPages;
                if (f == 0)
                {
                    document = new Document(reader.GetPageSizeWithRotation(1));
                    writer   = new PdfCopy(document, ms);
                    document.Open();
                }
                for (int i = 0; i < n;)
                {
                    ++i;
                    if (writer != null)
                    {
                        PdfImportedPage page = writer.GetImportedPage(reader, i);
                        writer.AddPage(page);
                    }
                }
                PRAcroForm form = reader.AcroForm;
                if (form != null && writer != null)
                {
                    writer.CopyAcroForm(reader);
                }
                f++;
            }
            if (document != null)
            {
                document.Close();
            }
            result = ms.ToArray();
            ms.Close();
            return(result);
        }
Пример #18
0
        public void MergeFiles()
        {
            if (FileList == null || FileList.Count == 0)
            {
                MessageBox.Show("No PDF files found on folder.");
            }
            else
            {
                OutputFileName = System.IO.Path.Combine(Path, $"{OutputFileName}.pdf");

                Document document = new Document();

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

                    document.Open();

                    foreach (var file in FileList)
                    {
                        PdfReader reader = new PdfReader(file.FullPath);
                        reader.ConsolidateNamedDestinations();

                        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);
                        }

                        reader.Close();
                    }

                    writer.Close();
                    document.Close();
                }
            }
        }
Пример #19
0
        public static byte[] GetMergePdf(List <byte[]> pdfData)
        {
            var document = new Document();

            byte[] pdfBytes;

            using (var memoryStream = new MemoryStream())
            {
                var writer = new PdfCopy(document, memoryStream);

                if (writer == null)
                {
                    return(null);
                }

                document.Open();

                foreach (var file in pdfData)
                {
                    var reader = new PdfReader(file);
                    reader.ConsolidateNamedDestinations();

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

                    var form = reader.AcroForm;

                    if (form != null)
                    {
                        writer.AddDocument(reader);
                    }

                    reader.Close();
                }

                writer.Close();
                document.Close();

                pdfBytes = memoryStream.ToArray();
            }

            return(pdfBytes);
        }
Пример #20
0
        private void CombineMultiplePDFs()
        {
            // 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(destinationFile, FileMode.Create));

            if (writer == null)
            {
                return;
            }

            // step 3: we open the document
            document.Open();

            foreach (string fileName in FilesName)
            {
                // 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);
                //}

                reader.Close();
            }

            // step 5: we close the document and writer
            writer.Close();
            document.Close();

            // Eliminar PDFs temp
            //foreach (string filePath in FilesName)
            //    File.Delete(filePath);
        }
Пример #21
0
        private void CombineMultiplePDFs(List <string> fileNames, string outFile)
        {
            // step 1: creation of a document-object
            Document document = new Document();

            //create newFileStream object which will be disposed at the end
            using (FileStream newFileStream = new FileStream(outFile, FileMode.Create))
            {
                // step 2: we create a writer that listens to the document
                PdfCopy writer = new PdfCopy(document, newFileStream);
                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);
                    }

                    reader.Close();
                }

                // step 5: we close the document and writer
                writer.Close();
                document.Close();
            }//disposes the newFileStream object
        }
Пример #22
0
        public string MergePDF(List <string> pi_objTarget)
        {
            string sReturn = string.Format("C://test-{0}.pdf", System.DateTime.Now.ToString("yyMMdd-hhmmss"));

            // 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(sReturn, FileMode.Create));

            try
            {
                if (writer != null)
                {
                    // step 3: we open the document
                    document.Open();

                    foreach (string fileName in pi_objTarget)
                    {
                        // 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);
                        }
                        reader.Close();
                    }
                    // step 5: we close the document and writer
                    writer.Close();
                    document.Close();
                }
            }
            finally
            {
                writer   = null;
                document = null;
            }

            return(sReturn);
        }
Пример #23
0
        void DumpResults(string path)
        {
            using (var reader = new PdfReader(path))
            {
                // need this call to parse page numbers
                reader.ConsolidateNamedDestinations();

                var bookmarks = ParseBookMarks(SimpleBookmark.GetBookmark(reader));
                var sb        = new StringBuilder();
                foreach (var bookmark in bookmarks)
                {
                    sb.AppendLine(string.Format(
                                      "{0, -4}{1, -100}{2, -25}{3}",
                                      bookmark.Number, bookmark.Title,
                                      bookmark.PageNumberString, bookmark.PageNumberInteger
                                      ));
                }
                File.WriteAllText(outputTextFile, sb.ToString());
            }
        }
Пример #24
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();
            }
        }
Пример #25
0
        // append one pdf to another at runtime.
        // rename the temporary pdf file wont work.
        private PdfConcatenate ReadExistingPdfAndCreateWriter(string outPutPdfFile, FileStream fsOriginalPdfFile)
        {
            string OriginalPdfFile = outPutPdfFile;

            // creating a temporary file path.
            string TempPdfFile = CreateTempPathFile(OriginalPdfFile);

            PdfReader OriginalFileReader = new PdfReader(OriginalPdfFile);

            OriginalFileReader.ConsolidateNamedDestinations();

            var Pages = new List <int>();

            // create a temporary .pdf file and open it.
            FileStream     fsTempPdfFile = new FileStream(TempPdfFile, FileMode.Create, FileAccess.ReadWrite);
            PdfConcatenate Writer        = new PdfConcatenate(fsTempPdfFile);

            Writer.Open();

            // reading all page number available in original pdf file
            for (int i = 1; i <= OriginalFileReader.NumberOfPages; i++)
            {
                Pages.Add(i);
            }

            // select all the pages available in original pdf file
            OriginalFileReader.SelectPages(Pages);

            // write all pages into temporary pdf file
            Writer.AddPages(OriginalFileReader);

            OriginalFileReader.Close();
            OriginalFileReader.Dispose();

            // delete the original file.
            File.Delete(OriginalPdfFile);

            return(Writer);
        }
Пример #26
0
        private Byte[] CombineMultiplePDFs(List <Byte[]> fileNames)
        {
            Byte[]       _Byte    = null;
            Document     document = new Document();
            MemoryStream _PageAll = new MemoryStream();
            PdfCopy      writer   = new iTextSharp.text.pdf.PdfCopy(document, _PageAll);

            if (writer == null)
            {
                return(_Byte);
            }
            // step 3: we open the document
            document.Open();
            foreach (Byte[] fileName in fileNames.ToArray())
            {
                // 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);
                }
                reader.Close();
            }
            // step 5: we close the document and writer
            writer.Close();
            document.Close();


            return(_PageAll.ToArray());
        }
Пример #27
0
        public static void CombineMultiplePDFs(string[] fileNames, string outFile)
        {

            string[] extensions = { ".pdf", ".PDF", };

            Document document = new Document();

            PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
            if (writer == null)
            {
                return;
            }

            document.Open();

            foreach (string fileName in fileNames)
            {

                PdfReader reader = new PdfReader(fileName);

                reader.ConsolidateNamedDestinations();

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

                writer.FreeReader(reader);
                reader.Close();
 
            }            

            writer.Close();
            document.Close();
            
        }
Пример #28
0
        public static void CombinePdfs(string[] filenames, string outFile)
        {
            Document document = new Document();

            PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));

            if (writer == null)
            {
                return;
            }

            document.Open();

            foreach (string filename in filenames)
            {
                PdfReader reader = new PdfReader(filename);
                reader.ConsolidateNamedDestinations();

                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);
                }

                reader.Close();
            }

            writer.Close();
            document.Close();
        }
Пример #29
0
        private void ConcatenateLabelPdfs(List <string> filenames)
        {
            //int pageOffset = 0;
            //ArrayList master = new ArrayList();
            //int f = 0;

            //String outFile = args[args.length - 1];

            List <string> inputFilepaths = filenames.ConvertAll(x => storeUrls.ShippingLabelFolderFileRoot + x);

            Document document = null;
            PdfCopy  writer   = null;

            //int pageOffset = 0;
            int fileIndex = 0;

            foreach (string inputFile in inputFilepaths)
            {
                PdfReader reader = new PdfReader(inputFile);
                reader.ConsolidateNamedDestinations();
                int pageCount = reader.NumberOfPages;
                //pageOffset += pageCount;

                if (fileIndex == 0)
                {
                    document = new Document(reader.GetPageSizeWithRotation(1));
                    writer   = new PdfCopy(document, new FileStream(@"C:\WEB\DNNspot_DEV\DesktopModules\DNNspot-Store\ShippingLabels\bulk.pdf", FileMode.Create));
                    document.Open();
                }

                PdfImportedPage page;
                for (int p = 0; p < pageCount; p++)
                {
                    ++p;
                    page = writer.GetImportedPage(reader, p);
                    writer.AddPage(page);
                }
                PRAcroForm form = reader.AcroForm;
                if (form != null)
                {
                    writer.CopyAcroForm(reader);
                }
                fileIndex++;
            }
            document.Close();


            //while (f < args.length - 1) {
            //  PdfReader reader = new PdfReader(args[f]);
            //  reader.consolidateNamedDestinations();
            //  int n = reader.getNumberOfPages();
            //  List bookmarks = SimpleBookmark.getBookmark(reader);
            //  if (bookmarks != null) {
            //    if (pageOffset != 0) {
            //      SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset,
            //         null);
            //    }
            //    master.addAll(bookmarks);
            //   }
            //   pageOffset += n;

            //   if (f == 0) {
            //     document = new Document(reader.getPageSizeWithRotation(1));
            //     writer = new PdfCopy(document,
            //         new FileOutputStream(outFile));
            //     document.open();
            //   }
            //   PdfImportedPage page;
            //   for (int i = 0; i < n;) {
            //     ++i;
            //     page = writer.getImportedPage(reader, i);
            //     writer.addPage(page);
            //   }
            //   PRAcroForm form = reader.getAcroForm();
            //   if (form != null) {
            //     writer.copyAcroForm(reader);
            //   }
            //   f++;
            //}
            //if (!master.isEmpty()) {
            //  writer.setOutlines(master);
            //}
            //document.close();
        }
Пример #30
0
        public async Task <IActionResult> Thanks(int Id)
        {
            ViewData["AllCategories"] = _categoryRepository.GetAll().ToList();

            Order order = _orderRepository.GetBy(Id);

            if (_signInManager.IsSignedIn(User))
            {
                var applicationUser = await _userManager.GetUserAsync(User);

                var application = _userRepository.GetBy(applicationUser.Email);
                var orderer     = _userRepository.GetByOrderId(Id);

                if (application == orderer)
                {
                    ICollection <OrderItem> OrderItem     = RetrieveOrderItem(order);
                    IList <OrderItem>       orderItemList = OrderItem.ToList();
                    ViewData["OrderItem"] = orderItemList;

                    // 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(@"wwwroot/pdf/merged_" + orderItemList[0].QRCode + ".pdf", FileMode.Create));
                    if (writer != null)
                    {
                        // step 3: we open the document
                        document.Open();

                        foreach (var bestellijn in OrderItem)
                        {
                            // we create a reader for a certain document
                            PdfReader reader = new PdfReader(@"wwwroot/pdf/c_" + bestellijn.QRCode + ".pdf");
                            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);
                            //}

                            reader.Close();
                        }

                        // step 5: we close the document and writer
                        writer.Close();
                        document.Close();
                        return(View());
                    }
                }
            }
            //anoniem of hackattempt -> geen download knop
            ViewData["OrderItem"] = null;
            return(View());
        }