Пример #1
0
        /**
         * Add an imported page to our output
         * @param iPage an imported page
         * @throws IOException, BadPdfFormatException
         */
        public void AddPage(PdfImportedPage iPage)
        {
            int pageNum = SetFromIPage(iPage);

            PdfDictionary       thePage = reader.GetPageN(pageNum);
            PRIndirectReference origRef = reader.GetPageOrigRef(pageNum);

            reader.ReleasePage(pageNum);
            RefKey key = new RefKey(origRef);
            PdfIndirectReference pageRef;
            IndirectReferences   iRef;

            indirects.TryGetValue(key, out iRef);
            if (iRef != null && !iRef.Copied)
            {
                pageReferences.Add(iRef.Ref);
                iRef.SetCopied();
            }
            pageRef = CurrentPage;
            if (iRef == null)
            {
                iRef           = new IndirectReferences(pageRef);
                indirects[key] = iRef;
            }
            iRef.SetCopied();
            PdfDictionary newPage = CopyDictionary(thePage);

            root.AddPage(newPage);
            iPage.SetCopied();
            ++currentPageNumber;
        }
Пример #2
0
 /**
  * convenience method. Given a reader, set our "globals"
  */
 protected void SetFromReader(PdfReader reader)
 {
     this.reader = reader;
     indirects   = (Hashtable)indirectMap[reader];
     if (indirects == null)
     {
         indirects           = new Hashtable();
         indirectMap[reader] = indirects;
         PdfDictionary       catalog = reader.Catalog;
         PRIndirectReference refi    = (PRIndirectReference)catalog.Get(PdfName.PAGES);
         indirects[new RefKey(refi)] = new IndirectReferences(topPageParent);
         refi = null;
         PdfObject o = catalog.Get(PdfName.ACROFORM);
         if (o == null || o.Type != PdfObject.INDIRECT)
         {
             return;
         }
         refi = (PRIndirectReference)o;
         if (acroForm == null)
         {
             acroForm = body.PdfIndirectReference;
         }
         indirects[new RefKey(refi)] = new IndirectReferences(acroForm);
     }
 }
        /// <summary>
        /// Translate a PRIndirectReference to a PdfIndirectReference
        /// In addition, translates the object numbers, and copies the
        /// referenced object to the output file.
        /// NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
        /// file they came from, because each file has its own namespace. The translation
        /// we do from their namespace to ours is *at best* heuristic, and guaranteed to
        /// fail under some circumstances.
        /// </summary>
        protected virtual PdfIndirectReference CopyIndirect(PrIndirectReference inp)
        {
            PdfIndirectReference theRef;
            var key  = new RefKey(inp);
            var iRef = (IndirectReferences)Indirects[key];

            if (iRef != null)
            {
                theRef = iRef.Ref;
                if (iRef.Copied)
                {
                    return(theRef);
                }
            }
            else
            {
                theRef         = Body.PdfIndirectReference;
                iRef           = new IndirectReferences(theRef);
                Indirects[key] = iRef;
            }
            var obj = PdfReader.GetPdfObjectRelease(inp);

            if (obj != null && obj.IsDictionary())
            {
                var type = PdfReader.GetPdfObjectRelease(((PdfDictionary)obj).Get(PdfName.TYPE));
                if (type != null && PdfName.Page.Equals(type))
                {
                    return(theRef);
                }
            }
            iRef.SetCopied();
            obj = CopyObject(obj);
            AddToBody(obj, theRef);
            return(theRef);
        }
        /// <summary>
        /// convenience method. Given a reader, set our "globals"
        /// </summary>
        protected void SetFromReader(PdfReader reader)
        {
            Reader    = reader;
            Indirects = (Hashtable)IndirectMap[reader];
            if (Indirects == null)
            {
                Indirects           = new Hashtable();
                IndirectMap[reader] = Indirects;
                var catalog = reader.Catalog;
                PrIndirectReference refi = null;
                var o = catalog.Get(PdfName.Acroform);
                if (o == null || o.Type != PdfObject.INDIRECT)
                {
                    return;
                }

                refi = (PrIndirectReference)o;
                if (acroForm == null)
                {
                    acroForm = Body.PdfIndirectReference;
                }

                Indirects[new RefKey(refi)] = new IndirectReferences(acroForm);
            }
        }
        /// <summary>
        /// Add an imported page to our output
        /// @throws IOException, BadPdfFormatException
        /// </summary>
        /// <param name="iPage">an imported page</param>
        public void AddPage(PdfImportedPage iPage)
        {
            var pageNum = SetFromIPage(iPage);

            var thePage = Reader.GetPageN(pageNum);
            var origRef = Reader.GetPageOrigRef(pageNum);

            Reader.ReleasePage(pageNum);
            var key = new RefKey(origRef);
            PdfIndirectReference pageRef;
            var iRef = (IndirectReferences)Indirects[key];

            if (iRef != null && !iRef.Copied)
            {
                PageReferences.Add(iRef.Ref);
                iRef.SetCopied();
            }
            pageRef = CurrentPage;
            if (iRef == null)
            {
                iRef           = new IndirectReferences(pageRef);
                Indirects[key] = iRef;
            }
            iRef.SetCopied();
            var newPage = CopyDictionary(thePage);

            Root.AddPage(newPage);
            ++currentPageNumber;
        }
Пример #6
0
        /**
         * Translate a PRIndirectReference to a PdfIndirectReference
         * In addition, translates the object numbers, and copies the
         * referenced object to the output file.
         * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
         * file they came from, because each file has its own namespace. The translation
         * we do from their namespace to ours is *at best* heuristic, and guaranteed to
         * fail under some circumstances.
         */
        protected PdfIndirectReference CopyIndirect(PRIndirectReference inp)
        {
            PdfIndirectReference theRef;
            RefKey             key  = new RefKey(inp);
            IndirectReferences iRef = (IndirectReferences)indirects[key];

            if (iRef != null)
            {
                theRef = iRef.Ref;
                if (iRef.Copied)
                {
                    return(theRef);
                }
            }
            else
            {
                theRef         = body.PdfIndirectReference;
                iRef           = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            iRef.SetCopied();
            PdfObject obj = CopyObject(PdfReader.GetPdfObjectRelease(inp));

            AddToBody(obj, theRef);
            return(theRef);
        }
Пример #7
0
        /**
         * Add an imported page to our output
         * @param iPage an imported page
         * @throws IOException, BadPdfFormatException
         */
        public void AddPage(PdfImportedPage iPage)
        {
            int pageNum = SetFromIPage(iPage);

            PdfDictionary       thePage = reader.GetPageN(pageNum);
            PRIndirectReference origRef = reader.GetPageOrigRef(pageNum);

            reader.ReleasePage(pageNum);
            RefKey key = new RefKey(origRef);
            PdfIndirectReference pageRef;
            IndirectReferences   iRef = (IndirectReferences)indirects[key];

            // if we already have an iref for the page (we got here by another link)
            if (iRef != null)
            {
                pageRef = iRef.Ref;
            }
            else
            {
                pageRef        = body.PdfIndirectReference;
                iRef           = new IndirectReferences(pageRef);
                indirects[key] = iRef;
            }
            pageReferences.Add(pageRef);
            ++currentPageNumber;
            if (!iRef.Copied)
            {
                iRef.SetCopied();
                PdfDictionary newPage = CopyDictionary(thePage);
                newPage.Put(PdfName.PARENT, topPageParent);
                AddToBody(newPage, pageRef);
            }
            root.AddPage(pageRef);
            pageNumbersToRefs.Add(pageRef);
        }
Пример #8
0
        /**
         * Translate a PRIndirectReference to a PdfIndirectReference
         * In addition, translates the object numbers, and copies the
         * referenced object to the output file.
         * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
         * file they came from, because each file has its own namespace. The translation
         * we do from their namespace to ours is *at best* heuristic, and guaranteed to
         * fail under some circumstances.
         */
        protected virtual PdfIndirectReference CopyIndirect(PRIndirectReference inp, bool keepStructure, bool directRootKids)
        {
            PdfIndirectReference theRef;
            RefKey             key = new RefKey(inp);
            IndirectReferences iRef;

            indirects.TryGetValue(key, out iRef);
            PdfObject obj = PdfReader.GetPdfObjectRelease(inp);

            if ((keepStructure) && (directRootKids))
            {
                if (obj is PdfDictionary)
                {
                    PdfDictionary dict = (PdfDictionary)obj;
                    if (dict.Contains(PdfName.PG))
                    {
                        return(null);
                    }
                }
            }
            if (iRef != null)
            {
                theRef = iRef.Ref;
                if (iRef.Copied)
                {
                    return(theRef);
                }
            }
            else
            {
                theRef         = body.PdfIndirectReference;
                iRef           = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            if (obj != null && obj.IsDictionary())
            {
                PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)obj).Get(PdfName.TYPE));
                if (type != null && PdfName.PAGE.Equals(type))
                {
                    return(theRef);
                }
            }
            iRef.SetCopied();
            parentObjects[obj] = inp;
            PdfObject res = CopyObject(obj, keepStructure, directRootKids);

            if (disableIndirects.ContainsKey(obj))
            {
                iRef.Copied = false;
            }
            if ((res != null) && !(res is PdfNull))
            {
                AddToBody(res, theRef);
                return(theRef);
            }
            indirects.Remove(key);
            return(null);
        }
Пример #9
0
        /**
         * Translate a PRIndirectReference to a PdfIndirectReference
         * In addition, translates the object numbers, and copies the
         * referenced object to the output file if it wasn't available
         * in the cache yet. If it's in the cache, the reference to
         * the already used stream is returned.
         *
         * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
         * file they came from, because each file has its own namespace. The translation
         * we do from their namespace to ours is *at best* heuristic, and guaranteed to
         * fail under some circumstances.
         */
        protected override PdfIndirectReference CopyIndirect(PRIndirectReference inp)
        {
            PdfObject srcObj      = PdfReader.GetPdfObjectRelease(inp);
            ByteStore streamKey   = null;
            bool      validStream = false;

            if (srcObj.IsStream())
            {
                streamKey   = new ByteStore((PRStream)srcObj);
                validStream = true;
                PdfIndirectReference streamRef;
                if (streamMap.TryGetValue(streamKey, out streamRef))
                {
                    return(streamRef);
                }
            }

            PdfIndirectReference theRef;
            RefKey             key = new RefKey(inp);
            IndirectReferences iRef;

            indirects.TryGetValue(key, out iRef);
            if (iRef != null)
            {
                theRef = iRef.Ref;
                if (iRef.Copied)
                {
                    return(theRef);
                }
            }
            else
            {
                theRef         = body.PdfIndirectReference;
                iRef           = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            if (srcObj != null && srcObj.IsDictionary())
            {
                PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)srcObj).Get(PdfName.TYPE));
                if (type != null && PdfName.PAGE.Equals(type))
                {
                    return(theRef);
                }
            }
            iRef.SetCopied();

            if (validStream)
            {
                streamMap[streamKey] = theRef;
            }

            PdfObject obj = CopyObject(srcObj);

            AddToBody(obj, theRef);
            return(theRef);
        }
Пример #10
0
        /// <summary>
        /// Translate a PRIndirectReference to a PdfIndirectReference
        /// In addition, translates the object numbers, and copies the
        /// referenced object to the output file if it wasn't available
        /// in the cache yet. If it's in the cache, the reference to
        /// the already used stream is returned.
        /// NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
        /// file they came from, because each file has its own namespace. The translation
        /// we do from their namespace to ours is *at best* heuristic, and guaranteed to
        /// fail under some circumstances.
        /// </summary>
        protected override PdfIndirectReference CopyIndirect(PrIndirectReference inp)
        {
            var       srcObj      = PdfReader.GetPdfObjectRelease(inp);
            ByteStore streamKey   = null;
            var       validStream = false;

            if (srcObj.IsStream())
            {
                streamKey   = new ByteStore((PrStream)srcObj);
                validStream = true;
                var streamRef = (PdfIndirectReference)_streamMap[streamKey];
                if (streamRef != null)
                {
                    return(streamRef);
                }
            }

            PdfIndirectReference theRef;
            var key  = new RefKey(inp);
            var iRef = (IndirectReferences)Indirects[key];

            if (iRef != null)
            {
                theRef = iRef.Ref;
                if (iRef.Copied)
                {
                    return(theRef);
                }
            }
            else
            {
                theRef         = Body.PdfIndirectReference;
                iRef           = new IndirectReferences(theRef);
                Indirects[key] = iRef;
            }
            if (srcObj != null && srcObj.IsDictionary())
            {
                var type = PdfReader.GetPdfObjectRelease(((PdfDictionary)srcObj).Get(PdfName.TYPE));
                if (type != null && PdfName.Page.Equals(type))
                {
                    return(theRef);
                }
            }
            iRef.SetCopied();

            if (validStream)
            {
                _streamMap[streamKey] = theRef;
            }

            var obj = CopyObject(srcObj);

            AddToBody(obj, theRef);
            return(theRef);
        }
Пример #11
0
        protected override PdfIndirectReference CopyIndirect(PRIndirectReference @in) {
            PdfObject srcObj = PdfReader.GetPdfObjectRelease(@in);
            PdfSmartCopy.ByteStore streamKey = null;
            bool validStream = false;
            if (srcObj.IsStream()) {
                streamKey = new PdfSmartCopy.ByteStore((PRStream) srcObj, serialized);
                validStream = true;
                PdfIndirectReference streamRef;
                if (streamMap.TryGetValue(streamKey, out streamRef)) {
                    return streamRef;
                }
            } else if (srcObj.IsDictionary()) {
                streamKey = new PdfSmartCopy.ByteStore((PdfDictionary) srcObj, serialized);
                validStream = true;
                PdfIndirectReference streamRef;
                if (streamMap.TryGetValue(streamKey, out streamRef)) {
                    return streamRef;
                }
            }

            PdfIndirectReference theRef;
            RefKey key = new RefKey(@in);
            IndirectReferences iRef;
            if (indirects.TryGetValue(key, out iRef)) {
                theRef = iRef.Ref;
                if (iRef.Copied) {
                    return theRef;
                }
            } else {
                theRef = body.PdfIndirectReference;
                iRef = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            if (srcObj.IsDictionary()) {
                PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary) srcObj).Get(PdfName.TYPE));
                if (type != null && PdfName.PAGE.Equals(type)) {
                    return theRef;
                }
            }
            iRef.SetCopied();

            if (validStream) {
                streamMap[streamKey] = theRef;
            }

            PdfObject obj = CopyObject(srcObj);
            AddToBody(obj, theRef);
            return theRef;
        }
        /// <summary>
        /// Copy the acroform for an input document. Note that you can only have one,
        /// we make no effort to merge them.
        /// @throws IOException, BadPdfFormatException
        /// </summary>
        /// <param name="reader">The reader of the input file that is being copied</param>
        public void CopyAcroForm(PdfReader reader)
        {
            SetFromReader(reader);

            var catalog = reader.Catalog;
            PrIndirectReference hisRef = null;
            var o = catalog.Get(PdfName.Acroform);

            if (o != null && o.Type == PdfObject.INDIRECT)
            {
                hisRef = (PrIndirectReference)o;
            }

            if (hisRef == null)
            {
                return; // bugfix by John Engla
            }

            var key = new RefKey(hisRef);
            PdfIndirectReference myRef;
            var iRef = (IndirectReferences)Indirects[key];

            if (iRef != null)
            {
                acroForm = myRef = iRef.Ref;
            }
            else
            {
                acroForm       = myRef = Body.PdfIndirectReference;
                iRef           = new IndirectReferences(myRef);
                Indirects[key] = iRef;
            }
            if (!iRef.Copied)
            {
                iRef.SetCopied();
                var theForm = CopyDictionary((PdfDictionary)PdfReader.GetPdfObject(hisRef));
                AddToBody(theForm, myRef);
            }
        }
Пример #13
0
        /**
         * Copy the acroform for an input document. Note that you can only have one,
         * we make no effort to merge them.
         * @param reader The reader of the input file that is being copied
         * @throws IOException, BadPdfFormatException
         */
        public void CopyAcroForm(PdfReader reader)
        {
            SetFromReader(reader);

            PdfDictionary       catalog = reader.Catalog;
            PRIndirectReference hisRef  = null;
            PdfObject           o       = catalog.Get(PdfName.ACROFORM);

            if (o != null && o.Type == PdfObject.INDIRECT)
            {
                hisRef = (PRIndirectReference)o;
            }
            if (hisRef == null)
            {
                return;                 // bugfix by John Engla
            }
            RefKey key = new RefKey(hisRef);
            PdfIndirectReference myRef;
            IndirectReferences   iRef;

            indirects.TryGetValue(key, out iRef);
            if (iRef != null)
            {
                acroForm = myRef = iRef.Ref;
            }
            else
            {
                acroForm       = myRef = body.PdfIndirectReference;
                iRef           = new IndirectReferences(myRef);
                indirects[key] = iRef;
            }
            if (!iRef.Copied)
            {
                iRef.SetCopied();
                PdfDictionary theForm = CopyDictionary((PdfDictionary)PdfReader.GetPdfObject(hisRef));
                AddToBody(theForm, myRef);
            }
        }
Пример #14
0
 /**
  * convenience method. Given a reader, set our "globals"
  */
 protected void SetFromReader(PdfReader reader)
 {
     this.reader = reader;
     indirectMap.TryGetValue(reader, out indirects);
     if (indirects == null)
     {
         indirects           = new Dictionary <RefKey, IndirectReferences>();
         indirectMap[reader] = indirects;
         PdfDictionary       catalog = reader.Catalog;
         PRIndirectReference refi    = null;
         PdfObject           o       = catalog.Get(PdfName.ACROFORM);
         if (o == null || o.Type != PdfObject.INDIRECT)
         {
             return;
         }
         refi = (PRIndirectReference)o;
         if (acroForm == null)
         {
             acroForm = body.PdfIndirectReference;
         }
         indirects[new RefKey(refi)] = new IndirectReferences(acroForm);
     }
 }
Пример #15
0
        /**
         * Translate a PRIndirectReference to a PdfIndirectReference
         * In addition, translates the object numbers, and copies the
         * referenced object to the output file.
         * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
         * file they came from, because each file has its own namespace. The translation
         * we do from their namespace to ours is *at best* heuristic, and guaranteed to
         * fail under some circumstances.
         */
        protected virtual PdfIndirectReference CopyIndirect(PRIndirectReference inp)
        {
            PdfIndirectReference theRef;
            RefKey             key = new RefKey(inp);
            IndirectReferences iRef;

            indirects.TryGetValue(key, out iRef);
            if (iRef != null)
            {
                theRef = iRef.Ref;
                if (iRef.Copied)
                {
                    return(theRef);
                }
            }
            else
            {
                theRef         = body.PdfIndirectReference;
                iRef           = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            PdfObject obj = PdfReader.GetPdfObjectRelease(inp);

            if (obj != null && obj.IsDictionary())
            {
                PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)obj).Get(PdfName.TYPE));
                if (type != null && PdfName.PAGE.Equals(type))
                {
                    return(theRef);
                }
            }
            iRef.SetCopied();
            obj = CopyObject(obj);
            AddToBody(obj, theRef);
            return(theRef);
        }
Пример #16
0
        /**
        * Add an imported page to our output
        * @param iPage an imported page
        * @throws IOException, BadPdfFormatException
        */
        public void AddPage(PdfImportedPage iPage)
        {
            int pageNum = SetFromIPage(iPage);

            PdfDictionary thePage = reader.GetPageN(pageNum);
            PRIndirectReference origRef = reader.GetPageOrigRef(pageNum);
            reader.ReleasePage(pageNum);
            RefKey key = new RefKey(origRef);
            PdfIndirectReference pageRef;
            IndirectReferences iRef;
            indirects.TryGetValue(key, out iRef);
            if (iRef != null && !iRef.Copied) {
                pageReferences.Add(iRef.Ref);
                iRef.SetCopied();
            }
            pageRef = CurrentPage;
            if (iRef == null) {
                iRef = new IndirectReferences(pageRef);
                indirects[key] = iRef;
            }
            iRef.SetCopied();
            PdfDictionary newPage = CopyDictionary(thePage);
            root.AddPage(newPage);
            iPage.SetCopied();
            ++currentPageNumber;
        }
Пример #17
0
        /**
        * Add an imported page to our output
        * @param iPage an imported page
        * @throws IOException, BadPdfFormatException
        */
        public void AddPage(PdfImportedPage iPage)
        {
            int pageNum = SetFromIPage(iPage);

            PdfDictionary thePage = reader.GetPageN(pageNum);
            PRIndirectReference origRef = reader.GetPageOrigRef(pageNum);
            reader.ReleasePage(pageNum);
            RefKey key = new RefKey(origRef);
            PdfIndirectReference pageRef;
            IndirectReferences iRef = (IndirectReferences)indirects[key] ;
            iRef = null; // temporary hack to have multiple pages, may break is some cases
            // if we already have an iref for the page (we got here by another link)
            if (iRef != null) {
                pageRef = iRef.Ref;
            }
            else {
                pageRef = body.PdfIndirectReference;
                iRef = new IndirectReferences(pageRef);
                indirects[key] =  iRef;
            }
            pageReferences.Add(pageRef);
            ++currentPageNumber;
            if (! iRef.Copied) {
                iRef.SetCopied();
                PdfDictionary newPage = CopyDictionary(thePage);
                newPage.Put(PdfName.PARENT, topPageParent);
                AddToBody(newPage, pageRef);
            }
            root.AddPage(pageRef);
            pageNumbersToRefs.Add(pageRef);
        }
Пример #18
0
 /**
 * Translate a PRIndirectReference to a PdfIndirectReference
 * In addition, translates the object numbers, and copies the
 * referenced object to the output file.
 * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
 * file they came from, because each file has its own namespace. The translation
 * we do from their namespace to ours is *at best* heuristic, and guaranteed to
 * fail under some circumstances.
 */
 protected virtual PdfIndirectReference CopyIndirect(PRIndirectReference inp)
 {
     PdfIndirectReference theRef;
     RefKey key = new RefKey(inp);
     IndirectReferences iRef = (IndirectReferences)indirects[key] ;
     if (iRef != null) {
         theRef = iRef.Ref;
         if (iRef.Copied) {
             return theRef;
         }
     }
     else {
         theRef = body.PdfIndirectReference;
         iRef = new IndirectReferences(theRef);
         indirects[key] =  iRef;
     }
     iRef.SetCopied();
     PdfObject obj = CopyObject(PdfReader.GetPdfObjectRelease(inp));
     AddToBody(obj, theRef);
     return theRef;
 }
Пример #19
0
 /**
 * convenience method. Given a reader, set our "globals"
 */
 protected void SetFromReader(PdfReader reader)
 {
     this.reader = reader;
     indirects = (Hashtable)indirectMap[reader] ;
     if (indirects == null) {
         indirects = new Hashtable();
         indirectMap[reader] = indirects;
         PdfDictionary catalog = reader.Catalog;
         PRIndirectReference refi = (PRIndirectReference)catalog.Get(PdfName.PAGES);
         indirects[new RefKey(refi)] =  new IndirectReferences(topPageParent);
         refi = null;
         PdfObject o = catalog.Get(PdfName.ACROFORM);
         if (o == null || o.Type != PdfObject.INDIRECT)
             return;
         refi = (PRIndirectReference)o;
         if (acroForm == null) acroForm = body.PdfIndirectReference;
         indirects[new RefKey(refi)] =  new IndirectReferences(acroForm);
     }
 }
Пример #20
0
        protected override PdfIndirectReference CopyIndirect(PRIndirectReference @in)
        {
            PdfObject srcObj = PdfReader.GetPdfObjectRelease(@in);

            PdfSmartCopy.ByteStore streamKey = null;
            bool validStream = false;

            if (srcObj.IsStream())
            {
                streamKey   = new PdfSmartCopy.ByteStore((PRStream)srcObj, serialized);
                validStream = true;
                PdfIndirectReference streamRef;
                if (streamMap.TryGetValue(streamKey, out streamRef))
                {
                    return(streamRef);
                }
            }
            else if (srcObj.IsDictionary())
            {
                streamKey   = new PdfSmartCopy.ByteStore((PdfDictionary)srcObj, serialized);
                validStream = true;
                PdfIndirectReference streamRef;
                if (streamMap.TryGetValue(streamKey, out streamRef))
                {
                    return(streamRef);
                }
            }

            PdfIndirectReference theRef;
            RefKey             key = new RefKey(@in);
            IndirectReferences iRef;

            if (indirects.TryGetValue(key, out iRef))
            {
                theRef = iRef.Ref;
                if (iRef.Copied)
                {
                    return(theRef);
                }
            }
            else
            {
                theRef         = body.PdfIndirectReference;
                iRef           = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            if (srcObj.IsDictionary())
            {
                PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)srcObj).Get(PdfName.TYPE));
                if (type != null)
                {
                    if ((PdfName.PAGE.Equals(type)))
                    {
                        return(theRef);
                    }
                    if ((PdfName.CATALOG.Equals(type)))
                    {
                        LOGGER.Warn(MessageLocalization.GetComposedMessage("make.copy.of.catalog.dictionary.is.forbidden"));
                        return(null);
                    }
                }
            }

            iRef.SetCopied();

            if (validStream)
            {
                streamMap[streamKey] = theRef;
            }

            PdfObject obj = CopyObject(srcObj);

            AddToBody(obj, theRef);
            return(theRef);
        }
Пример #21
0
        /**
        * Translate a PRIndirectReference to a PdfIndirectReference
        * In addition, translates the object numbers, and copies the
        * referenced object to the output file if it wasn't available
        * in the cache yet. If it's in the cache, the reference to
        * the already used stream is returned.
        *
        * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
        * file they came from, because each file has its own namespace. The translation
        * we do from their namespace to ours is *at best* heuristic, and guaranteed to
        * fail under some circumstances.
        */
        protected override PdfIndirectReference CopyIndirect(PRIndirectReference inp)
        {
            PdfObject srcObj = PdfReader.GetPdfObjectRelease(inp);
            ByteStore streamKey = null;
            if (srcObj.Type == PdfObject.STREAM) {
                byte[] streamContent = PdfReader.GetStreamBytesRaw((PRStream) srcObj);
                // Only the content is compared, probably the keys should also be compared
                streamKey = new ByteStore(streamContent);
                PdfIndirectReference streamRef = (PdfIndirectReference) streamMap[streamKey];
                if (streamRef != null) {
                    return streamRef;
                }
            }

            PdfIndirectReference theRef;
            RefKey key = new RefKey(inp);
            IndirectReferences iRef = (IndirectReferences) indirects[key];
            if (iRef != null) {
                theRef = iRef.Ref;
                if (iRef.Copied) {
                    return theRef;
                }
            } else {
                theRef = body.PdfIndirectReference;
                iRef = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            iRef.SetCopied();

            if (srcObj.Type == PdfObject.STREAM) {
                streamMap[streamKey] = theRef;
            }

            PdfObject obj = CopyObject(srcObj);
            AddToBody(obj, theRef);
            return theRef;
        }
Пример #22
0
 /**
 * convenience method. Given a reader, set our "globals"
 */
 protected void SetFromReader(PdfReader reader)
 {
     this.reader = reader;
     indirectMap.TryGetValue(reader, out indirects);
     if (indirects == null) {
         indirects = new Dictionary<RefKey,IndirectReferences>();
         indirectMap[reader] = indirects;
         PdfDictionary catalog = reader.Catalog;
         PRIndirectReference refi = null;
         PdfObject o = catalog.Get(PdfName.ACROFORM);
         if (o == null || o.Type != PdfObject.INDIRECT)
             return;
         refi = (PRIndirectReference)o;
         if (acroForm == null) acroForm = body.PdfIndirectReference;
         indirects[new RefKey(refi)] =  new IndirectReferences(acroForm);
     }
 }
Пример #23
0
        /**
        * Copy the acroform for an input document. Note that you can only have one,
        * we make no effort to merge them.
        * @param reader The reader of the input file that is being copied
        * @throws IOException, BadPdfFormatException
        */
        public void CopyAcroForm(PdfReader reader)
        {
            SetFromReader(reader);

            PdfDictionary catalog = reader.Catalog;
            PRIndirectReference hisRef = null;
            PdfObject o = catalog.Get(PdfName.ACROFORM);
            if (o != null && o.Type == PdfObject.INDIRECT)
                hisRef = (PRIndirectReference)o;
            if (hisRef == null) return; // bugfix by John Engla
            RefKey key = new RefKey(hisRef);
            PdfIndirectReference myRef;
            IndirectReferences iRef;
            indirects.TryGetValue(key, out iRef);
            if (iRef != null) {
                acroForm = myRef = iRef.Ref;
            }
            else {
                acroForm = myRef = body.PdfIndirectReference;
                iRef = new IndirectReferences(myRef);
                indirects[key] =  iRef;
            }
            if (! iRef.Copied) {
                iRef.SetCopied();
                PdfDictionary theForm = CopyDictionary((PdfDictionary)PdfReader.GetPdfObject(hisRef));
                AddToBody(theForm, myRef);
            }
        }
Пример #24
0
 /**
 * Add an imported page to our output
 * @param iPage an imported page
 * @throws IOException, BadPdfFormatException
 */
 public void AddPage(PdfImportedPage iPage) {
     int pageNum = SetFromIPage(iPage);
     
     PdfDictionary thePage = reader.GetPageN(pageNum);
     PRIndirectReference origRef = reader.GetPageOrigRef(pageNum);
     reader.ReleasePage(pageNum);
     RefKey key = new RefKey(origRef);
     PdfIndirectReference pageRef;
     IndirectReferences iRef;
     if (indirects.TryGetValue(key, out iRef) && !iRef.Copied)
     {
         pageReferences.Add(iRef.Ref);
         iRef.SetCopied();
     }
     pageRef = CurrentPage;
     if (iRef == null) {
         iRef = new IndirectReferences(pageRef);
         indirects[key] = iRef;
     }
     iRef.SetCopied();
     if (tagged)
         structTreeRootReference = (PRIndirectReference)reader.Catalog.Get(PdfName.STRUCTTREEROOT);
     PdfDictionary newPage = CopyDictionary(thePage);
     root.AddPage(newPage);
     iPage.SetCopied();
     ++currentPageNumber;
     structTreeRootReference = null;
 }
Пример #25
0
        /**
        * Translate a PRIndirectReference to a PdfIndirectReference
        * In addition, translates the object numbers, and copies the
        * referenced object to the output file if it wasn't available
        * in the cache yet. If it's in the cache, the reference to
        * the already used stream is returned.
        *
        * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
        * file they came from, because each file has its own namespace. The translation
        * we do from their namespace to ours is *at best* heuristic, and guaranteed to
        * fail under some circumstances.
        */
        protected override PdfIndirectReference CopyIndirect(PRIndirectReference inp)
        {
            PdfObject srcObj = PdfReader.GetPdfObjectRelease(inp);
            ByteStore streamKey = null;
            bool validStream = false;
            if (srcObj.IsStream()) {
                streamKey = new ByteStore((PRStream)srcObj);
                validStream = true;
                PdfIndirectReference streamRef = (PdfIndirectReference) streamMap[streamKey];
                if (streamRef != null) {
                    return streamRef;
                }
            }

            PdfIndirectReference theRef;
            RefKey key = new RefKey(inp);
            IndirectReferences iRef = (IndirectReferences) indirects[key];
            if (iRef != null) {
                theRef = iRef.Ref;
                if (iRef.Copied) {
                    return theRef;
                }
            } else {
                theRef = body.PdfIndirectReference;
                iRef = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            if (srcObj != null && srcObj.IsDictionary()) {
                PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)srcObj).Get(PdfName.TYPE));
                if (type != null && PdfName.PAGE.Equals(type)) {
                    return theRef;
                }
            }
            iRef.SetCopied();

            if (validStream) {
                streamMap[streamKey] = theRef;
            }

            PdfObject obj = CopyObject(srcObj);
            AddToBody(obj, theRef);
            return theRef;
        }
Пример #26
0
 /**
 * Translate a PRIndirectReference to a PdfIndirectReference
 * In addition, translates the object numbers, and copies the
 * referenced object to the output file.
 * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
 * file they came from, because each file has its own namespace. The translation
 * we do from their namespace to ours is *at best* heuristic, and guaranteed to
 * fail under some circumstances.
 */
 protected virtual PdfIndirectReference CopyIndirect(PRIndirectReference inp)
 {
     PdfIndirectReference theRef;
     RefKey key = new RefKey(inp);
     IndirectReferences iRef;
     indirects.TryGetValue(key, out iRef);
     if (iRef != null) {
         theRef = iRef.Ref;
         if (iRef.Copied) {
             return theRef;
         }
     }
     else {
         theRef = body.PdfIndirectReference;
         iRef = new IndirectReferences(theRef);
         indirects[key] =  iRef;
     }
     PdfObject obj = PdfReader.GetPdfObjectRelease(inp);
     if (obj != null && obj.IsDictionary()) {
         PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)obj).Get(PdfName.TYPE));
         if (type != null && PdfName.PAGE.Equals(type)) {
             return theRef;
         }
     }
     iRef.SetCopied();
     obj = CopyObject(obj);
     AddToBody(obj, theRef);
     return theRef;
 }
Пример #27
0
 /**
 * Translate a PRIndirectReference to a PdfIndirectReference
 * In addition, translates the object numbers, and copies the
 * referenced object to the output file.
 * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
 * file they came from, because each file has its own namespace. The translation
 * we do from their namespace to ours is *at best* heuristic, and guaranteed to
 * fail under some circumstances.
 */
 protected internal virtual PdfIndirectReference CopyIndirect(PRIndirectReference inp, bool keepStructure, bool directRootKids) {
     PdfIndirectReference theRef;
     RefKey key = new RefKey(inp);
     IndirectReferences iref;
     indirects.TryGetValue(key, out iref);
     PdfObject obj = PdfReader.GetPdfObjectRelease(inp);
     if ((keepStructure) && (directRootKids))
         if (obj is PdfDictionary) {
             PdfDictionary dict = (PdfDictionary) obj;
             if (dict.Contains(PdfName.PG))
                 return null;
         }
     if (iref != null) {
         theRef = iref.Ref;
         if (iref.Copied) {
             return theRef;
         }
     }
     else {
         theRef = body.PdfIndirectReference;
         iref = new IndirectReferences(theRef);
         indirects[key] =  iref;
     }
     if (obj != null && obj.IsDictionary()) {
         PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)obj).Get(PdfName.TYPE));
         if (type != null && PdfName.PAGE.Equals(type)) {
             return theRef;
         }
     }
     iref.SetCopied();
     parentObjects[obj] = inp;
     PdfObject res = CopyObject(obj, keepStructure, directRootKids);
     if (disableIndirects.Contains(obj))
         iref.Copied = false;
     if ((res != null) && !(res is PdfNull)) {
         AddToBody(res, theRef);
         return theRef;
     }
     indirects.Remove(key);
     return null;
 }
Пример #28
0
 /**
 * Add an imported page to our output
 * @param iPage an imported page
 * @throws IOException, BadPdfFormatException
 */
 public virtual void AddPage(PdfImportedPage iPage) {
     if (mergeFields && !mergeFieldsInternalCall) {
         throw new ArgumentException(MessageLocalization.GetComposedMessage("1.method.cannot.be.used.in.mergeFields.mode.please.use.addDocument", "addPage"));
     }
     int pageNum = SetFromIPage(iPage);
     
     PdfDictionary thePage = reader.GetPageN(pageNum);
     PRIndirectReference origRef = reader.GetPageOrigRef(pageNum);
     reader.ReleasePage(pageNum);
     RefKey key = new RefKey(origRef);
     PdfIndirectReference pageRef;
     IndirectReferences iRef;
     if (indirects.TryGetValue(key, out iRef) && !iRef.Copied)
     {
         pageReferences.Add(iRef.Ref);
         iRef.SetCopied();
     }
     pageRef = CurrentPage;
     if (iRef == null) {
         iRef = new IndirectReferences(pageRef);
         indirects[key] = iRef;
     }
     iRef.SetCopied();
     if (tagged)
         structTreeRootReference = (PRIndirectReference)reader.Catalog.Get(PdfName.STRUCTTREEROOT);
     PdfDictionary newPage = CopyDictionary(thePage);
     root.AddPage(newPage);
     iPage.SetCopied();
     ++currentPageNumber;
     structTreeRootReference = null;
 }
Пример #29
0
        /**
        * Translate a PRIndirectReference to a PdfIndirectReference
        * In addition, translates the object numbers, and copies the
        * referenced object to the output file if it wasn't available
        * in the cache yet. If it's in the cache, the reference to
        * the already used stream is returned.
        * 
        * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
        * file they came from, because each file has its own namespace. The translation
        * we do from their namespace to ours is *at best* heuristic, and guaranteed to
        * fail under some circumstances.
        */
        protected override PdfIndirectReference CopyIndirect(PRIndirectReference inp) {
            PdfObject srcObj = PdfReader.GetPdfObjectRelease(inp);
            ByteStore streamKey = null;
            bool validStream = false;
            if (srcObj.IsStream()) {
                streamKey = new ByteStore((PRStream)srcObj, serialized);
                validStream = true;
                PdfIndirectReference streamRef;
                if (streamMap.TryGetValue(streamKey, out streamRef)) {
                    return streamRef;
                }
            } else if (srcObj.IsDictionary()) {
                streamKey = new ByteStore((PdfDictionary)srcObj, serialized);
                validStream = true;
                PdfIndirectReference streamRef;
                if (streamMap.TryGetValue(streamKey, out streamRef)) {
                    return streamRef;
                }
            }

            PdfIndirectReference theRef;
            RefKey key = new RefKey(inp);
            IndirectReferences iRef;
            indirects.TryGetValue(key, out iRef);
            if (iRef != null) {
                theRef = iRef.Ref;
                if (iRef.Copied) {
                    return theRef;
                }
            } else {
                theRef = body.PdfIndirectReference;
                iRef = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            if (srcObj.IsDictionary()) {
                PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)srcObj).Get(PdfName.TYPE));
                if (type != null) {
                    if ((PdfName.PAGE.Equals(type))) {
                        return theRef;
                    }
                    if ((PdfName.CATALOG.Equals(type))) {
                        LOGGER.Warn(MessageLocalization.GetComposedMessage("make.copy.of.catalog.dictionary.is.forbidden"));
                        return null;
                    }
                }
            }
            iRef.SetCopied();

            if (validStream) {
                streamMap[streamKey] = theRef;
            }

            PdfObject obj = CopyObject(srcObj);
            AddToBody(obj, theRef);
            return theRef;
        }