IsDictionary() public method

public IsDictionary ( ) : bool
return bool
            private PdfDictionary GetPropertiesDictionary(iTextSharp.text.pdf.PdfObject operand1, ResourceDictionary resources)
            {
                if (operand1.IsDictionary())
                {
                    return((PdfDictionary)operand1);
                }

                PdfName dictionaryName = ((PdfName)operand1);

                return(resources.GetAsDict(dictionaryName));
            }
Example #2
0
        /** Add a chained action.
         * @param na the next action
         */
        virtual public void Next(PdfAction na)
        {
            PdfObject nextAction = Get(PdfName.NEXT);

            if (nextAction == null)
            {
                Put(PdfName.NEXT, na);
            }
            else if (nextAction.IsDictionary())
            {
                PdfArray array = new PdfArray(nextAction);
                array.Add(na);
                Put(PdfName.NEXT, array);
            }
            else
            {
                ((PdfArray)nextAction).Add(na);
            }
        }
Example #3
0
 private void SerObject(PdfObject obj, int level, ByteBuffer bb)
 {
     if (level <= 0)
     {
         return;
     }
     if (obj == null)
     {
         bb.Append("$Lnull");
         return;
     }
     obj = PdfReader.GetPdfObject(obj);
     if (obj.IsStream())
     {
         bb.Append("$B");
         SerDic((PdfDictionary)obj, level - 1, bb);
         if (level > 0)
         {
             md5.Initialize();
             bb.Append(md5.ComputeHash(PdfReader.GetStreamBytesRaw((PRStream)obj)));
         }
     }
     else if (obj.IsDictionary())
     {
         SerDic((PdfDictionary)obj, level - 1, bb);
     }
     else if (obj.IsArray())
     {
         SerArray((PdfArray)obj, level - 1, bb);
     }
     else if (obj.IsString())
     {
         bb.Append("$S").Append(obj.ToString());
     }
     else if (obj.IsName())
     {
         bb.Append("$N").Append(obj.ToString());
     }
     else
     {
         bb.Append("$L").Append(obj.ToString());
     }
 }
        internal void BuildTree()
        {
            Dictionary <int, PdfIndirectReference> numTree = new Dictionary <int, PdfIndirectReference>();

            foreach (int i in parentTree.Keys)
            {
                PdfArray ar = (PdfArray)parentTree[i];
                numTree[i] = writer.AddToBody(ar).IndirectReference;
            }
            PdfDictionary dicTree = PdfNumberTree.WriteTree(numTree, writer);

            if (dicTree != null)
            {
                Put(PdfName.PARENTTREE, writer.AddToBody(dicTree).IndirectReference);
            }
            if (classMap != null)
            {
                foreach (KeyValuePair <PdfName, PdfObject> entry in classes)
                {
                    PdfObject value = entry.Value;
                    if (value.IsDictionary())
                    {
                        classMap.Put(entry.Key, writer.AddToBody(value).IndirectReference);
                    }
                    else if (value.IsArray())
                    {
                        PdfArray newArray = new PdfArray();
                        PdfArray array    = (PdfArray)value;
                        for (int i = 0; i < array.Size; ++i)
                        {
                            if (array[i].IsDictionary())
                            {
                                newArray.Add(writer.AddToBody(array.GetAsDict(i)).IndirectReference);
                            }
                        }
                        classMap.Put(entry.Key, newArray);
                    }
                }
                Put(PdfName.CLASSMAP, writer.AddToBody(classMap).IndirectReference);
            }
            NodeProcess(this, reference);
        }
Example #5
0
 private void SerObject(PdfObject obj, int level, ByteBuffer bb)
 {
     if (level <= 0)
     {
         return;
     }
     if (obj == null)
     {
         bb.Append("$Lnull");
         return;
     }
     obj = PdfReader.GetPdfObject(obj);
     if (obj.IsStream())
     {
         bb.Append("$B");
         SerDic((PdfDictionary)obj, level - 1, bb);
         if (level > 0)
         {
             bb.Append(DigestAlgorithms.Digest("MD5", PdfReader.GetStreamBytesRaw((PRStream)obj)));
         }
     }
     else if (obj.IsDictionary())
     {
         SerDic((PdfDictionary)obj, level - 1, bb);
     }
     else if (obj.IsArray())
     {
         SerArray((PdfArray)obj, level - 1, bb);
     }
     else if (obj.IsString())
     {
         bb.Append("$S").Append(obj.ToString());
     }
     else if (obj.IsName())
     {
         bb.Append("$N").Append(obj.ToString());
     }
     else
     {
         bb.Append("$L").Append(obj.ToString());
     }
 }
Example #6
0
        /**
         * Gets a <CODE>List</CODE> with the bookmarks. It returns <CODE>null</CODE> if
         * the document doesn't have any bookmarks.
         * @param reader the document
         * @return a <CODE>List</CODE> with the bookmarks or <CODE>null</CODE> if the
         * document doesn't have any
         */
        public static ArrayList GetBookmark(PdfReader reader)
        {
            PdfDictionary catalog = reader.Catalog;
            PdfObject     obj     = PdfReader.GetPdfObjectRelease(catalog.Get(PdfName.OUTLINES));

            if (obj == null || !obj.IsDictionary())
            {
                return(null);
            }
            PdfDictionary outlines = (PdfDictionary)obj;
            IntHashtable  pages    = new IntHashtable();
            int           numPages = reader.NumberOfPages;

            for (int k = 1; k <= numPages; ++k)
            {
                pages[reader.GetPageOrigRef(k).Number] = k;
                reader.ReleasePage(k);
            }
            return(BookmarkDepth(reader, (PdfDictionary)PdfReader.GetPdfObjectRelease(outlines.Get(PdfName.FIRST)), pages));
        }
Example #7
0
        internal void BuildTree()
        {
            CreateNumTree();
            PdfDictionary dicTree = PdfNumberTree.WriteTree(numTree, writer);

            if (dicTree != null)
            {
                Put(PdfName.PARENTTREE, writer.AddToBody(dicTree).IndirectReference);
            }
            if (classMap != null && classes.Count > 0)
            {
                foreach (KeyValuePair <PdfName, PdfObject> entry in classes)
                {
                    PdfObject value = entry.Value;
                    if (value.IsDictionary())
                    {
                        classMap.Put(entry.Key, writer.AddToBody(value).IndirectReference);
                    }
                    else if (value.IsArray())
                    {
                        PdfArray newArray = new PdfArray();
                        PdfArray array    = (PdfArray)value;
                        for (int i = 0; i < array.Size; ++i)
                        {
                            if (array.GetPdfObject(i).IsDictionary())
                            {
                                newArray.Add(writer.AddToBody(array.GetAsDict(i)).IndirectReference);
                            }
                        }
                        classMap.Put(entry.Key, newArray);
                    }
                }
                Put(PdfName.CLASSMAP, writer.AddToBody(classMap).IndirectReference);
            }
            if (idTreeMap != null && idTreeMap.Count > 0)
            {
                PdfDictionary dic = PdfNameTree.WriteTree(idTreeMap, writer);
                this.Put(PdfName.IDTREE, dic);
            }
            NodeProcess(this, reference);
        }
Example #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)
        {
            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);
        }
Example #9
0
 public override PdfIndirectObject AddToBody(PdfObject objecta, PdfIndirectReference refa)
 {
     if (tagged && indirectObjects != null && (objecta.IsArray() || objecta.IsDictionary())) {
         RefKey key = new RefKey(refa);
         PdfIndirectObject obj;
         if (!indirectObjects.TryGetValue(key, out obj)) {
             obj = new PdfIndirectObject(refa, objecta, this);
             indirectObjects[key] = obj;
         }
         return obj;
     } else {
         if (tagged && objecta.IsStream())
             streams.Add(new RefKey(refa));
         return base.AddToBody(objecta, refa);
     }
 }
Example #10
0
 private void SerObject(PdfObject obj, int level, ByteBuffer bb)
 {
     if (level <= 0)
         return;
     if (obj == null) {
         bb.Append("$Lnull");
         return;
     }
     obj = PdfReader.GetPdfObject(obj);
     if (obj.IsStream()) {
         bb.Append("$B");
         SerDic((PdfDictionary)obj, level - 1, bb);
         if (level > 0) {
             bb.Append(PdfEncryption.DigestComputeHash("MD5", PdfReader.GetStreamBytesRaw((PRStream)obj)));
         }
     }
     else if (obj.IsDictionary()) {
         SerDic((PdfDictionary)obj, level - 1, bb);
     }
     else if (obj.IsArray()) {
         SerArray((PdfArray)obj, level - 1, bb);
     }
     else if (obj.IsString()) {
         bb.Append("$S").Append(obj.ToString());
     }
     else if (obj.IsName()) {
         bb.Append("$N").Append(obj.ToString());
     }
     else
         bb.Append("$L").Append(obj.ToString());
 }
Example #11
0
            private void SerObject(PdfObject obj, int level, ByteBuffer bb, Dictionary<RefKey, int> serialized)
            {
                if (level <= 0)
                    return;
                if (obj == null) {
                    bb.Append("$Lnull");
                    return;
                }

                PdfIndirectReference refe = null;
                ByteBuffer savedBb = null;

                if (obj.IsIndirect()) {
                    refe = (PdfIndirectReference)obj;
                    RefKey key = new RefKey(refe);
                    if (serialized.ContainsKey(key)) {
                        bb.Append(serialized[key]);
                        return;
                    }
                    else {
                        savedBb = bb;
                        bb = new ByteBuffer();
                    }
                }
                obj = PdfReader.GetPdfObject(obj);
                if (obj.IsStream()) {
                    bb.Append("$B");
                    SerDic((PdfDictionary)obj, level - 1, bb, serialized);
                    if (level > 0) {
                        bb.Append(DigestAlgorithms.Digest("MD5", PdfReader.GetStreamBytesRaw((PRStream)obj)));
                    }
                }
                else if (obj.IsDictionary()) {
                    SerDic((PdfDictionary)obj, level - 1, bb,serialized);
                }
                else if (obj.IsArray()) {
                    SerArray((PdfArray)obj, level - 1, bb,serialized);
                }
                else if (obj.IsString()) {
                    bb.Append("$S").Append(obj.ToString());
                }
                else if (obj.IsName()) {
                    bb.Append("$N").Append(obj.ToString());
                }
                else
                    bb.Append("$L").Append(obj.ToString());

                if (savedBb != null) {
                    RefKey key = new RefKey(refe);
                    if (!serialized.ContainsKey(key))
                        serialized[key] = CalculateHash(bb.Buffer);
                    savedBb.Append(bb);
                }
            }
Example #12
0
 internal void AddClass(PdfObject obj) {
     obj = GetDirectObject(obj);
     if (obj.IsDictionary()) {
         PdfObject curClass = ((PdfDictionary)obj).Get(PdfName.C);
         if (curClass == null)
             return;
         if (curClass.IsArray()) {
             PdfArray array = (PdfArray)curClass;
             for (int i = 0; i < array.Size; ++i) {
                 AddClass(array[i]);
             }
         } else if (curClass.IsName())
             AddClass(curClass);
     } else if (obj.IsName()) {
         PdfName name = (PdfName)obj;
         if (sourceClassMap == null) {
             obj = GetDirectObject(structTreeRoot.Get(PdfName.CLASSMAP));
             if (obj == null || !obj.IsDictionary()) {
                 return;
             }
             sourceClassMap = (PdfDictionary)obj;
         }
         obj = GetDirectObject(sourceClassMap.Get(name));
         if (obj == null) {
             return;
         }
         PdfObject put = structureTreeRoot.GetMappedClass(name);
         if (put != null) {
             if (!CompareObjects(put,obj)) {
                 throw new BadPdfFormatException(MessageLocalization.GetComposedMessage("conflict.in.classmap",name));
             }
         } else {
             if (obj.IsDictionary())
                 structureTreeRoot.MapClass(name, GetDirectDict((PdfDictionary)obj));
             else if (obj.IsArray()) {
                 structureTreeRoot.MapClass(name, GetDirectArray((PdfArray)obj));
             }
         }
     }
 }
Example #13
0
 internal void AddClass(PdfObject obj)
 {
     obj = GetDirectObject(obj);
     if (obj.IsDictionary())
     {
         PdfObject curClass = ((PdfDictionary)obj).Get(PdfName.C);
         if (curClass == null)
         {
             return;
         }
         if (curClass.IsArray())
         {
             PdfArray array = (PdfArray)curClass;
             for (int i = 0; i < array.Size; ++i)
             {
                 AddClass(array[i]);
             }
         }
         else if (curClass.IsName())
         {
             AddClass(curClass);
         }
     }
     else if (obj.IsName())
     {
         PdfName name = (PdfName)obj;
         if (sourceClassMap == null)
         {
             obj = GetDirectObject(structTreeRoot.Get(PdfName.CLASSMAP));
             if (obj == null || !obj.IsDictionary())
             {
                 return;
             }
             sourceClassMap = (PdfDictionary)obj;
         }
         obj = GetDirectObject(sourceClassMap.Get(name));
         if (obj == null)
         {
             return;
         }
         PdfObject put = structureTreeRoot.GetMappedClass(name);
         if (put != null)
         {
             if (!CompareObjects(put, obj))
             {
                 throw new BadPdfFormatException(MessageLocalization.GetComposedMessage("conflict.in.classmap", name));
             }
         }
         else
         {
             if (obj.IsDictionary())
             {
                 structureTreeRoot.MapClass(name, GetDirectDict((PdfDictionary)obj));
             }
             else if (obj.IsArray())
             {
                 structureTreeRoot.MapClass(name, GetDirectArray((PdfArray)obj));
             }
         }
     }
 }
Example #14
0
        private ReturnType FindAndCopyMarks(PdfArray pages, int arrayNumber, int newArrayNumber)
        {
            if (pages.GetAsNumber(0).IntValue > arrayNumber)
            {
                return(ReturnType.BELOW);
            }
            if (pages.GetAsNumber(pages.Size - 2).IntValue < arrayNumber)
            {
                return(ReturnType.ABOVE);
            }
            int cur   = pages.Size / 4;
            int begin = 0;
            int curNumber;

            while (true)
            {
                curNumber = pages.GetAsNumber((begin + cur) * 2).IntValue;
                if (curNumber == arrayNumber)
                {
                    PdfObject obj  = pages[(begin + cur) * 2 + 1];
                    PdfObject obj1 = obj;
                    while (obj.IsIndirect())
                    {
                        obj = PdfReader.GetPdfObjectRelease(obj);
                    }
                    //invalid Nums
                    if (obj.IsArray())
                    {
                        PdfObject firstNotNullKid = null;
                        foreach (PdfObject numObj in (PdfArray)obj)
                        {
                            if (numObj.IsNull())
                            {
                                if (nullReference == null)
                                {
                                    nullReference = writer.AddToBody(new PdfNull()).IndirectReference;
                                }
                                structureTreeRoot.SetPageMark(newArrayNumber, nullReference);
                            }
                            else
                            {
                                PdfObject res = writer.CopyObject(numObj, true, false);
                                if (firstNotNullKid == null)
                                {
                                    firstNotNullKid = res;
                                }
                                structureTreeRoot.SetPageMark(newArrayNumber, (PdfIndirectReference)res);
                            }
                        }
                        AttachStructTreeRootKids(firstNotNullKid);
                    }
                    else if (obj.IsDictionary())
                    {
                        PdfDictionary k = GetKDict((PdfDictionary)obj);
                        if (k == null)
                        {
                            return(ReturnType.NOTFOUND);
                        }
                        PdfObject res = writer.CopyObject(obj1, true, false);
                        structureTreeRoot.SetAnnotationMark(newArrayNumber, (PdfIndirectReference)res);
                    }
                    else
                    {
                        return(ReturnType.NOTFOUND);
                    }
                    return(ReturnType.FOUND);
                }
                if (curNumber < arrayNumber)
                {
                    begin += cur;
                    cur   /= 2;
                    if (cur == 0)
                    {
                        cur = 1;
                    }
                    if (cur + begin == pages.Size)
                    {
                        return(ReturnType.NOTFOUND);
                    }
                    continue;
                }
                if (cur + begin == 0)
                {
                    return(ReturnType.BELOW);
                }
                if (cur == 0)
                {
                    return(ReturnType.NOTFOUND);
                }
                cur /= 2;
            }
        }
Example #15
0
 private PdfObject Propagate(PdfObject obj) {
     if (obj == null) {
         return new PdfNull();
     } 
     if (obj.IsArray()) {
         PdfArray a = (PdfArray)obj;
         for (int i = 0; i < a.Size; i++) {
             a[i] = Propagate(a[i]);
         }
         return a;
     } 
     if (obj.IsDictionary() || obj.IsStream()) {
         PdfDictionary d = (PdfDictionary)obj;
         PdfDictionary newD = new PdfDictionary();
         foreach (PdfName key in d.Keys) {
             newD.Put(key, Propagate(d.Get(key)));
         }
         return newD;
     } 
     if (obj.IsIndirect()) {
         obj = PdfReader.GetPdfObject(obj);
         return AddToBody(Propagate(obj)).IndirectReference;
     } 
     return obj;
 }
Example #16
0
        private void UpdateAnnotationReferences(PdfObject obj) {
            if (obj.IsArray()) {
                PdfArray array = (PdfArray)obj;
                for (int i = 0; i < array.Size; i++) {
                    PdfObject o = array[i];
                    if (o is PdfIndirectReference) {
                        foreach (PdfIndirectObject entry in unmergedSet) {
                            if (entry.IndirectReference.ToString().Equals(o.ToString())) {
                                if (entry.objecti.IsDictionary()) {
                                    PdfNumber annotId = ((PdfDictionary)entry.objecti).GetAsNumber(PdfCopy.annotId);
                                    if (annotId != null) {
                                        PdfIndirectObject merged = null;
                                        if (mergedMap.TryGetValue(annotId.IntValue, out merged)) {
                                            array[i] = merged.IndirectReference;
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        UpdateAnnotationReferences(o);
                    }
                }
            } else if (obj.IsDictionary() || obj.IsStream()) {
                PdfDictionary dictionary = (PdfDictionary)obj;
                PdfDictionary newDictionary = new PdfDictionary();
                foreach (PdfName key in dictionary.Keys) {
                    PdfObject o = dictionary.Get(key);
                    if (o is PdfIndirectReference) {
                        foreach (PdfIndirectObject entry in unmergedSet) {
                            if (entry.IndirectReference.ToString().Equals(o.ToString())) {
                                if (entry.objecti.IsDictionary()) {
                                    PdfNumber annotId = ((PdfDictionary)entry.objecti).GetAsNumber(PdfCopy.annotId);
                                    if (annotId != null) {
                                        PdfIndirectObject merged;
                                        if (mergedMap.TryGetValue(annotId.IntValue, out merged)) {
                                            newDictionary.Put(key, merged.IndirectReference);
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        UpdateAnnotationReferences(o);
                    }
                }
                foreach (PdfName key in newDictionary.Keys) {
                    dictionary.Put(key, newDictionary.Get(key));
                }

            }
        }
Example #17
0
 private void SerObject(PdfObject obj, int level, ByteBuffer bb) {
     if (level <= 0)
         return;
     if (obj == null) {
         bb.Append("$Lnull");
         return;
     }
     if (obj.IsIndirect()) {
         if (serialized.Contains(obj))
             return;
         else
             serialized.Add(obj);
     }
     obj = PdfReader.GetPdfObject(obj);
     if (obj.IsStream()) {
         bb.Append("$B");
         SerDic((PdfDictionary)obj, level - 1, bb);
         if (level > 0) {
             bb.Append(DigestAlgorithms.Digest("MD5", PdfReader.GetStreamBytesRaw((PRStream)obj)));
         }
     }
     else if (obj.IsDictionary()) {
         SerDic((PdfDictionary)obj, level - 1, bb);
     }
     else if (obj.IsArray()) {
         SerArray((PdfArray)obj, level - 1, bb);
     }
     else if (obj.IsString()) {
         bb.Append("$S").Append(obj.ToString());
     }
     else if (obj.IsName()) {
         bb.Append("$N").Append(obj.ToString());
     }
     else
         bb.Append("$L").Append(obj.ToString());
 }
Example #18
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 = 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);
        }
Example #19
0
            private void SerObject(PdfObject obj, int level, ByteBuffer bb, Dictionary <RefKey, int> serialized)
            {
                if (level <= 0)
                {
                    return;
                }
                if (obj == null)
                {
                    bb.Append("$Lnull");
                    return;
                }

                PdfIndirectReference refe    = null;
                ByteBuffer           savedBb = null;

                if (obj.IsIndirect())
                {
                    refe = (PdfIndirectReference)obj;
                    RefKey key = new RefKey(refe);
                    if (serialized.ContainsKey(key))
                    {
                        bb.Append(serialized[key]);
                        return;
                    }
                    else
                    {
                        savedBb = bb;
                        bb      = new ByteBuffer();
                    }
                }
                obj = PdfReader.GetPdfObject(obj);
                if (obj.IsStream())
                {
                    bb.Append("$B");
                    SerDic((PdfDictionary)obj, level - 1, bb, serialized);
                    if (level > 0)
                    {
                        bb.Append(DigestAlgorithms.Digest("MD5", PdfReader.GetStreamBytesRaw((PRStream)obj)));
                    }
                }
                else if (obj.IsDictionary())
                {
                    SerDic((PdfDictionary)obj, level - 1, bb, serialized);
                }
                else if (obj.IsArray())
                {
                    SerArray((PdfArray)obj, level - 1, bb, serialized);
                }
                else if (obj.IsString())
                {
                    bb.Append("$S").Append(obj.ToString());
                }
                else if (obj.IsName())
                {
                    bb.Append("$N").Append(obj.ToString());
                }
                else
                {
                    bb.Append("$L").Append(obj.ToString());
                }

                if (savedBb != null)
                {
                    RefKey key = new RefKey(refe);
                    if (!serialized.ContainsKey(key))
                    {
                        serialized[key] = CalculateHash(bb.Buffer);
                    }
                    savedBb.Append(bb);
                }
            }
Example #20
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();

            obj = CopyObject(obj);
            parentObjects.Add(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);
            }
            else
            {
                indirects.Remove(key);
                return(null);
            }
        }
Example #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;
            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);
        }
Example #22
0
 private bool ReplaceNamedDestination(PdfObject obj, Dictionary<Object, PdfObject> names) {
     obj = GetPdfObject(obj);
     int objIdx = lastXrefPartial;
     ReleaseLastXrefPartial();
     if (obj != null && obj.IsDictionary()) {
         PdfObject ob2 = GetPdfObjectRelease(((PdfDictionary)obj).Get(PdfName.DEST));
         Object name = null;
         if (ob2 != null) {
             if (ob2.IsName())
                 name = ob2;
             else if (ob2.IsString())
                 name = ob2.ToString();
             if (name != null) {
                 PdfArray dest = null;
                 if (names.ContainsKey(name) && names[name] is PdfArray)
                     dest = (PdfArray)names[name];
                 if (dest != null) {
                     ((PdfDictionary)obj).Put(PdfName.DEST, dest);
                     SetXrefPartialObject(objIdx, obj);
                     return true;
                 }
             }
         }
         else if ((ob2 = GetPdfObject(((PdfDictionary)obj).Get(PdfName.A))) != null) {
             int obj2Idx = lastXrefPartial;
             ReleaseLastXrefPartial();
             PdfDictionary dic = (PdfDictionary)ob2;
             PdfName type = (PdfName)GetPdfObjectRelease(dic.Get(PdfName.S));
             if (PdfName.GOTO.Equals(type)) {
                 PdfObject ob3 = GetPdfObjectRelease(dic.Get(PdfName.D));
                 if (ob3 != null) {
                     if (ob3.IsName())
                         name = ob3;
                     else if (ob3.IsString())
                         name = ob3.ToString();
                 }
                 if (name != null) {
                     PdfArray dest = null;
                     if (names.ContainsKey(name) && names[name] is PdfArray)
                         dest = (PdfArray)names[name];
                     if (dest != null) {
                         dic.Put(PdfName.D, dest);
                         SetXrefPartialObject(obj2Idx, ob2);
                         SetXrefPartialObject(objIdx, obj);
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
Example #23
0
 private PdfObject Propagate(PdfObject obj) {
     if (obj == null) {
         return new PdfNull();
     } else if (obj.IsArray()) {
         PdfArray a = (PdfArray)obj;
         for (int i = 0; i < a.Size; i++) {
             a.Set(i, Propagate(a.GetPdfObject(i)));
         }
         return a;
     } else if (obj.IsDictionary() || obj.IsStream()) {
         PdfDictionary d = (PdfDictionary)obj;
         List<PdfName> keys = new List<PdfName>(d.Keys);
         foreach (PdfName key in keys) {
             d.Put(key, Propagate(d.Get(key)));
         }
         return d;
     } else if (obj.IsIndirect()) {
         obj = PdfReader.GetPdfObject(obj);
         return AddToBody(Propagate(obj)).IndirectReference;
     } else
         return obj;
 }
Example #24
0
 private void UpdateReferences(PdfObject obj) {
     if (obj.IsDictionary() || obj.IsStream()) {
         PdfDictionary dictionary = (PdfDictionary)obj;
         PdfDictionary newDictionary = new PdfDictionary();
         foreach (PdfName key in dictionary.Keys) {
             PdfObject o = dictionary.Get(key);
             if (o.IsIndirect()) {
                 PdfReader reader = ((PRIndirectReference)o).Reader;
                 Dictionary<RefKey,IndirectReferences> indirects = indirectMap[reader];
                 IndirectReferences indRef = null;
                 if (indirects.TryGetValue(new RefKey((PRIndirectReference)o), out indRef)) {
                     newDictionary.Put(key, indRef.Ref);
                 }
             } else {
                 UpdateReferences(o);
             }
         }
         foreach (PdfName key in newDictionary.Keys) {
             dictionary.Put(key, newDictionary.Get(key));
         }
     } else if (obj.IsArray()) {
         PdfArray array = (PdfArray)obj;
         for (int i = 0; i < array.Size; i++) {
             PdfObject o = array[i];
             if (o.IsIndirect()) {
                 PdfReader reader = ((PRIndirectReference)o).Reader;
                 Dictionary<RefKey,IndirectReferences> indirects = indirectMap[reader];
                 IndirectReferences indRef = null;
                 if (indirects.TryGetValue(new RefKey((PRIndirectReference)o), out indRef)) {
                     array[i] = indRef.Ref;
                 }
             } else {
                 UpdateReferences(o);
             }
         }
     }
 }
Example #25
0
        private void DoType1TT()
        {
            CMapToUnicode toUnicode = null;
            PdfObject     enc       = PdfReader.GetPdfObject(font.Get(PdfName.ENCODING));

            if (enc == null)
            {
                PdfName baseFont = font.GetAsName(PdfName.BASEFONT);
                if (BuiltinFonts14.ContainsKey(fontName) &&
                    (PdfName.SYMBOL.Equals(baseFont) || PdfName.ZAPFDINGBATS.Equals(baseFont)))
                {
                    FillEncoding(baseFont);
                }
                else
                {
                    FillEncoding(null);
                }
                toUnicode = ProcessToUnicode();
                if (toUnicode != null)
                {
                    IDictionary <int, int> rm = toUnicode.CreateReverseMapping();
                    foreach (KeyValuePair <int, int> kv in rm)
                    {
                        uni2byte[kv.Key]   = kv.Value;
                        byte2uni[kv.Value] = kv.Key;
                    }
                }
            }
            else
            {
                if (enc.IsName())
                {
                    FillEncoding((PdfName)enc);
                }
                else if (enc.IsDictionary())
                {
                    PdfDictionary encDic = (PdfDictionary)enc;
                    enc = PdfReader.GetPdfObject(encDic.Get(PdfName.BASEENCODING));
                    if (enc == null)
                    {
                        FillEncoding(null);
                    }
                    else
                    {
                        FillEncoding((PdfName)enc);
                    }
                    FillDiffMap(encDic, toUnicode);
                }
            }
            if (BuiltinFonts14.ContainsKey(fontName))
            {
                BaseFont bf = BaseFont.CreateFont(fontName, WINANSI, false);
                int[]    e  = uni2byte.ToOrderedKeys();
                for (int k = 0; k < e.Length; ++k)
                {
                    int n = uni2byte[e[k]];
                    widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k]));
                }
                if (diffmap != null)
                {
                    //widths for differences must override existing ones
                    e = diffmap.ToOrderedKeys();
                    for (int k = 0; k < e.Length; ++k)
                    {
                        int n = diffmap[e[k]];
                        widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k]));
                    }
                    diffmap = null;
                }
                Ascender    = bf.GetFontDescriptor(ASCENT, 1000);
                CapHeight   = bf.GetFontDescriptor(CAPHEIGHT, 1000);
                Descender   = bf.GetFontDescriptor(DESCENT, 1000);
                ItalicAngle = bf.GetFontDescriptor(ITALICANGLE, 1000);
                fontWeight  = bf.GetFontDescriptor(FONT_WEIGHT, 1000);
                llx         = bf.GetFontDescriptor(BBOXLLX, 1000);
                lly         = bf.GetFontDescriptor(BBOXLLY, 1000);
                urx         = bf.GetFontDescriptor(BBOXURX, 1000);
                ury         = bf.GetFontDescriptor(BBOXURY, 1000);
            }
            FillWidths();
            FillFontDesc(font.GetAsDict(PdfName.FONTDESCRIPTOR));
        }
Example #26
0
 public new PdfIndirectObject AddToBody(PdfObject objecta, PdfIndirectReference refa, bool formBranching) {
     if (formBranching) {
         UpdateReferences(objecta);
     }
     PdfIndirectObject indObj;
     if ((tagged || mergeFields) && indirectObjects != null && (objecta.IsArray() || objecta.IsDictionary() || objecta.IsStream())) {
         RefKey key = new RefKey(refa);
         PdfIndirectObject obj;
         if (!indirectObjects.TryGetValue(key, out obj)) {
             obj = new PdfIndirectObject(refa, objecta, this);
             indirectObjects[key] = obj;
         }
         indObj =  obj;
     } else {
         indObj = base.AddToBody(objecta, refa);
     }
     if (mergeFields && objecta.IsDictionary()) {
         PdfNumber annotId = ((PdfDictionary)objecta).GetAsNumber(PdfCopy.annotId);
         if (annotId != null) {
             if (formBranching) {
                 mergedMap[annotId.IntValue] = indObj;
                 mergedSet.Add(indObj);
             } else {
                 unmergedMap[annotId.IntValue] = indObj;
                 unmergedSet.Add(indObj);
             }
         }
     }
     return indObj;
 }
Example #27
0
        public static bool CompareObjects(PdfObject value1, PdfObject value2)
        {
            value2 = GetDirectObject(value2);
            if (value2 == null)
            {
                return(false);
            }
            if (value1.Type != value2.Type)
            {
                return(false);
            }

            if (value1.IsBoolean())
            {
                if (value1 == value2)
                {
                    return(true);
                }
                if (value2 is PdfBoolean)
                {
                    return(((PdfBoolean)value1).BooleanValue == ((PdfBoolean)value2).BooleanValue);
                }
                return(false);
            }
            else if (value1.IsName())
            {
                return(value1.Equals(value2));
            }
            else if (value1.IsNumber())
            {
                if (value1 == value2)
                {
                    return(true);
                }
                if (value2 is PdfNumber)
                {
                    return(((PdfNumber)value1).DoubleValue == ((PdfNumber)value2).DoubleValue);
                }
                return(false);
            }
            else if (value1.IsNull())
            {
                if (value1 == value2)
                {
                    return(true);
                }
                if (value2 is PdfNull)
                {
                    return(true);
                }
                return(false);
            }
            else if (value1.IsString())
            {
                if (value1 == value2)
                {
                    return(true);
                }
                if (value2 is PdfString)
                {
                    return((value2 == null && value1.ToString() == null) ||
                           value1.ToString() == value2.ToString());
                }
                return(false);
            }
            if (value1.IsArray())
            {
                PdfArray array1 = (PdfArray)value1;
                PdfArray array2 = (PdfArray)value2;
                if (array1.Size != array2.Size)
                {
                    return(false);
                }
                for (int i = 0; i < array1.Size; ++i)
                {
                    if (!CompareObjects(array1[i], array2[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            if (value1.IsDictionary())
            {
                PdfDictionary first  = (PdfDictionary)value1;
                PdfDictionary second = (PdfDictionary)value2;
                if (first.Size != second.Size)
                {
                    return(false);
                }
                foreach (PdfName name in first.hashMap.Keys)
                {
                    if (!CompareObjects(first.Get(name), second.Get(name)))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            return(false);
        }
Example #28
0
        private static ArrayList BookmarkDepth(PdfReader reader, PdfDictionary outline, IntHashtable pages)
        {
            ArrayList list = new ArrayList();

            while (outline != null)
            {
                Hashtable map   = new Hashtable();
                PdfString title = (PdfString)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.TITLE));
                map["Title"] = title.ToUnicodeString();
                PdfArray color = (PdfArray)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.C));
                if (color != null && color.ArrayList.Count == 3)
                {
                    ByteBuffer outp = new ByteBuffer();
                    ArrayList  arr  = color.ArrayList;
                    outp.Append(((PdfNumber)arr[0]).FloatValue).Append(' ');
                    outp.Append(((PdfNumber)arr[1]).FloatValue).Append(' ');
                    outp.Append(((PdfNumber)arr[2]).FloatValue);
                    map["Color"] = PdfEncodings.ConvertToString(outp.ToByteArray(), null);
                }
                PdfNumber style = (PdfNumber)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.F));
                if (style != null)
                {
                    int    f = style.IntValue;
                    String s = "";
                    if ((f & 1) != 0)
                    {
                        s += "italic ";
                    }
                    if ((f & 2) != 0)
                    {
                        s += "bold ";
                    }
                    s = s.Trim();
                    if (s.Length != 0)
                    {
                        map["Style"] = s;
                    }
                }
                PdfNumber count = (PdfNumber)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.COUNT));
                if (count != null && count.IntValue < 0)
                {
                    map["Open"] = "false";
                }
                try {
                    PdfObject dest = PdfReader.GetPdfObjectRelease(outline.Get(PdfName.DEST));
                    if (dest != null)
                    {
                        MapGotoBookmark(map, dest, pages); //changed by ujihara 2004-06-13
                    }
                    else
                    {
                        PdfDictionary action = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.A));
                        if (action != null)
                        {
                            if (PdfName.GOTO.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                dest = PdfReader.GetPdfObjectRelease(action.Get(PdfName.D));
                                if (dest != null)
                                {
                                    MapGotoBookmark(map, dest, pages);
                                }
                            }
                            else if (PdfName.URI.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                map["Action"] = "URI";
                                map["URI"]    = ((PdfString)PdfReader.GetPdfObjectRelease(action.Get(PdfName.URI))).ToUnicodeString();
                            }
                            else if (PdfName.GOTOR.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                dest = PdfReader.GetPdfObjectRelease(action.Get(PdfName.D));
                                if (dest != null)
                                {
                                    if (dest.IsString())
                                    {
                                        map["Named"] = dest.ToString();
                                    }
                                    else if (dest.IsName())
                                    {
                                        map["NamedN"] = PdfName.DecodeName(dest.ToString());
                                    }
                                    else if (dest.IsArray())
                                    {
                                        ArrayList     arr = ((PdfArray)dest).ArrayList;
                                        StringBuilder s   = new StringBuilder();
                                        s.Append(arr[0].ToString());
                                        s.Append(' ').Append(arr[1].ToString());
                                        for (int k = 2; k < arr.Count; ++k)
                                        {
                                            s.Append(' ').Append(arr[k].ToString());
                                        }
                                        map["Page"] = s.ToString();
                                    }
                                }
                                map["Action"] = "GoToR";
                                PdfObject file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.F));
                                if (file != null)
                                {
                                    if (file.IsString())
                                    {
                                        map["File"] = ((PdfString)file).ToUnicodeString();
                                    }
                                    else if (file.IsDictionary())
                                    {
                                        file = PdfReader.GetPdfObject(((PdfDictionary)file).Get(PdfName.F));
                                        if (file.IsString())
                                        {
                                            map["File"] = ((PdfString)file).ToUnicodeString();
                                        }
                                    }
                                }
                                PdfObject newWindow = PdfReader.GetPdfObjectRelease(action.Get(PdfName.NEWWINDOW));
                                if (newWindow != null)
                                {
                                    map["NewWindow"] = newWindow.ToString();
                                }
                            }
                            else if (PdfName.LAUNCH.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                map["Action"] = "Launch";
                                PdfObject file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.F));
                                if (file == null)
                                {
                                    file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.WIN));
                                }
                                if (file != null)
                                {
                                    if (file.IsString())
                                    {
                                        map["File"] = ((PdfString)file).ToUnicodeString();
                                    }
                                    else if (file.IsDictionary())
                                    {
                                        file = PdfReader.GetPdfObjectRelease(((PdfDictionary)file).Get(PdfName.F));
                                        if (file.IsString())
                                        {
                                            map["File"] = ((PdfString)file).ToUnicodeString();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch  {
                    //empty on purpose
                }
                PdfDictionary first = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.FIRST));
                if (first != null)
                {
                    map["Kids"] = BookmarkDepth(reader, first, pages);
                }
                list.Add(map);
                outline = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.NEXT));
            }
            return(list);
        }
Example #29
0
        public static bool CompareObjects(PdfObject value1, PdfObject value2) {
            value2 = GetDirectObject(value2);
            if (value2 == null)
                return false;
            if (value1.Type != value2.Type)
                return false;

            if (value1.IsBoolean()){
                if (value1 == value2)
                    return true;
                if (value2 is PdfBoolean) {
                    return ((PdfBoolean)value1).BooleanValue == ((PdfBoolean)value2).BooleanValue;
                }
                return false;
            } else if (value1.IsName()) {
                return value1.Equals(value2);
            } else if (value1.IsNumber()){
                if (value1 == value2)
                    return true;
                if (value2 is PdfNumber) {
                    return ((PdfNumber)value1).DoubleValue == ((PdfNumber)value2).DoubleValue;
                }
                return false;
            } else if (value1.IsNull()){
                if (value1 == value2)
                    return true;
                if (value2 is PdfNull)
                    return true;
                return false;
            } else if (value1.IsString()){
                if (value1 == value2)
                    return true;
                if (value2 is PdfString) {
                    return ((value2 == null && value1.ToString() == null)
                        || value1.ToString() == value2.ToString());
                }
                return false;
            }
            if (value1.IsArray()) {
                PdfArray array1 = (PdfArray)value1;
                PdfArray array2 = (PdfArray)value2;
                if (array1.Size != array2.Size)
                    return false;
                for (int i = 0; i < array1.Size; ++i)
                    if (!CompareObjects(array1[i],array2[i]))
                        return false;
                return true;
            }
            if (value1.IsDictionary()) {
                PdfDictionary first = (PdfDictionary)value1;
                PdfDictionary second = (PdfDictionary)value2;
                if (first.Size != second.Size)
                    return false;
                foreach (PdfName name in first.hashMap.Keys) {
                    if (!CompareObjects(first.Get(name),second.Get(name)))
                        return false;
                }
                return true;
            }
            return false;
        }
Example #30
0
 private void UpdateAnnotationReferences(PdfObject obj) {
     if (obj.IsArray()) {
         PdfArray array = (PdfArray)obj;
         for (int i = 0; i < array.Size; i++) {
             PdfObject o = array.GetPdfObject(i);
             if (o != null && o.Type == 0) {
                 PdfIndirectObject entry;
                 bool contains = unmergedIndirectRefsMap.TryGetValue(new RefKey((PdfIndirectReference) o), out entry);
                 if (contains) {
                     if (entry.objecti.IsDictionary()) {
                         PdfNumber annotId = ((PdfDictionary) entry.objecti).GetAsNumber(PdfCopy.annotId);
                         if (annotId != null) {
                             PdfIndirectObject merged;
                             if (mergedMap.TryGetValue(annotId.IntValue, out merged)) {
                                 array.Set(i, merged.IndirectReference);
                             }
                         }
                     }
                 }
             } else {
                 UpdateAnnotationReferences(o);
             }
         }
     } else if (obj.IsDictionary() || obj.IsStream()) {
         PdfDictionary dictionary = (PdfDictionary)obj;
         List<PdfName> keys = new List<PdfName>(dictionary.Keys);
         foreach (PdfName key in keys) {
             PdfObject o = dictionary.Get(key);
             if (o != null && o.Type == 0) {
                 PdfIndirectObject entry;
                 bool contains = unmergedIndirectRefsMap.TryGetValue(new RefKey((PdfIndirectReference) o), out entry);
                 if (contains) {
                     if (entry.objecti.IsDictionary()) {
                         PdfNumber annotId = ((PdfDictionary) entry.objecti).GetAsNumber(PdfCopy.annotId);
                         if (annotId != null) {
                             PdfIndirectObject merged;
                             if (mergedMap.TryGetValue(annotId.IntValue, out merged)) {
                                 dictionary.Put(key, merged.IndirectReference);
                             }
                         }
                     }
                 }
             } else {
                 UpdateAnnotationReferences(o);
             }
         }
     }
 }
Example #31
0
        private void DoType1TT()
        {
            CMapToUnicode toUnicode = null;
            PdfObject     enc       = PdfReader.GetPdfObject(font.Get(PdfName.ENCODING));

            if (enc == null)
            {
                PdfName baseFont = font.GetAsName(PdfName.BASEFONT);
                if (BuiltinFonts14.ContainsKey(fontName) &&
                    (PdfName.SYMBOL.Equals(baseFont) || PdfName.ZAPFDINGBATS.Equals(baseFont)))
                {
                    FillEncoding(baseFont);
                }
                else
                {
                    FillEncoding(null);
                }
                toUnicode = ProcessToUnicode();
                if (toUnicode != null)
                {
                    IDictionary <int, int> rm = toUnicode.CreateReverseMapping();
                    foreach (KeyValuePair <int, int> kv in rm)
                    {
                        uni2byte[kv.Key]   = kv.Value;
                        byte2uni[kv.Value] = kv.Key;
                    }
                }
            }
            else
            {
                if (enc.IsName())
                {
                    FillEncoding((PdfName)enc);
                }
                else if (enc.IsDictionary())
                {
                    PdfDictionary encDic = (PdfDictionary)enc;
                    enc = PdfReader.GetPdfObject(encDic.Get(PdfName.BASEENCODING));
                    if (enc == null)
                    {
                        FillEncoding(null);
                    }
                    else
                    {
                        FillEncoding((PdfName)enc);
                    }
                    PdfArray diffs = encDic.GetAsArray(PdfName.DIFFERENCES);
                    if (diffs != null)
                    {
                        diffmap = new IntHashtable();
                        int currentNumber = 0;
                        for (int k = 0; k < diffs.Size; ++k)
                        {
                            PdfObject obj = diffs[k];
                            if (obj.IsNumber())
                            {
                                currentNumber = ((PdfNumber)obj).IntValue;
                            }
                            else
                            {
                                int[] c = GlyphList.NameToUnicode(PdfName.DecodeName(((PdfName)obj).ToString()));
                                if (c != null && c.Length > 0)
                                {
                                    uni2byte[c[0]]          = currentNumber;
                                    byte2uni[currentNumber] = c[0];
                                    diffmap[c[0]]           = currentNumber;
                                }
                                else
                                {
                                    if (toUnicode == null)
                                    {
                                        toUnicode = ProcessToUnicode();
                                        if (toUnicode == null)
                                        {
                                            toUnicode = new CMapToUnicode();
                                        }
                                    }
                                    string unicode = toUnicode.Lookup(new byte[] { (byte)currentNumber }, 0, 1);
                                    if ((unicode != null) && (unicode.Length == 1))
                                    {
                                        this.uni2byte[unicode[0]]    = currentNumber;
                                        this.byte2uni[currentNumber] = unicode[0];
                                        this.diffmap[unicode[0]]     = currentNumber;
                                    }
                                }
                                ++currentNumber;
                            }
                        }
                    }
                }
            }
            PdfArray  newWidths = font.GetAsArray(PdfName.WIDTHS);
            PdfNumber first     = font.GetAsNumber(PdfName.FIRSTCHAR);
            PdfNumber last      = font.GetAsNumber(PdfName.LASTCHAR);

            if (BuiltinFonts14.ContainsKey(fontName))
            {
                BaseFont bf = BaseFont.CreateFont(fontName, WINANSI, false);
                int[]    e  = uni2byte.ToOrderedKeys();
                for (int k = 0; k < e.Length; ++k)
                {
                    int n = uni2byte[e[k]];
                    widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k]));
                }
                if (diffmap != null)   //widths for differences must override existing ones
                {
                    e = diffmap.ToOrderedKeys();
                    for (int k = 0; k < e.Length; ++k)
                    {
                        int n = diffmap[e[k]];
                        widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k]));
                    }
                    diffmap = null;
                }
                Ascender    = bf.GetFontDescriptor(ASCENT, 1000);
                CapHeight   = bf.GetFontDescriptor(CAPHEIGHT, 1000);
                Descender   = bf.GetFontDescriptor(DESCENT, 1000);
                ItalicAngle = bf.GetFontDescriptor(ITALICANGLE, 1000);
                fontWeight  = bf.GetFontDescriptor(FONT_WEIGHT, 1000);
                llx         = bf.GetFontDescriptor(BBOXLLX, 1000);
                lly         = bf.GetFontDescriptor(BBOXLLY, 1000);
                urx         = bf.GetFontDescriptor(BBOXURX, 1000);
                ury         = bf.GetFontDescriptor(BBOXURY, 1000);
            }
            if (first != null && last != null && newWidths != null)
            {
                int f     = first.IntValue;
                int nSize = f + newWidths.Size;
                if (widths.Length < nSize)
                {
                    int[] tmp = new int[nSize];
                    System.Array.Copy(widths, 0, tmp, 0, f);
                    widths = tmp;
                }
                for (int k = 0; k < newWidths.Size; ++k)
                {
                    widths[f + k] = newWidths.GetAsNumber(k).IntValue;
                }
            }
            FillFontDesc(font.GetAsDict(PdfName.FONTDESCRIPTOR));
        }
Example #32
0
        /**
        * @param in
        * @param dicPar
        * @return a byte array
        */
        public static byte[] DecodePredictor(byte[] inp, PdfObject dicPar) {
            if (dicPar == null || !dicPar.IsDictionary())
                return inp;
            PdfDictionary dic = (PdfDictionary)dicPar;
            PdfObject obj = GetPdfObject(dic.Get(PdfName.PREDICTOR));
            if (obj == null || !obj.IsNumber())
                return inp;
            int predictor = ((PdfNumber)obj).IntValue;
            if (predictor < 10 && predictor != 2)
                return inp;
            int width = 1;
            obj = GetPdfObject(dic.Get(PdfName.COLUMNS));
            if (obj != null && obj.IsNumber())
                width = ((PdfNumber)obj).IntValue;
            int colors = 1;
            obj = GetPdfObject(dic.Get(PdfName.COLORS));
            if (obj != null && obj.IsNumber())
                colors = ((PdfNumber)obj).IntValue;
            int bpc = 8;
            obj = GetPdfObject(dic.Get(PdfName.BITSPERCOMPONENT));
            if (obj != null && obj.IsNumber())
                bpc = ((PdfNumber)obj).IntValue;
            MemoryStream dataStream = new MemoryStream(inp);
            MemoryStream fout = new MemoryStream(inp.Length);
            int bytesPerPixel = colors * bpc / 8;
            int bytesPerRow = (colors*width*bpc + 7)/8;
            byte[] curr = new byte[bytesPerRow];
            byte[] prior = new byte[bytesPerRow];

            if (predictor == 2) {
                if (bpc == 8) {
                    int numRows = inp.Length/bytesPerRow;
                    for (int row = 0; row < numRows; row++) {
                        int rowStart = row*bytesPerRow;
                        for (int col = 0 + bytesPerPixel; col < bytesPerRow; col++) {
                            inp[rowStart + col] = (byte) (inp[rowStart + col] + inp[rowStart + col - bytesPerPixel]);
                        }
                    }
                }
                return inp;
            }

            // Decode the (sub)image row-by-row
            while (true) {
                // Read the filter type byte and a row of data
                int filter = 0;
                try {
                    filter = dataStream.ReadByte();
                    if (filter < 0) {
                        return fout.ToArray();
                    }
                    int tot = 0;
                    while (tot < bytesPerRow) {
                        int n = dataStream.Read(curr, tot, bytesPerRow - tot);
                        if (n <= 0)
                            return fout.ToArray();
                        tot += n;
                    }
                } catch {
                    return fout.ToArray();
                }
                
                switch (filter) {
                    case 0: //PNG_FILTER_NONE
                        break;
                    case 1: //PNG_FILTER_SUB
                        for (int i = bytesPerPixel; i < bytesPerRow; i++) {
                            curr[i] += curr[i - bytesPerPixel];
                        }
                        break;
                    case 2: //PNG_FILTER_UP
                        for (int i = 0; i < bytesPerRow; i++) {
                            curr[i] += prior[i];
                        }
                        break;
                    case 3: //PNG_FILTER_AVERAGE
                        for (int i = 0; i < bytesPerPixel; i++) {
                            curr[i] += (byte)(prior[i] / 2);
                        }
                        for (int i = bytesPerPixel; i < bytesPerRow; i++) {
                            curr[i] += (byte)(((curr[i - bytesPerPixel] & 0xff) + (prior[i] & 0xff))/2);
                        }
                        break;
                    case 4: //PNG_FILTER_PAETH
                        for (int i = 0; i < bytesPerPixel; i++) {
                            curr[i] += prior[i];
                        }

                        for (int i = bytesPerPixel; i < bytesPerRow; i++) {
                            int a = curr[i - bytesPerPixel] & 0xff;
                            int b = prior[i] & 0xff;
                            int c = prior[i - bytesPerPixel] & 0xff;

                            int p = a + b - c;
                            int pa = Math.Abs(p - a);
                            int pb = Math.Abs(p - b);
                            int pc = Math.Abs(p - c);

                            int ret;

                            if ((pa <= pb) && (pa <= pc)) {
                                ret = a;
                            } else if (pb <= pc) {
                                ret = b;
                            } else {
                                ret = c;
                            }
                            curr[i] += (byte)(ret);
                        }
                        break;
                    default:
                        // Error -- uknown filter type
                        throw new Exception(MessageLocalization.GetComposedMessage("png.filter.unknown"));
                }
                fout.Write(curr, 0, curr.Length);
                
                // Swap curr and prior
                byte[] tmp = prior;
                prior = curr;
                curr = tmp;
            }        
        }
Example #33
0
 private static PdfArray GetNameArray(PdfObject obj) {
     if (obj == null)
         return null;
     obj = GetPdfObjectRelease(obj);
     if (obj == null)
         return null;
     if (obj.IsArray())
         return (PdfArray)obj;
     else if (obj.IsDictionary()) {
         PdfObject arr2 = GetPdfObjectRelease(((PdfDictionary)obj).Get(PdfName.D));
         if (arr2 != null && arr2.IsArray())
             return (PdfArray)arr2;
     }
     return null;
 }
Example #34
0
 /**
 * Converts a remote named destination GoToR with a local named destination
 * if there's a corresponding name.
 * @param   obj an annotation that needs to be screened for links to external named destinations.
 * @param   names   a map with names of local named destinations
 * @since   iText 5.0
 */
 private bool ConvertNamedDestination(PdfObject obj, Dictionary<Object, PdfObject> names) {
     obj = GetPdfObject(obj);
     int objIdx = lastXrefPartial;
     ReleaseLastXrefPartial();
     if (obj != null && obj.IsDictionary()) {
         PdfObject ob2 = GetPdfObject(((PdfDictionary)obj).Get(PdfName.A));
         if (ob2 != null) {
             int obj2Idx = lastXrefPartial;
             ReleaseLastXrefPartial();
             PdfDictionary dic = (PdfDictionary)ob2;
             PdfName type = (PdfName)GetPdfObjectRelease(dic.Get(PdfName.S));
             if (PdfName.GOTOR.Equals(type)) {
                 PdfObject ob3 = GetPdfObjectRelease(dic.Get(PdfName.D));
                 Object name = null;
                 if (ob3 != null) {
                     if (ob3.IsName())
                         name = ob3;
                     else if (ob3.IsString())
                         name = ob3.ToString();
                     PdfArray dest = null;
                     if (name != null && names.ContainsKey(name))
                         dest = (PdfArray)names[name];
                     if (dest != null) {
                         dic.Remove(PdfName.F);
                         dic.Remove(PdfName.NEWWINDOW);
                         dic.Put(PdfName.S, PdfName.GOTO);
                         SetXrefPartialObject(obj2Idx, ob2);
                         SetXrefPartialObject(objIdx, obj);
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
Example #35
0
 private PdfDictionary VerifyIsDictionary(PdfObject obj, String message) {
     if (obj == null || !obj.IsDictionary())
         Assert.Fail(message);
     return (PdfDictionary) obj;
 }
Example #36
0
            private PdfDictionary GetPropertiesDictionary(PdfObject operand1, ResourceDictionary resources){
                if (operand1.IsDictionary())
                    return (PdfDictionary)operand1;

                PdfName dictionaryName = ((PdfName)operand1);
                return resources.GetAsDict(dictionaryName);
            }