Example #1
0
        public static void Merge(List <string> filesContents, string filePath)
        {
            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                using (var document = new Document(PageSize.A4))
                {
                    using (var copyWriter = new PdfSmartCopy(document, fileStream))
                    {
                        document.Open();
                        copyWriter.RegisterFonts();

                        foreach (var fileContent in filesContents)
                        {
                            using (var pdfReader = new PdfReader(fileContent))
                            {
                                copyWriter.AddDocument(pdfReader);
                            }
                        }

                        document.Close();
                        copyWriter.RegisterProperties();
                    }
                }
            }

            AddPdfAFlag(filePath);
        }
Example #2
0
        public static byte[] Merge(List <byte[]> filesContents)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var document = new Document(PageSize.A4))
                {
                    using (var copyWriter = new PdfSmartCopy(document, memoryStream))
                    {
                        document.Open();
                        copyWriter.RegisterFonts();

                        foreach (var fileContent in filesContents)
                        {
                            using (var pdfReader = new PdfReader(fileContent))
                            {
                                copyWriter.AddDocument(pdfReader);
                            }
                        }

                        document.Close();
                        copyWriter.RegisterProperties();
                    }
                }

                return(AddPdfAFlag(memoryStream.ToArray()));
            }
        }
Example #3
0
        public static byte[] DeletePages(byte[] fileContents, int[] pages)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var document = new Document(PageSize.A4))
                {
                    using (var pdfReader = new PdfReader(fileContents))
                    {
                        pages = pages
                                .Where(o => o >= 1 && o <= pdfReader.NumberOfPages)
                                .Distinct()
                                .ToArray();

                        if (pages.Length < pdfReader.NumberOfPages)
                        {
                            using (var copyWriter = new PdfSmartCopy(document, memoryStream))
                            {
                                document.Open();
                                copyWriter.RegisterFonts();

                                var contents = copyWriter.DirectContent;

                                for (int i = 1; i <= pdfReader.NumberOfPages; i++)
                                {
                                    if (!pages.Contains(i))
                                    {
                                        var importedPage = copyWriter.GetImportedPage(pdfReader, i);

                                        copyWriter.AddPage(importedPage);
                                    }
                                }

                                document.Close();
                                copyWriter.RegisterProperties();

                                fileContents = memoryStream.ToArray();
                            }
                        }
                    }
                }
            }

            return(fileContents);
        }