public PdfSharpSplitterMerger()
 {
     this.Subject        = string.Empty;
     this.Author         = Environment.UserName;
     this.Title          = string.Empty;
     this.outputDocument = new PdfDocument();
     this.bm             = new PdfBookmark(0);
     this.lastAddedCount = 0;
 }
        public int DoAddBookmarksFromFile(ref PdfBookmark bm, int page, int level)
        {
            // get the outline object
            PdfDictionary outline = (PdfDictionary)this.inputDocument.Internals.Catalog.Elements.GetObject("/Outlines");

            if (outline == null)
            {
                return(0);
            }

            PdfDictionary first = (PdfDictionary)outline.Elements.GetObject("/First");

            if (first == null)
            {
                return(0);
            }

            // add the bookmarks
            return(this.AddSubBookmarksFromFile(ref bm, page, level, first));
        }
Esempio n. 3
0
        public int AddBookmark(string title, int page, int add_at_level)
        {
            if (add_at_level == this.level)
            {
                // add to this object
                this.bookmarkObjects.Add(new PdfBookmark(this.level + 1));
                PdfBookmark mark = this.bookmarkObjects[this.bookmarkObjects.Count - 1] as PdfBookmark;
                mark.pageIndex = page - 1;
                mark.title     = title.Replace(">", string.Empty);
                mark.title     = mark.title.Replace("(", string.Empty);
                mark.title     = mark.title.Replace(")", string.Empty);
                mark.parent    = this;
                if (this.bookmarkObjects.Count > 1)
                {
                    mark.prev = this.bookmarkObjects[this.bookmarkObjects.Count - 2] as PdfBookmark;
                }

                if (mark.prev != null)
                {
                    mark.prev.next = mark;
                }
            }
            else
            {
                // add to a sub object
                if (this.bookmarkObjects.Count == 0)
                {
                    throw new Exception("Bookmark hierarchy is invalid");
                }

                PdfBookmark mark = this.bookmarkObjects[this.bookmarkObjects.Count - 1] as PdfBookmark;
                mark.AddBookmark(title, page, add_at_level);
            }

            return(this.GetCount());
        }
        public int AddSubBookmarksFromFile(ref PdfBookmark bm, int page, int level, PdfDictionary outline)
        {
            int bmCount = 0;

            PdfDictionary mark = outline;

            // get titles and pages of all bookmarks at this level
            while (mark != null)
            {
                int level_offset = 0;

                // get the title
                string title = mark.Elements.GetString("/Title");
                if (title == null)
                {
                    return(bmCount);
                }

                // direct
                PdfObject dest = mark.Elements.GetObject("/Dest");

                // indirect
                if (dest == null)
                {
                    PdfDictionary @ref = (PdfDictionary)mark.Elements.GetObject("/A");
                    if (@ref != null)
                    {
                        // if null this is a blind bookmark
                        dest = (PdfArray)@ref.Elements.GetObject("/D");
                    }
                }

                // indirectly named (per 1.6 sample from acrobat website)
                // used for multiple bookmarks on same page
                if (dest == null)
                {
                    dest = this.GetNamedDestination(mark);
                }

                // add the bookmark. if it isn't a blind bookmark
                if (dest != null)
                {
                    PdfReference pref = null;
                    if (dest is PdfArray)
                    {
                        PdfArray pDest = (PdfArray)dest;
                        if (pDest.Elements[0] is PdfReference)
                        {
                            pref = (PdfReference)pDest.Elements[0];
                        }
                    }

                    if (pref != null)
                    {
                        // convert page to offset
                        int page_offset = -1;
                        for (int x = 0; x < this.inputDocument.Pages.Count; ++x)
                        {
                            PdfReference pageRef = (PdfReference)this.inputDocument.Pages.PagesArray.Elements[x];
                            if (pageRef == pref)
                            {
                                page_offset = x;
                                break;
                            }
                        }

                        // check if this page is being added or not
                        bool included = false;
                        if (this.lastPageList == null)
                        {
                            // null means all pages
                            included = true;
                        }
                        else
                        {
                            // check if page needs to be included
                            for (int x = 0; x < this.lastPageList.Length; ++x)
                            {
                                if (this.lastPageList[x] == page_offset)
                                {
                                    included    = true;
                                    page_offset = x;
                                    break;
                                }
                            }
                        }

                        // to do - show a warning for bad bookmark?
                        // if (page_offset == -1)
                        //    throw new Exception(string.Format(
                        //        "Warning: Bookmark '{0}' refers to page object {1} which does not exist, bookmark will be ignored", title, pref.ObjectNumber));
                        if (page_offset != -1 && included == true)
                        {
                            bm.AddBookmark(title, page_offset + page, level);
                            ++bmCount;
                            level_offset = 1;
                        }
                    }
                }

                // if this bookmark has children, add them recursively
                PdfDictionary child = (PdfDictionary)mark.Elements.GetObject("/First");
                if (child != null)
                {
                    bmCount += this.AddSubBookmarksFromFile(ref bm, page, level + level_offset, child);
                }

                // get the next mark
                mark = (PdfDictionary)mark.Elements.GetObject("/Next");
            }

            return(bmCount);
        }