Esempio n. 1
0
 private void _Replaced(string index, PDFObject obj)
 {
     if (this.Replaced != null)
     {
         this.Replaced.Invoke(this, index, obj);
     }
 }
Esempio n. 2
0
 private void _Inserted(string index, PDFObject obj)
 {
     if (this.Inserted != null)
     {
         this.Inserted.Invoke(this, index, obj);
     }
 }
Esempio n. 3
0
        public Stream ApplyDecodeFilters(PDFObject filter, PDFObject decodeParams)
        {
            Stream   stream1;
            PDFArray array1;
            PDFArray array2;
            int      num1;
            Stream   stream2;

            try
            {
                if (filter.PDFType == PDFObjectType.tPDFArray)
                {
                    stream1 = this.Decrypt();
                    array1  = ((PDFArray)filter);
                    array2  = ((PDFArray)decodeParams);
                    for (num1 = 0; (num1 < array1.Count); num1 += 1)
                    {
                        stream1 = this.ApplyDecodeFilter(stream1, ((PDFName)array1[num1]), ((array2 != null) ? (array2[num1] as PDFDict) : null));
                    }
                    return(stream1);
                }
                return(this.ApplyDecodeFilter(this.Decrypt(), ((PDFName)filter), ((PDFDict)decodeParams)));
            }
            catch (InvalidCastException)
            {
                throw new PDFSyntaxException("Invalid stream dictionary");
            }
            return(stream2);
        }
Esempio n. 4
0
        private void Create()
        {
            XRefEntry entry1 = new XRefEntry();

            entry1.dirty      = false;
            entry1.type       = 0;
            entry1.generation = 65535;
            entry1.indirect   = null;
            entry1.offset     = ((long)0);
            this.mXRef.Add(entry1);
            this.mRoot = Library.CreateDict();
            this.Indirects.New().Direct = this.mRoot;
            this.mRoot["Type"]          = Library.CreateName("Catalog");
            PDFObject obj1 = this.Indirects.New();

            this.mRoot["Pages"] = obj1;
            PDFDirect direct1 = Library.CreateDict();

            obj1.Direct = direct1;
            PDFDict dict1 = ((PDFDict)direct1);

            dict1["Type"]         = Library.CreateName("Pages");
            dict1["Count"]        = Library.CreateInteger(((long)0));
            dict1["Kids"]         = Library.CreateArray(0);
            this.mInfo            = Library.CreateDict();
            this.mTrailer         = Library.CreateDict();
            this.mTrailer["Root"] = this.Indirects.New(this.mRoot);
            this.mTrailer["Info"] = this.Indirects.New(this.mInfo);
        }
Esempio n. 5
0
 private void _Inserting(int index, PDFObject obj)
 {
     if (this.Inserting != null)
     {
         this.Inserting.Invoke(this, index, obj);
     }
 }
Esempio n. 6
0
        public void InsertStream(PDFStream s, int nr)
        {
            PDFArray  array1;
            PDFObject obj2;
            PDFObject obj1 = this.mPageDict["Contents"];

            if (obj1 == null)
            {
                obj2 = Library.CreateArray(0);
                this.mPageDict["Contents"] = obj2;
                obj1 = obj2;
            }
            if (obj1.PDFType == PDFObjectType.tPDFStream)
            {
                array1 = Library.CreateArray(0);
                if (nr > 1)
                {
                    throw new IndexOutOfRangeException("Invalid contents stream index");
                }
                this.mPageDict["Contents"] = array1;
                array1.Add(((nr == 1) ? obj1 : s));
                array1.Add(((nr == 1) ? s : obj1));
                return;
            }
            if (obj1.PDFType == PDFObjectType.tPDFArray)
            {
                ((PDFArray)obj1).Insert(nr, s);
                return;
            }
            throw new PDFSyntaxException("Invalid CONTENTS entry");
        }
Esempio n. 7
0
        PDFObject HitTestPDFImageObject(LeadPoint pt)
        {
            PDFObject retObject = new PDFObject();

            PDFDocumentPage page = _document.Pages[_currentPageNumber - 1];

            if (page.Objects != null && page.Objects.Count > 0)
            {
                foreach (PDFObject obj in page.Objects)
                {
                    if (obj.ObjectType == PDFObjectType.Image)
                    {
                        PDFRect  tempPDFRect = page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, obj.Bounds);
                        LeadRect objectRect  = ToLeadRect(tempPDFRect);
                        if (objectRect.Contains(pt))
                        {
                            // Point in rect then user pressed over image object, so return it.
                            retObject = obj;
                            break;
                        }
                    }
                }
            }

            return(retObject);
        }
Esempio n. 8
0
        private void WritePDFFile()
        {
            List <long> xref = new List <long>();

            using (Stream stream = new FileStream(pdfFileName, FileMode.OpenOrCreate))
            {
                stream.SetLength(0); // clear previous file contents

                // File header
                PDFObject.WriteASCIIBytes("%PDF-1.5\r\n", stream);
                WriteBinaryMarker(stream);

                foreach (PDFFont font in _fontMapping.Values)
                {
                    font.Write(stream);
                }

                foreach (PDFObject obj in indirectObjects)
                {
                    xref.Add(stream.Position);
                    obj.Write(stream);
                }

                long startXref = stream.Position;
                WriteXref(xref, stream);

                PDFObject.WriteASCIIBytes("trailer\r\n", stream);
                trailerDictionary.Write(stream);
                PDFObject.WriteASCIIBytes($"\r\nstartxref\r\n{startXref}\r\n%%EOF\r\n", stream);
            }
        }
Esempio n. 9
0
        public static Resource Get(PDFObject obj, Type typ)
        {
            if (obj == null)
            {
                return(null);
            }
            Resource resource1 = ((Resource)Resources.mAllResources[obj.Direct.UniqueID]);

            if (resource1 == null)
            {
                resource1 = Resources.CreateResource(obj.Direct, typ);
                if (resource1 != null)
                {
                    Resources.mAllResources[obj.Direct.UniqueID] = resource1;
                }
                return(resource1);
            }
            Type type1 = resource1.GetType();

            if (!type1.Equals(typ) && !type1.IsSubclassOf(typ))
            {
                return(null);
            }
            return(resource1);
        }
Esempio n. 10
0
 public Resource this[PDFObject i]
 {
     get
     {
         return(this[i, typeof(Resource)]);
     }
 }
        private void updateDictionaryKey(PDFDict pdfDict, string newKey, string oldKey)
        {
            PDFObject valueInDict = pdfDict.Get(oldKey);

            pdfDict.Remove(oldKey);
            pdfDict.Put(newKey, valueInDict);
        }
Esempio n. 12
0
 internal static PDFObject TreatNewCollectionObject(PDFDirect collection, PDFObject o)
 {
     if (!o.IsIndirect)
     {
         if (o.Parent != null)
         {
             o = ((PDFObject)o.Clone());
         }
         if (o.PDFType == PDFObjectType.tPDFStream)
         {
             if (collection.Doc != null)
             {
                 o = collection.Doc.Indirects.New(o.Direct);
             }
             else
             {
                 o = Library.CreateIndirect(o.Direct);
             }
         }
         o.Parent = collection;
         return(o);
     }
     if (((o.Doc != collection.Doc) && (o.Doc != null)) && (collection.Doc != null))
     {
         throw new InvalidOperationException("Can not connect objects from different documents");
     }
     if ((o.Doc == null) && (collection.Doc != null))
     {
         ((CorePDFObject)o.Indirect).SetDocument(collection.Doc);
     }
     return(o.Indirect);
 }
Esempio n. 13
0
        internal object _GetObject(string index)
        {
            DictionaryEntry entry2;
            PDFObject       obj1  = this.mBaseDict[this.mBaseKeyName];
            PDFDict         dict1 = (obj1 as PDFDict);
            object          obj2  = null;

            if (dict1 == null)
            {
                return(null);
            }
            obj2 = ((object)dict1[index]);
            foreach (DictionaryEntry entry1 in this.mObjectList)
            {
                if (entry1.Key != obj2)
                {
                    continue;
                }
                return(entry1.Value);
            }
            entry2 = new DictionaryEntry(obj2, this.ComplexObjectFactory(((PDFDirect)obj2)));
            if (entry2.Value != null)
            {
                this.mObjectList.Add(entry2);
            }
            return(entry2.Value);
        }
Esempio n. 14
0
 private void _Replacing(int index, PDFObject obj)
 {
     if (this.Replacing != null)
     {
         this.Replacing.Invoke(this, index, obj);
     }
 }
Esempio n. 15
0
        // These methods are public static so the Enum classes can use them
        // Return strings for the data type and contents of the object
        public static bool GetObjectTypeAndValue(PDFObject obj, out String type, out String value)
        {
            bool retval = true;

            // Classify based on object type
            if (obj is PDFBoolean)
            {
                type  = "Bool";
                value = (obj as PDFBoolean).Value.ToString();
            }
            else if (obj is PDFInteger)
            {
                type  = "Int";
                value = (obj as PDFInteger).Value.ToString();
            }
            else if (obj is PDFReal)
            {
                type  = "Real";
                value = (obj as PDFReal).Value.ToString();
            }
            else if (obj is PDFName)
            {
                type  = "Name";
                value = (obj as PDFName).Value;
            }
            else if (obj is PDFString)
            {
                type  = "String";
                value = (obj as PDFString).Value;
            }
            else if (obj is PDFArray)
            {
                type  = "Array";
                value = "[...]";
            }
            else if (obj is PDFDict)
            {
                type  = "Dict";
                value = "<<...>>";
            }
            else if (obj is PDFStream)
            {
                type  = "Stream";
                value = "<<...>>";
            }
            else if (obj == null)
            {
                // Arrays can have "null" objects, we respect that here
                type  = "(null)";
                value = "(null)";
            }
            else
            {
                type   = "unknown";
                value  = "???";
                retval = false;
            }

            return(retval);
        }
Esempio n. 16
0
        public void Encode(Stream str, bool setDirectly)
        {
            if (str.Position > ((long)0))
            {
                str.Position = ((long)0);
            }
            PDFObject obj1 = null;
            PDFObject obj2 = null;

            if (this.IsExternal)
            {
                obj1 = this.mDict["FFilter"];
                obj2 = this.mDict["FDecodeParms"];
            }
            else
            {
                obj1 = this.mDict["Filter"];
                obj2 = this.mDict["DecodeParms"];
            }
            if (obj1 == null)
            {
                this.Encrypt(str, setDirectly);
                return;
            }
            this.ApplyEncodeFilters(str, obj1, obj2, setDirectly);
        }
        private void editSelectedScalar(object newValue)
        {
            PDFObject newPDFObject = createNewValueForSelectedScalar(selectedNodeBeforeEdit.Tag as PDFObject, newValue.ToString());

            modifyParentOfSelectedScalar(newPDFObject);
            selectedNodeBeforeEdit.Tag = newPDFObject;
            docWasModified             = true;
        }
 internal void _SetObject(int index, object value, PDFObject valueDict)
 {
     if (index > this.Count)
     {
         throw new IndexOutOfRangeException("Index in collection is out of range");
     }
     this.mInternalArr[index] = valueDict;
     this.mObjectList.Add(new DictionaryEntry(valueDict.Direct, value));
 }
Esempio n. 19
0
 public Resource this[PDFObject i, Type resType]
 {
     get
     {
         object obj1 = Resources.Get(i, resType);
         this.mResources[i.Direct.UniqueID] = obj1;
         return((Resource)obj1);
     }
 }
Esempio n. 20
0
        /// <summary>
        /// Ends a direct or indirect dictionary or array.
        /// </summary>
        public void WriteEndObject()
        {
            int count = _stack.Count;

            Debug.Assert(count > 0, "PDFWriter stack underflow.");

            StackItem stackItem = _stack[count - 1];

            _stack.RemoveAt(count - 1);

            PDFObject value    = stackItem.Object;
            bool      indirect = value.IsIndirect;

            if (Layout == PDFWriterLayout.Verbose)
            {
                DecreaseIndent();
            }
            if (value is PDFArray)
            {
                if (indirect)
                {
                    WriteRaw("\n]\n");
                    _lastCat = CharCat.NewLine;
                }
                else
                {
                    WriteRaw("]");
                    _lastCat = CharCat.Delimiter;
                }
            }
            else if (value is PDFDictionary)
            {
                if (indirect)
                {
                    if (!stackItem.HasStream)
                    {
                        WriteRaw(_lastCat == CharCat.NewLine ? ">>\n" : " >>\n");
                    }
                }
                else
                {
                    Debug.Assert(!stackItem.HasStream, "Direct object with stream??");
                    WriteSeparator(CharCat.NewLine);
                    WriteRaw(">>\n");
                    _lastCat = CharCat.NewLine;
                }
            }
            if (indirect)
            {
                NewLine();
                WriteRaw("endobj\n");
                if (Layout == PDFWriterLayout.Verbose)
                {
                    WriteRaw("%--------------------------------------------------------------------------------------------------\n");
                }
            }
        }
        private void editSelectedDictValue(PDFDict selectedDict, string key, object newValue)
        {
            PDFObject newPDFObject = createNewValueForSelectedScalar(selectedDict.Get(key), newValue.ToString());

            updateDictionaryValue(selectedDict, key, newPDFObject);
            //refresh the tree
            selectedDict.EnumPDFObjects(new EnumObjectsForTree(selectedNodeBeforeEdit));
            mainWindow.expandNode(selectedNodeBeforeEdit);
        }
        private void editSelectedArrayValue(PDFArray selectedArray, int index, object newValue)
        {
            PDFObject newPDFObject = createNewValueForSelectedScalar(selectedArray.Get(index), newValue.ToString());

            updateArrayValue(selectedArray, index, newPDFObject);
            //refresh the tree
            mainWindow.addArrayItemToTree(selectedArray, index, selectedNodeBeforeEdit);
            mainWindow.expandNode(selectedNodeBeforeEdit);
        }
Esempio n. 23
0
 public void Insert(int pos, PDFObject obj)
 {
     base._Changing();
     this._Inserting(pos, obj);
     obj = CorePDFObject.TreatNewCollectionObject(this, obj);
     this.mArray.Insert(pos, obj);
     this.Dirty = true;
     this._Inserted(pos, obj);
     base._Changed();
 }
Esempio n. 24
0
 public void Add(PDFObject obj)
 {
     base._Changing();
     this._Inserting(this.Count, obj);
     obj = CorePDFObject.TreatNewCollectionObject(this, obj);
     this.mArray.Add(obj);
     this.Dirty = true;
     this._Inserted((this.Count - 1), obj);
     base._Changed();
 }
Esempio n. 25
0
        public static string FileSpecToFileName(PDFObject spec)
        {
            FileSpec spec1 = new FileSpec(spec.Direct);

            if (spec1.IsValid)
            {
                return(spec1.Path);
            }
            return("");
        }
Esempio n. 26
0
        public void ApplyEncodeFilters(Stream str, PDFObject filter, PDFObject encodeParams, bool setDirectly)
        {
            MemoryStream stream1;
            Stream       stream2;
            PDFArray     array1;
            PDFArray     array2;
            int          num1;

            byte[] numArray1;
            int    num2;

            try
            {
                stream1 = new MemoryStream();
                stream2 = stream1;
                if (filter.PDFType == PDFObjectType.tPDFArray)
                {
                    array1 = ((PDFArray)filter);
                    array2 = ((PDFArray)encodeParams);
                    for (num1 = 0; (num1 < array1.Count); num1 += 1)
                    {
                        stream2 = this.ApplyEncodeFilter(stream2, ((PDFName)array1[num1]), ((array2 != null) ? (array2[num1] as PDFDict) : null));
                    }
                }
                else
                {
                    stream2 = this.ApplyEncodeFilter(stream2, ((PDFName)filter), ((PDFDict)encodeParams));
                }
                numArray1 = new byte[16384];
                if (stream2 == stream1)
                {
                    goto Label_00B4;
                }
                num2 = 0;
                goto Label_0092;
Label_0087:
                stream2.Write(numArray1, 0, num2);
Label_0092:
                num2 = str.Read(numArray1, 0, numArray1.Length);
                if (num2 != 0)
                {
                    goto Label_0087;
                }
                stream2.Flush();
                this.Encrypt(stream1, true);
                return;

Label_00B4:
                this.Encrypt(str, setDirectly);
            }
            catch (InvalidCastException)
            {
                throw new PDFSyntaxException("Invalid stream dictionary");
            }
        }
Esempio n. 27
0
        private void WriteXref(List <long> xref, Stream stream)
        {
            PDFObject.WriteASCIIBytes(
                $"xref\r\n0 {indirectObjects.Count + 1}\r\n0000000000 65535 f\r\n", stream);

            foreach (long offset in xref)
            {
                string str = offset.ToString().PadLeft(10, '0');
                PDFObject.WriteASCIIBytes($"{str} 00000 n\r\n", stream);
            }
        }
Esempio n. 28
0
        internal void _SetObject(string index, object value, PDFObject valueDict)
        {
            PDFDict dict1 = (this.mBaseDict[this.mBaseKeyName] as PDFDict);

            if (dict1 == null)
            {
                dict1 = Library.CreateDict();
                this.mBaseDict[this.mBaseKeyName] = dict1;
            }
            dict1[index] = valueDict;
            this.mObjectList.Add(new DictionaryEntry(valueDict.Direct, value));
        }
Esempio n. 29
0
 public PDFObject this[PDFName key, bool getDirect]
 {
     get
     {
         PDFObject obj1 = ((PDFObject)this.mHash[key]);
         if ((obj1 != null) && getDirect)
         {
             obj1 = obj1.Direct;
         }
         return(obj1);
     }
 }
Esempio n. 30
0
        // About PDFReference
        //
        // * A PDFReference holds either the ObjectID or the PDFObject or both.
        //
        // * Each PDFObject has a PDFReference if and only if it is an indirect object. Direct objects have
        //   no PDFReference, because they are embedded in a parent objects.
        //
        // * PDFReference objects are used to reference PDFObject instances. A value in a PDF dictionary
        //   or array that is a PDFReference represents an indirect reference. A value in a PDF dictionary or
        //   or array that is a PDFObject represents a direct (or embeddded) object.
        //
        // * When a PDF file is imported, the PDFXRefTable is filled with PDFReference objects keeping the
        //   ObjectsIDs and file positions (offsets) of all indirect objects.
        //
        // * Indirect objects can easily be renumbered because they do not rely on their ObjectsIDs.
        //
        // * During modification of a document the ObjectID of an indirect object has no meaning,
        //   except that they must be different in pairs.

        /// <summary>
        /// Initializes a new PDFReference instance for the specified indirect object.
        /// </summary>
        public PDFReference(PDFObject pdfObject)
        {
            if (pdfObject.Reference != null)
            {
                throw new InvalidOperationException("Must not create iref for an object that already has one.");
            }
            _value = pdfObject;
            pdfObject.Reference = this;
#if UNIQUE_IREF && DEBUG
            _uid = ++s_counter;
#endif
        }