Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
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;
                }
            } else if (srcObj.IsDictionary()) {
                streamKey = new ByteStore((PdfDictionary)srcObj);
                validStream = true;
                PdfIndirectReference streamRef = null;
                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;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="StructureValue"/> class.
        /// </summary>
        /// <param name="theBytes">The bytes.</param>
        /// <param name="theStructureDefinition">The structure definition.</param>
        /// <param name="parent">The parent.</param>
        public StructureValue(ByteStore theBytes, StructureDefinition theStructureDefinition, Value parent)
            : base(theStructureDefinition, parent)
        {
            m_StructureDefinition = theStructureDefinition;
            m_AttributeValues = new AttributeValueCollection(m_StructureDefinition.Name); 

            int startPosition = theBytes.ReadPosition;
            foreach (var attribute in m_StructureDefinition.AttributeDefinitions)
            {
                int localOffset = theBytes.ReadPosition - startPosition;

                // Adjust alignment within current structure 
                if (attribute.ByteOffset.HasValue)
                {
                    while (attribute.ByteOffset.Value > localOffset)
                    {
                        theBytes.GetByte();
                        localOffset++;
                    }
                }

                // Error handling - trap decode errors for structure attributes and replace the attribute
                // value with an error value
                try
                {
                    // Decode the value from the binary data
                    Value theValue = attribute.Type.Decode(theBytes, this);

                    // Values decoded from switch cases need to have their name tweaked
                    if (attribute.Type.TypeId == TypeId.SwitchType)
                    {
                        SwitchDefinition switchDefinition = attribute.Type as SwitchDefinition;
                        SwitchCaseDefinition theCase = switchDefinition.GetSwitchCaseDefinition(this);
                        AddAttributeValue(attribute, theCase.Name, theValue);
                    }
                    else
                    {
                        AddAttributeValue(attribute, theValue);
                    }
                }
                catch (DataDictionaryException ex)
                {
                    AddAttributeValue(attribute, new ErrorValue (string.Format ("{0} Error: {1}", InitialType.Name, ex.Message), this));
                    throw new PartialDecodeException(GetTopParent(), ex.Message);
                }
            }
        }
Пример #5
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);
        }
        /// <summary>
        /// Encodes the value into the list of bytes
        /// </summary>
        /// <param name="theBytes"></param>
        public override void Encode(ByteStore theBytes)
        {
            int startPosition = theBytes.WritePosition;
            int localOffset;
            foreach (var item in m_StructureDefinition.AttributeDefinitions)
            {
                // Apply the attribute's byte offset first then encode the attribute
                localOffset = theBytes.WritePosition - startPosition;
                if (item.ByteOffset.HasValue)
                {
                    while (item.ByteOffset.Value > localOffset)
                    {
                        theBytes.PutByte(0);
                        ++localOffset;
                    }                    
                }

                Value theAttributeValue = m_AttributeValues[item.Name].Value;
                theAttributeValue.Encode(theBytes);
            }

            // Add padding bytes to pad to the correct structure length
            localOffset = theBytes.WritePosition - startPosition;
            if (m_StructureDefinition.FixedSizeBytes.HasValue)
            {
                while (localOffset < m_StructureDefinition.FixedSizeBytes.Value)
                {
                    theBytes.PutByte(0);
                    ++localOffset;
                }
            }
        }
Пример #7
0
 /// <summary>
 /// Encodes the value into the list of bytes
 /// </summary>
 /// <param name="theBytes">The bytes.</param>
 public override void Encode(ByteStore theBytes)
 {
     throw new DataDictionaryException("Not implemented");
 }
Пример #8
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;
        }
Пример #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, 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;
        }