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);
            }
        }
        public void Append(string url, PageRange[] pageRanges, PdfConcatenatorOption option)
        {
            PdfReader reader = CreatePdfReader(url, option);

            try
            {
                Append(reader, Path.GetFileNameWithoutExtension(url), pageRanges, option);
            }
            finally
            {
                reader.Close();
            }
        }
        public PdfReader CreatePdfReader(byte[] bytes, PdfConcatenatorOption option)
        {
            PdfReader reader;

            switch (LibPDFTools.GetImageFileType(bytes))
            {
            case ImageFileType.PDF:
                reader = LibPDFTools.CreatePdfReader(bytes, QueryPassword, true);
                break;

            case ImageFileType.TIFF:
                reader = LibPDFTools.CreatePdfReaderFromTiff(bytes, option.GetBounds(), option.Margins);
                break;

            default:
                reader = LibPDFTools.CreatePdfReaderFromImage(bytes, option.GetBounds(), option.Margins);
                break;
            }
            return(reader);
        }
        public PdfReader CreatePdfReader(string url, PdfConcatenatorOption option)
        {
            var bytes = LibPDFTools.GetBytes(LibPDFTools.ToUri(url));

            return(CreatePdfReader(bytes, option));
        }
        private void AppendMain(PdfReader reader, string title, PageRange[] pageRanges, PdfConcatenatorOption option)
        {
            bool addOutlines  = option.AddOutlines;
            bool copyOutlines = option.CopyOutlines;

            int i;

            bool[] appendOrNots = new bool[reader.NumberOfPages + 1];
            foreach (var pr in pageRanges)
            {
                for (int j = pr.StartPage; j <= pr.EndPage; j++)
                {
                    appendOrNots[j] = true;
                }
            }

            int theFirstPage = 0;

            for (i = 1; i <= reader.NumberOfPages; i++)
            {
                if (appendOrNots[i])
                {
                    theFirstPage = i;
                    break;
                }
            }
            if (theFirstPage == 0)
            {
                // There is no append paes.
                return;
            }

            if (!opened)
            {
                opened   = true;
                document = new Document(reader.GetPageSizeWithRotation(theFirstPage));
                writer   = new PdfCopy(document, outStream);
                writer.SetMergeFields();

                if (ownerPassword != null)
                {
                    byte[] bytesUserPassword = userPassword == null ? null : Encoding.ASCII.GetBytes(userPassword);
                    writer.SetEncryption(bytesUserPassword, Encoding.ASCII.GetBytes(ownerPassword), permissions, encryptionStrength);
                }
                writer.ViewerPreferences = this.viewerPreference;

                document.Open();
            }

            if (bookmarks == null)
            {
                if (addOutlines || copyOutlines)
                {
                    bookmarks = new List <Dictionary <string, object> >();
                }
            }
            if (bookmarks != null)
            {
                Dictionary <string, object> m = null;
                if (addOutlines)
                {
                    m           = new Dictionary <string, object>();
                    m["Title"]  = title;
                    m["Action"] = "GoTo";
                    m["Page"]   = currPageNum.ToString() + " " + option.FittingStyle;
                }
                if (copyOutlines)
                {
                    var cpbookmarks = SimpleBookmark.GetBookmark(reader);

                    if (cpbookmarks != null)
                    {
                        int[] elimPages  = new int[2];
                        int[] shiftPages = new int[2];
                        shiftPages[1] = reader.NumberOfPages;
                        for (int pageIndex = reader.NumberOfPages; pageIndex > 0; --pageIndex)
                        {
                            if (!appendOrNots[pageIndex])
                            {
                                elimPages[0]  = elimPages[1] = pageIndex;
                                shiftPages[0] = pageIndex + 1;
                                SimpleBookmark.EliminatePages(cpbookmarks, elimPages);
                                SimpleBookmark.ShiftPageNumbers(cpbookmarks, -1, shiftPages);
                            }
                        }
                        SimpleBookmark.ShiftPageNumbers(cpbookmarks, currPageNum - 1, null);

                        if (m == null)
                        {
                            foreach (var c in cpbookmarks)
                            {
                                bookmarks.Add(c);
                            }
                        }
                        else
                        {
                            m["Kids"] = cpbookmarks;
                        }
                    }
                }
                if (m != null)
                {
                    bookmarks.Add(m);
                }
            }

            {
                var pages = new List <int>();
                for (int ii = 1; ii < appendOrNots.Length; ii++)
                {
                    if (appendOrNots[ii])
                    {
                        pages.Add(ii);
                        currPageNum++;
                    }
                }
                reader.SelectPages(pages);
                writer.AddDocument(reader);
            }
        }