Exemplo n.º 1
0
 public Image(PDF pdf, PDFobj obj)
 {
     pdf.Newobj();
     pdf.Append("<<\n");
     pdf.Append("/Type /XObject\n");
     pdf.Append("/Subtype /Image\n");
     pdf.Append("/Filter ");
     pdf.Append(obj.GetValue(PDFobj.FILTER));
     pdf.Append('\n');
     pdf.Append("/Width ");
     pdf.Append(obj.GetValue(PDFobj.WIDTH));
     pdf.Append('\n');
     pdf.Append("/Height ");
     pdf.Append(obj.GetValue(PDFobj.HEIGHT));
     pdf.Append('\n');
     pdf.Append("/ColorSpace ");
     pdf.Append(obj.GetValue(PDFobj.COLORSPACE));
     pdf.Append('\n');
     pdf.Append("/BitsPerComponent ");
     pdf.Append(obj.GetValue(PDFobj.BITSPERCOMPONENT));
     pdf.Append('\n');
     pdf.Append("/Length ");
     pdf.Append(obj.stream.Length);
     pdf.Append('\n');
     pdf.Append(">>\n");
     pdf.Append("stream\n");
     pdf.Append(obj.stream, 0, obj.stream.Length);
     pdf.Append("\nendstream\n");
     pdf.Endobj();
     pdf.images.Add(this);
     this.objNumber = pdf.objNumber;
 }
Exemplo n.º 2
0
        private void GetPdfObjects2(byte[] pdf, PDFobj obj, List <PDFobj> pdfObjects)
        {
            string value = obj.GetValue("/Prev");

            if (!value.Equals(""))
            {
                this.GetPdfObjects2(pdf, this.GetObject(pdf, int.Parse(value)), pdfObjects);
            }
            obj.SetStream(pdf, int.Parse(obj.GetValue("/Length")));
            Decompressor decompressor = new Decompressor(obj.stream);

            byte[] decompressedData = decompressor.getDecompressedData();
            int    num  = 0;
            int    num2 = 0;
            int    num3 = 0;
            int    num4 = 0;

            for (int i = 0; i < obj.dict.Count; i++)
            {
                string text = obj.dict[i];
                if (text.Equals("/Predictor") && obj.dict[i + 1].Equals("12"))
                {
                    num = 1;
                }
                if (text.Equals("/W"))
                {
                    num2 = int.Parse(obj.dict[i + 2]);
                    num3 = int.Parse(obj.dict[i + 3]);
                    num4 = int.Parse(obj.dict[i + 4]);
                }
            }
            int num5 = num + num2 + num3 + num4;

            byte[] array = new byte[num5];
            for (int j = 0; j < decompressedData.Length; j += num5)
            {
                for (int k = 0; k < num5; k++)
                {
                    byte[] expr_135_cp_0 = array;
                    int    expr_135_cp_1 = k;
                    expr_135_cp_0[expr_135_cp_1] += decompressedData[j + k];
                }
                if (array[1] == 1)
                {
                    int off = this.ToInt(array, num + num2, num3);
                    pdfObjects.Add(this.GetObject(pdf, off, pdf.Length));
                }
            }
        }
Exemplo n.º 3
0
        private void GetPdfObjects1(byte[] pdf, PDFobj obj, List <PDFobj> pdfObjects)
        {
            string value = obj.GetValue("/Prev");

            if (!value.Equals(""))
            {
                this.GetPdfObjects1(pdf, this.GetObject(pdf, int.Parse(value)), pdfObjects);
            }
            int num = 1;

            while (true)
            {
                string text = obj.dict[num++];
                if (text.Equals("trailer"))
                {
                    break;
                }
                int num2 = int.Parse(obj.dict[num++]);
                for (int i = 0; i < num2; i++)
                {
                    string s        = obj.dict[num++];
                    string arg_8C_0 = obj.dict[num++];
                    string text2    = obj.dict[num++];
                    if (!text2.Equals("f"))
                    {
                        int num3 = int.Parse(s);
                        if (num3 != 0)
                        {
                            pdfObjects.Add(this.GetObject(pdf, num3));
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        public Image(PDF pdf, PDFobj obj)
        {
            // Console.WriteLine(obj.GetDict());
            w = float.Parse(obj.GetValue("/Width"));
            h = float.Parse(obj.GetValue("/Height"));
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /XObject\n");
            pdf.Append("/Subtype /Image\n");
            pdf.Append("/Filter ");
            pdf.Append(obj.GetValue("/Filter"));
            pdf.Append("\n");
            pdf.Append("/Width ");
            pdf.Append(w);
            pdf.Append('\n');
            pdf.Append("/Height ");
            pdf.Append(h);
            pdf.Append('\n');
            String colorSpace = obj.GetValue("/ColorSpace");

            if (!colorSpace.Equals(""))
            {
                pdf.Append("/ColorSpace ");
                pdf.Append(colorSpace);
                pdf.Append("\n");
            }
            pdf.Append("/BitsPerComponent ");
            pdf.Append(obj.GetValue("/BitsPerComponent"));
            pdf.Append("\n");
            String decodeParms = obj.GetValue("/DecodeParms");

            if (!decodeParms.Equals(""))
            {
                pdf.Append("/DecodeParms ");
                pdf.Append(decodeParms);
                pdf.Append("\n");
            }
            String imageMask = obj.GetValue("/ImageMask");

            if (!imageMask.Equals(""))
            {
                pdf.Append("/ImageMask ");
                pdf.Append(imageMask);
                pdf.Append("\n");
            }
            pdf.Append("/Length ");
            pdf.Append(obj.stream.Length);
            pdf.Append('\n');
            pdf.Append(">>\n");
            pdf.Append("stream\n");
            pdf.Append(obj.stream, 0, obj.stream.Length);
            pdf.Append("\nendstream\n");
            pdf.Endobj();
            pdf.images.Add(this);
            objNumber = pdf.objNumber;
        }
Exemplo n.º 5
0
        private void GetPdfObjects1(
            byte[] pdf, PDFobj obj, List <PDFobj> pdfObjects)
        {
            String xref = obj.GetValue("/Prev");

            if (!xref.Equals(""))
            {
                GetPdfObjects1(
                    pdf,
                    GetObject(pdf, Int32.Parse(xref)),
                    pdfObjects);
            }

            int i = 1;

            while (true)
            {
                String token = obj.dict[i++];
                if (token.Equals("trailer"))
                {
                    break;
                }

                int n = Int32.Parse(obj.dict[i++]); // Number of entries
                for (int j = 0; j < n; j++)
                {
                    String offset = obj.dict[i++];  // Object offset
                    String number = obj.dict[i++];  // Generation number
                    String status = obj.dict[i++];  // Status keyword
                    if (!status.Equals("f"))
                    {
                        int off = Int32.Parse(offset);
                        if (off != 0)
                        {
                            pdfObjects.Add(GetObject(pdf, off));
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
        public Font(PDF pdf, PDFobj obj)
        {
            string text = obj.GetValue("/BaseFont").Substring(1);

            if (text.Equals(Helvetica.name))
            {
                this.AddCoreFont(pdf, CoreFont.HELVETICA);
                return;
            }
            if (text.Equals(Helvetica_Bold.name))
            {
                this.AddCoreFont(pdf, CoreFont.HELVETICA_BOLD);
                return;
            }
            if (text.Equals(Helvetica_Oblique.name))
            {
                this.AddCoreFont(pdf, CoreFont.HELVETICA_OBLIQUE);
                return;
            }
            if (text.Equals(Helvetica_BoldOblique.name))
            {
                this.AddCoreFont(pdf, CoreFont.HELVETICA_BOLD_OBLIQUE);
                return;
            }
            if (text.Equals(Times_Roman.name))
            {
                this.AddCoreFont(pdf, CoreFont.TIMES_ROMAN);
                return;
            }
            if (text.Equals(Times_Bold.name))
            {
                this.AddCoreFont(pdf, CoreFont.TIMES_BOLD);
                return;
            }
            if (text.Equals(Times_Italic.name))
            {
                this.AddCoreFont(pdf, CoreFont.TIMES_ITALIC);
                return;
            }
            if (text.Equals(Times_BoldItalic.name))
            {
                this.AddCoreFont(pdf, CoreFont.TIMES_BOLD_ITALIC);
                return;
            }
            if (text.Equals(Courier.name))
            {
                this.AddCoreFont(pdf, CoreFont.COURIER);
                return;
            }
            if (text.Equals(Courier_Bold.name))
            {
                this.AddCoreFont(pdf, CoreFont.COURIER_BOLD);
                return;
            }
            if (text.Equals(Courier_Oblique.name))
            {
                this.AddCoreFont(pdf, CoreFont.COURIER_OBLIQUE);
                return;
            }
            if (text.Equals(Courier_BoldOblique.name))
            {
                this.AddCoreFont(pdf, CoreFont.COURIER_BOLD_OBLIQUE);
                return;
            }
            if (text.Equals(Symbol.name))
            {
                this.AddCoreFont(pdf, CoreFont.SYMBOL);
                return;
            }
            if (text.Equals(ZapfDingbats.name))
            {
                this.AddCoreFont(pdf, CoreFont.ZAPF_DINGBATS);
            }
        }
Exemplo n.º 7
0
        public Dictionary <int, PDFobj> Read(Stream inputStream)
        {
            List <PDFobj> list         = new List <PDFobj>();
            MemoryStream  memoryStream = new MemoryStream();
            int           num;

            while ((num = inputStream.ReadByte()) != -1)
            {
                memoryStream.WriteByte((byte)num);
            }
            byte[] array     = memoryStream.ToArray();
            int    startXRef = this.GetStartXRef(array);
            PDFobj pDFobj    = this.GetObject(array, startXRef);

            if (pDFobj.dict[0].Equals("xref"))
            {
                this.GetPdfObjects1(array, pDFobj, list);
            }
            else
            {
                this.GetPdfObjects2(array, pDFobj, list);
            }
            Dictionary <int, PDFobj> dictionary = new Dictionary <int, PDFobj>();

            for (int i = 0; i < list.Count; i++)
            {
                pDFobj        = list[i];
                pDFobj.number = int.Parse(pDFobj.dict[0]);
                if (pDFobj.dict.Contains("stream"))
                {
                    pDFobj.SetStream(array, pDFobj.GetLength(list));
                }
                if (pDFobj.GetValue("/Filter").Equals("/FlateDecode"))
                {
                    Decompressor decompressor = new Decompressor(pDFobj.stream);
                    pDFobj.data = decompressor.getDecompressedData();
                }
                if (pDFobj.GetValue("/Type").Equals("/ObjStm"))
                {
                    int num2 = int.Parse(pDFobj.GetValue("/First"));
                    int.Parse(pDFobj.GetValue("/N"));
                    PDFobj @object = this.GetObject(pDFobj.data, 0, num2);
                    for (int j = 0; j < @object.dict.Count; j += 2)
                    {
                        int key  = int.Parse(@object.dict[j]);
                        int num3 = int.Parse(@object.dict[j + 1]);
                        int len  = pDFobj.data.Length;
                        if (j <= @object.dict.Count - 4)
                        {
                            len = num2 + int.Parse(@object.dict[j + 3]);
                        }
                        PDFobj object2 = this.GetObject(pDFobj.data, num2 + num3, len);
                        object2.dict.Insert(0, "obj");
                        object2.dict.Insert(0, "0");
                        object2.dict.Insert(0, key.ToString());
                        dictionary[key] = object2;
                    }
                }
                else
                {
                    dictionary[pDFobj.number] = pDFobj;
                }
            }
            return(dictionary);
        }
Exemplo n.º 8
0
        public Dictionary <Int32, PDFobj> Read(Stream inputStream)
        {
            List <PDFobj> objects = new List <PDFobj>();

            MemoryStream baos = new MemoryStream();
            int          ch;

            while ((ch = inputStream.ReadByte()) != -1)
            {
                baos.WriteByte((byte)ch);
            }
            byte[] pdf = baos.ToArray();

            int xref = GetStartXRef(pdf);

            PDFobj obj = GetObject(pdf, xref);

            if (obj.dict[0].Equals("xref"))
            {
                GetPdfObjects1(pdf, obj, objects);
            }
            else
            {
                GetPdfObjects2(pdf, obj, objects);
            }

            Dictionary <Int32, PDFobj> pdfObjects = new Dictionary <Int32, PDFobj>();

            for (int i = 0; i < objects.Count; i++)
            {
                obj        = objects[i];
                obj.number = Int32.Parse(obj.dict[0]);
                if (obj.dict.Contains("stream"))
                {
                    obj.SetStream(pdf, obj.GetLength(objects));
                }

                if (obj.GetValue("/Filter").Equals("/FlateDecode"))
                {
                    Decompressor decompressor = new Decompressor(obj.stream);
                    obj.data = decompressor.getDecompressedData();
                }

                if (obj.GetValue("/Type").Equals("/ObjStm"))
                {
                    int    first = Int32.Parse(obj.GetValue("/First"));
                    int    n     = Int32.Parse(obj.GetValue("/N"));
                    PDFobj o2    = GetObject(obj.data, 0, first);
                    for (int j = 0; j < o2.dict.Count; j += 2)
                    {
                        int num = Int32.Parse(o2.dict[j]);
                        int off = Int32.Parse(o2.dict[j + 1]);
                        int end = obj.data.Length;
                        if (j <= o2.dict.Count - 4)
                        {
                            end = first + Int32.Parse(o2.dict[j + 3]);
                        }

                        PDFobj o3 = GetObject(obj.data, first + off, end);
                        o3.dict.Insert(0, "obj");
                        o3.dict.Insert(0, "0");
                        o3.dict.Insert(0, num.ToString());
                        pdfObjects[num] = o3;
                    }
                }
                else
                {
                    pdfObjects[obj.number] = obj;
                }
            }

            return(pdfObjects);
        }
Exemplo n.º 9
0
        private void GetPdfObjects2(
            byte[] pdf, PDFobj obj, List <PDFobj> pdfObjects)
        {
            String prev = obj.GetValue("/Prev");

            if (!prev.Equals(""))
            {
                GetPdfObjects2(
                    pdf,
                    GetObject(pdf, Int32.Parse(prev)),
                    pdfObjects);
            }

            obj.SetStream(pdf, Int32.Parse(obj.GetValue("/Length")));

            Decompressor decompressor = new Decompressor(obj.stream);

            byte[] data = decompressor.getDecompressedData();

            int p1 = 0; // Predictor byte
            int f1 = 0; // Field 1
            int f2 = 0; // Field 2
            int f3 = 0; // Field 3

            for (int i = 0; i < obj.dict.Count; i++)
            {
                String token = obj.dict[i];
                if (token.Equals("/Predictor"))
                {
                    if (obj.dict[i + 1].Equals("12"))
                    {
                        p1 = 1;
                    }
                    else
                    {
                        // TODO:
                    }
                }

                if (token.Equals("/W"))
                {
                    // "/W [ 1 3 1 ]"
                    f1 = Int32.Parse(obj.dict[i + 2]);
                    f2 = Int32.Parse(obj.dict[i + 3]);
                    f3 = Int32.Parse(obj.dict[i + 4]);
                }
            }

            int n = p1 + f1 + f2 + f3; // Number of bytes per entry

            byte[] entry = new byte[n];

            for (int i = 0; i < data.Length; i += n)
            {
                // Apply the 'Up' filter.
                for (int j = 0; j < n; j++)
                {
                    entry[j] += data[i + j];
                }

                if (entry[1] == 0x01)
                {
                    int off = ToInt(entry, p1 + f1, f2);
                    pdfObjects.Add(GetObject(pdf, off, pdf.Length));
                }
            }
        }
Exemplo n.º 10
0
        private void GetObjects2(
            byte[] pdf,
            PDFobj obj,
            List<PDFobj> objects)
        {
            String prev = obj.GetValue("/Prev");
            if (!prev.Equals("")) {
            GetObjects2(
                    pdf,
                    GetObject(pdf, Int32.Parse(prev)),
                    objects);
            }

            obj.SetStream(pdf, Int32.Parse(obj.GetValue("/Length")));
            try {
            Decompressor decompressor = new Decompressor(obj.stream);
            obj.data = decompressor.GetDecompressedData();
            }
            catch (Exception e) {
            // Assume no compression.
            obj.data = obj.stream;
            }

            int p1 = 0; // Predictor byte
            int f1 = 0; // Field 1
            int f2 = 0; // Field 2
            int f3 = 0; // Field 3
            for (int i = 0; i < obj.dict.Count; i++) {
            String token = obj.dict[i];
            if (token.Equals("/Predictor")) {
                if (obj.dict[i + 1].Equals("12")) {
                    p1 = 1;
                }
            }

            if (token.Equals("/W")) {
                // "/W [ 1 3 1 ]"
                f1 = Int32.Parse(obj.dict[i + 2]);
                f2 = Int32.Parse(obj.dict[i + 3]);
                f3 = Int32.Parse(obj.dict[i + 4]);
            }
            }

            int n = p1 + f1 + f2 + f3;          // Number of bytes per entry
            byte[] entry = new byte[n];
            for (int i = 0; i < obj.data.Length; i += n) {
            // Apply the 'Up' filter.
            for (int j = 0; j < n; j++) {
                entry[j] += obj.data[i + j];
            }

            // Process the entries in a cross-reference stream
            // Page 51 in PDF32000_2008.pdf
            if (entry[p1] == 1) {           // Type 1 entry
                PDFobj o2 = GetObject(pdf, ToInt(entry, p1 + f1, f2));
                o2.number = Int32.Parse(o2.dict[0]);
                objects.Add(o2);
            }
            }
        }
Exemplo n.º 11
0
        private void GetObjects1(
            byte[] pdf,
            PDFobj obj,
            List<PDFobj> objects)
        {
            String xref = obj.GetValue("/Prev");
            if (!xref.Equals("")) {
            GetObjects1(
                    pdf,
                    GetObject(pdf, Int32.Parse(xref)),
                    objects);
            }

            int i = 1;
            while (true) {
            String token = obj.dict[i++];
            if (token.Equals("trailer")) {
                break;
            }

            int n = Int32.Parse(obj.dict[i++]);     // Number of entries
            for (int j = 0; j < n; j++) {
                String offset = obj.dict[i++];      // Object offset
                String number = obj.dict[i++];      // Generation number
                String status = obj.dict[i++];      // Status keyword
                if (!status.Equals("f")) {
                    PDFobj o2 = GetObject(pdf, Int32.Parse(offset));
                    o2.number = Int32.Parse(o2.dict[0]);
                    objects.Add(o2);
                }
            }
            }
        }