Beispiel #1
0
        /// <summary>
        /// Inserts the specified PdfPage at the specified position to this document and maybe returns a new PdfPage object.
        /// The value returned is a new object if the inserted page comes from a foreign document.
        /// </summary>
        public PdfPage Insert(int index, PdfPage page)
        {
            if (page == null)
            {
                throw new ArgumentNullException("page");
            }

            // Is the page already owned by this document
            if (page.Owner == Owner)
            {
                // Case: Page is removed and than inserted a another position.
                int count = Count;
                for (int idx = 0; idx < count; idx++)
                {
                    if (ReferenceEquals(this[idx], page))
                    {
                        throw new InvalidOperationException(PSSR.MultiplePageInsert);
                    }
                }
                // TODO: check this case
                Owner.irefTable.Add(page);
                PagesArray.Elements.Insert(index, page.Reference);
                Elements.SetInteger(PdfPages.Keys.Count, PagesArray.Elements.Count);
                return(page);
            }

            // All page insertions come here
            if (page.Owner == null)
            {
                // Case: New page was created and inserted now.
                page.Document = Owner;

                Owner.irefTable.Add(page);
                PagesArray.Elements.Insert(index, page.Reference);
                Elements.SetInteger(PdfPages.Keys.Count, PagesArray.Elements.Count);
            }
            else
            {
                // Case: Page is from an external document -> import it.
                page = ImportExternalPage(page);
                Owner.irefTable.Add(page);
                PagesArray.Elements.Insert(index, page.Reference);
                Elements.SetInteger(PdfPages.Keys.Count, PagesArray.Elements.Count);
                PdfAnnotations.FixImportedAnnotation(page);
            }
            if (Owner.Settings.TrimMargins.AreSet)
            {
                page.TrimMargins = Owner.Settings.TrimMargins;
            }
            return(page);
        }
Beispiel #2
0
        /// <summary>
        /// Inserts the specified PdfPage at the specified position to this document and maybe returns a new PdfPage object.
        /// The value returned is a new object if the inserted page comes from a foreign document.
        /// </summary>
        public PdfPage Insert(int index, PdfPage page)
        {
            if (page == null)
            {
                throw new ArgumentNullException("page");
            }

            // Is the page already owned by this document?
            if (page.Owner == Owner)
            {
                // Case: Page is first removed and than inserted again, maybe at another position.
                int count = Count;
                // Check if page is not already part of the document.
                for (int idx = 0; idx < count; idx++)
                {
                    if (ReferenceEquals(this[idx], page))
                    {
                        throw new InvalidOperationException(PSSR.MultiplePageInsert);
                    }
                }

                // TODO: check this case
                // Because the owner of the inserted page is this document we assume that the page was former part of it
                // and it is therefore well-defined.
                Owner._irefTable.Add(page);
                Debug.Assert(page.Owner == Owner);

                // Insert page in array.
                PagesArray.Elements.Insert(index, page.Reference);

                // Update page count.
                Elements.SetInteger(Keys.Count, PagesArray.Elements.Count);

                return(page);
            }

            // All new page insertions come here.
            if (page.Owner == null)
            {
                // Case: New page was newly created and inserted now.
                page.Document = Owner;

                Owner._irefTable.Add(page);
                Debug.Assert(page.Owner == Owner);
                PagesArray.Elements.Insert(index, page.Reference);
                Elements.SetInteger(Keys.Count, PagesArray.Elements.Count);
            }
            else
            {
                // Case: Page is from an external document -> import it.
                PdfPage importPage = page;
                page = ImportExternalPage(importPage);
                Owner._irefTable.Add(page);

                // Add page substitute to importedObjectTable.
                PdfImportedObjectTable importedObjectTable = Owner.FormTable.GetImportedObjectTable(importPage);
                importedObjectTable.Add(importPage.ObjectID, page.Reference);

                PagesArray.Elements.Insert(index, page.Reference);
                Elements.SetInteger(Keys.Count, PagesArray.Elements.Count);
                PdfAnnotations.FixImportedAnnotation(page);
            }
            if (Owner.Settings.TrimMargins.AreSet)
            {
                page.TrimMargins = Owner.Settings.TrimMargins;
            }
            return(page);
        }
Beispiel #3
0
        /// <summary>
        /// Inserts the specified PdfPage at the specified position to this document and maybe returns a new PdfPage object.
        /// The value returned is a new object if the inserted page comes from a foreign document.
        /// </summary>
        public PdfPage Insert(int index, PdfPage page)
        {
            if (page == null)
            {
                throw new ArgumentNullException("page");
            }

            // Is the page already owned by this document?
            if (page.Owner == Owner)
            {
                // Case: Page is first removed and than inserted again, maybe at another position.
                int count = Count;
                // Check if page is not already part of the document.
                for (int idx = 0; idx < count; idx++)
                {
                    if (ReferenceEquals(this[idx], page))
                    {
                        throw new InvalidOperationException(PSSR.MultiplePageInsert);
                    }
                }

                // TODO: check this case
                // Because the owner of the inserted page is this document we assume that the page was former part of it
                // and it is therefore well-defined.
                Owner._irefTable.Add(page);
                Debug.Assert(page.Owner == Owner);

                // Insert page in array.
                PagesArray.Elements.Insert(index, page.Reference);

                // Update page count.
                Elements.SetInteger(Keys.Count, PagesArray.Elements.Count);

                // @PDF/UA: Pages must not be moved.
                if (_document._uaManager != null)
                {
                    _document.Events.OnPageAdded(_document, new PageEventArgs {
                        Page = page, PageIndex = index, EventType = PageEventType.Moved
                    });
                }

                return(page);
            }

            // All new page insertions come here.
            if (page.Owner == null)
            {
                // Case: New page was newly created and inserted now.
                page.Document = Owner;

                Owner._irefTable.Add(page);
                Debug.Assert(page.Owner == Owner);
                PagesArray.Elements.Insert(index, page.Reference);
                Elements.SetInteger(Keys.Count, PagesArray.Elements.Count);

                // @PDF/UA: Page was created.
                if (_document._uaManager != null)
                {
                    _document.Events.OnPageAdded(_document, new PageEventArgs {
                        Page = page, PageIndex = index, EventType = PageEventType.Created
                    });
                }
            }
            else
            {
#if true
                // original code
                // Case: Page is from an external document -> import it.
                PdfPage importPage = page;
                page = ImportExternalPage(importPage);
                Owner._irefTable.Add(page);

                // Add page substitute to importedObjectTable.
                PdfImportedObjectTable importedObjectTable = Owner.FormTable.GetImportedObjectTable(importPage);
                importedObjectTable.Add(importPage.ObjectID, page.Reference);

                PagesArray.Elements.Insert(index, page.Reference);
                Elements.SetInteger(Keys.Count, PagesArray.Elements.Count);
                PdfAnnotations.FixImportedAnnotation(page);
#else
                var externalPage = page;
                // Case: Page is from an external document -> import it.
                page = ImportExternalPage(externalPage);

                Owner._irefTable.Add(page);
                // Add page substitute to importedObjectTable.
                PdfImportedObjectTable importedObjectTable = Owner.FormTable.GetImportedObjectTable(externalPage);
                importedObjectTable.Add(externalPage.ObjectID, page.Reference);

                PagesArray.Elements.Insert(index, page.Reference);
                Elements.SetInteger(Keys.Count, PagesArray.Elements.Count);

                PagesArray.Elements.Insert(index, page.Reference);
                Elements.SetInteger(PdfPages.Keys.Count, PagesArray.Elements.Count);

                // Import AcroFields, after the external Page has been imported and we have a valid ObjectID for the imported Page
                ImportAcroFields(page, externalPage);
                PdfAnnotations.FixImportedAnnotation(page);
#endif

                // @PDF/UA: Page was imported.
                if (_document._uaManager != null)
                {
                    _document.Events.OnPageAdded(_document, new PageEventArgs {
                        Page = page, PageIndex = index, EventType = PageEventType.Imported
                    });
                }
            }
            if (Owner.Settings.TrimMargins.AreSet)
            {
                page.TrimMargins = Owner.Settings.TrimMargins;
            }

            return(page);
        }