Beispiel #1
0
        internal static void GetFontData(Font font, Stream inputStream)
        {
            int len = inputStream.ReadByte();

            byte[] fontName = new byte[len];
            inputStream.Read(fontName, 0, len);
            font.name = System.Text.Encoding.UTF8.GetString(fontName, 0, len);

            len = GetInt24(inputStream);
            byte[] fontInfo = new byte[len];
            inputStream.Read(fontInfo, 0, len);
            font.info = System.Text.Encoding.UTF8.GetString(fontInfo, 0, len);

            byte[] buf = new byte[GetInt32(inputStream)];
            inputStream.Read(buf, 0, buf.Length);
            MemoryStream stream = new MemoryStream(Decompressor.inflate(buf));

            font.unitsPerEm             = GetInt32(stream);
            font.bBoxLLx                = GetInt32(stream);
            font.bBoxLLy                = GetInt32(stream);
            font.bBoxURx                = GetInt32(stream);
            font.bBoxURy                = GetInt32(stream);
            font.fontAscent             = GetInt32(stream);
            font.fontDescent            = GetInt32(stream);
            font.firstChar              = GetInt32(stream);
            font.lastChar               = GetInt32(stream);
            font.capHeight              = GetInt32(stream);
            font.fontUnderlinePosition  = GetInt32(stream);
            font.fontUnderlineThickness = GetInt32(stream);

            len = GetInt32(stream);
            font.advanceWidth = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.advanceWidth[i] = GetInt16(stream);
            }

            len             = GetInt32(stream);
            font.glyphWidth = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.glyphWidth[i] = GetInt16(stream);
            }

            len = GetInt32(stream);
            font.unicodeToGID = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.unicodeToGID[i] = GetInt16(stream);
            }

            font.cff = (inputStream.ReadByte() == 'Y') ? true : false;
            font.uncompressedSize = GetInt32(inputStream);
            font.compressedSize   = GetInt32(inputStream);
        }
Beispiel #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));
                }
            }
        }
Beispiel #3
0
 internal void SetStreamAndData(byte[] buf, int length)
 {
     if (this.stream == null)
     {
         this.stream = new byte[length];
         Array.Copy(buf, streamOffset, stream, 0, length);
         if (GetValue("/Filter").Equals("/FlateDecode"))
         {
             this.data = Decompressor.inflate(stream);
         }
         else
         {
             // Assume no compression for now.
             // In the future we may handle LZW decompression ...
             this.data = stream;
         }
     }
 }
Beispiel #4
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);
        }
Beispiel #5
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);
        }
Beispiel #6
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));
                }
            }
        }
Beispiel #7
0
 private byte[] GetDecompressedData()
 {
     Decompressor decompressor = new Decompressor(data);
     return decompressor.GetDecompressedData();
 }
Beispiel #8
0
        public SortedDictionary<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 obj1 = GetObject(pdf, xref);
            if (obj1.dict[0].Equals("xref")) {
            GetObjects1(pdf, obj1, objects);
            }
            else {
            GetObjects2(pdf, obj1, objects);
            }

            SortedDictionary<Int32, PDFobj> pdfObjects = new SortedDictionary<Int32, PDFobj>();
            foreach (PDFobj obj in objects) {
            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();
                }
                else {
                    // Assume no compression.
                    obj.data = obj.stream;
                }
            }

            if (obj.GetValue("/Type").Equals("/ObjStm")) {
                int n = Int32.Parse(obj.GetValue("/N"));
                int first = Int32.Parse(obj.GetValue("/First"));
                PDFobj o2 = GetObject(obj.data, 0, first);
                for (int i = 0; i < o2.dict.Count; i += 2) {
                    int num = Int32.Parse(o2.dict[i]);
                    int off = Int32.Parse(o2.dict[i + 1]);
                    int end = obj.data.Length;
                    if (i <= o2.dict.Count - 4) {
                        end = first + Int32.Parse(o2.dict[i + 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;
        }
Beispiel #9
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);
            }
            }
        }
Beispiel #10
0
        private void getObjOffsets( byte[] pdf, String xref )
        {
            PDFobj obj = getObject( pdf, Int32.Parse( xref ), pdf.Length );
            obj.setStream( pdf, obj.getLength( null ) );

            xref = obj.getValue( "/Prev" );
            if ( !xref.Equals( "" ) ) {
            getObjOffsets( pdf, xref );
            }

            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 );
                objects.Add( getObject( pdf, off, pdf.Length ) );
            }

            }
        }
Beispiel #11
0
        public List <PDFobj> read(Stream inputStream)
        {
            MemoryStream baos = new MemoryStream();
            int          ch;

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

            byte[] pdf = baos.ToArray();

            int startxref = indexOfStartXRef(pdf);

            if (pdf[startxref + 9] == 0x0D &&
                pdf[startxref + 10] == 0x0A)
            {
                // Console.WriteLine( "This PDF file is using CR/LF for EOL." );
                CR_LF = true;
            }

            objects = new List <PDFobj>();

            PDFobj obj = getObject(pdf, startxref, pdf.Length);

            String xref = obj.dict[1];

            obj = getObject(pdf, Int32.Parse(xref), pdf.Length);

            if (obj.dict[0].Equals("xref"))
            {
                for (int i = 3; i < obj.dict.Count; i += 3)
                {
                    String token = obj.dict[i];
                    if (!token.Equals("trailer"))
                    {
                        int off = Int32.Parse(token);
                        if (off != 0)
                        {
                            objects.Add(getObject(pdf, off, pdf.Length));
                        }
                    }
                }
            }
            else
            {
                getObjOffsets(pdf, xref);
            }

            for (int i = 0; i < objects.Count; i++)
            {
                obj = objects[i];
                int offset = obj.dict.Count - 1;
                if (obj.dict[offset].Equals("stream"))
                {
                    obj.setStream(pdf, obj.getLength(objects));
                }
            }

            objects2 = new List <PDFobj>();

            for (int i = 0; i < objects.Count; i++)
            {
                obj = objects[i];
                if (obj.getValue("/Type").Equals("/XRef") ||
                    !obj.getValue("/S").Equals(""))         // Hint Table
                {
                    continue;
                }

                if (obj.getValue("/Type").Equals("/XObject") ||
                    obj.getValue("/Subtype").Equals("/Type1C") ||
                    obj.getValue("/Subtype").Equals("/CIDFontType0C"))
                {
                    objects2.Add(obj);
                    continue;
                }

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


            objects3 = new List <PDFobj>();

            for (int i = 0; i < objects2.Count; i++)
            {
                obj = objects2[i];
                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());
                        objects3.Add(o3);
                    }
                }
                else
                {
                    objects3.Add(obj);
                }
            }


            objects4 = new List <PDFobj>();

            for (int i = 0; i < objects.Count; i++)
            {
                obj = objects[i];
                if (!obj.getValue("/Type").Equals("/ObjStm") &&
                    !obj.getValue("/Type").Equals("/XRef"))
                {
                    obj.number = Int32.Parse(obj.dict[0]);
                    objects4.Add(obj);
                }
            }

            for (int i = 0; i < objects3.Count; i++)
            {
                obj        = objects3[i];
                obj.number = Int32.Parse(obj.dict[0]);
                objects4.Add(obj);
            }

            return(objects4);
        }
        internal static void Register(
            PDF pdf,
            Font font,
            Stream inputStream)
        {
            int len = inputStream.ReadByte();

            byte[] fontName = new byte[len];
            inputStream.Read(fontName, 0, len);
            font.name = System.Text.Encoding.UTF8.GetString(fontName, 0, len);
            // Console.WriteLine(font.name);

            len = GetInt24(inputStream);
            byte[] fontInfo = new byte[len];
            inputStream.Read(fontInfo, 0, len);
            font.info = System.Text.Encoding.UTF8.GetString(fontInfo, 0, len);
            // Console.WriteLine(font.info);

            byte[] buf = new byte[GetInt32(inputStream)];
            inputStream.Read(buf, 0, buf.Length);
            Decompressor decompressor = new Decompressor(buf);
            MemoryStream stream       =
                new MemoryStream(decompressor.GetDecompressedData());

            font.unitsPerEm         = GetInt32(stream);
            font.bBoxLLx            = GetInt32(stream);
            font.bBoxLLy            = GetInt32(stream);
            font.bBoxURx            = GetInt32(stream);
            font.bBoxURy            = GetInt32(stream);
            font.ascent             = GetInt32(stream);
            font.descent            = GetInt32(stream);
            font.firstChar          = GetInt32(stream);
            font.lastChar           = GetInt32(stream);
            font.capHeight          = GetInt32(stream);
            font.underlinePosition  = GetInt32(stream);
            font.underlineThickness = GetInt32(stream);

            len = GetInt32(stream);
            font.advanceWidth = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.advanceWidth[i] = GetInt16(stream);
            }

            len             = GetInt32(stream);
            font.glyphWidth = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.glyphWidth[i] = GetInt16(stream);
            }

            len = GetInt32(stream);
            font.unicodeToGID = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.unicodeToGID[i] = GetInt16(stream);
            }

            font.cff = (inputStream.ReadByte() == 'Y') ? true : false;
            font.uncompressed_size = GetInt32(inputStream);
            font.compressed_size   = GetInt32(inputStream);

            EmbedFontFile(pdf, font, inputStream);
            AddFontDescriptorObject(pdf, font);
            AddCIDFontDictionaryObject(pdf, font);
            AddToUnicodeCMapObject(pdf, font);

            // Type0 Font Dictionary
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            pdf.Append("/Subtype /Type0\n");
            pdf.Append("/BaseFont /");
            pdf.Append(font.name);
            pdf.Append('\n');
            pdf.Append("/Encoding /Identity-H\n");
            pdf.Append("/DescendantFonts [");
            pdf.Append(font.GetCidFontDictObjNumber());
            pdf.Append(" 0 R]\n");
            pdf.Append("/ToUnicode ");
            pdf.Append(font.GetToUnicodeCMapObjNumber());
            pdf.Append(" 0 R\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            font.objNumber = pdf.objNumber;
        }
Beispiel #13
0
        private byte[] GetDecompressedData()
        {
            Decompressor decompressor = new Decompressor(data);

            return(decompressor.GetDecompressedData());
        }
        internal static void Register(
            SortedDictionary <Int32, PDFobj> objects,
            Font font,
            Stream inputStream)
        {
            int len = inputStream.ReadByte();

            byte[] fontName = new byte[len];
            inputStream.Read(fontName, 0, len);
            font.name = System.Text.Encoding.UTF8.GetString(fontName, 0, len);
            // Console.WriteLine(font.name);

            len = GetInt24(inputStream);
            byte[] fontInfo = new byte[len];
            inputStream.Read(fontInfo, 0, len);
            font.info = System.Text.Encoding.UTF8.GetString(fontInfo, 0, len);
            // Console.WriteLine(font.info);

            byte[] buf = new byte[GetInt32(inputStream)];
            inputStream.Read(buf, 0, buf.Length);
            Decompressor decompressor = new Decompressor(buf);
            MemoryStream stream       =
                new MemoryStream(decompressor.GetDecompressedData());

            font.unitsPerEm         = GetInt32(stream);
            font.bBoxLLx            = GetInt32(stream);
            font.bBoxLLy            = GetInt32(stream);
            font.bBoxURx            = GetInt32(stream);
            font.bBoxURy            = GetInt32(stream);
            font.ascent             = GetInt32(stream);
            font.descent            = GetInt32(stream);
            font.firstChar          = GetInt32(stream);
            font.lastChar           = GetInt32(stream);
            font.capHeight          = GetInt32(stream);
            font.underlinePosition  = GetInt32(stream);
            font.underlineThickness = GetInt32(stream);

            len = GetInt32(stream);
            font.advanceWidth = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.advanceWidth[i] = GetInt16(stream);
            }

            len             = GetInt32(stream);
            font.glyphWidth = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.glyphWidth[i] = GetInt16(stream);
            }

            len = GetInt32(stream);
            font.unicodeToGID = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.unicodeToGID[i] = GetInt16(stream);
            }

            font.cff = (inputStream.ReadByte() == 'Y') ? true : false;
            font.uncompressed_size = GetInt32(inputStream);
            font.compressed_size   = GetInt32(inputStream);

            EmbedFontFile(objects, font, inputStream);
            AddFontDescriptorObject(objects, font);
            AddCIDFontDictionaryObject(objects, font);
            AddToUnicodeCMapObject(objects, font);

            // Type0 Font Dictionary
            PDFobj        obj  = new PDFobj();
            List <String> dict = obj.GetDict();

            dict.Add("<<");
            dict.Add("/Type");
            dict.Add("/Font");
            dict.Add("/Subtype");
            dict.Add("/Type0");
            dict.Add("/BaseFont");
            dict.Add("/" + font.name);
            dict.Add("/Encoding");
            dict.Add("/Identity-H");
            dict.Add("/DescendantFonts");
            dict.Add("[");
            dict.Add(font.GetCidFontDictObjNumber().ToString());
            dict.Add("0");
            dict.Add("R");
            dict.Add("]");
            dict.Add("/ToUnicode");
            dict.Add(font.GetToUnicodeCMapObjNumber().ToString());
            dict.Add("0");
            dict.Add("R");
            dict.Add(">>");
            obj.number = MaxKey(objects.Keys) + 1;
            objects.Add(obj.number, obj);
            font.objNumber = obj.number;
        }
Beispiel #15
0
        private void getObjOffsets(byte[] pdf, String xref)
        {
            PDFobj obj = getObject(pdf, Int32.Parse(xref), pdf.Length);

            obj.setStream(pdf, obj.getLength(null));

            xref = obj.getValue("/Prev");
            if (!xref.Equals(""))
            {
                getObjOffsets(pdf, xref);
            }

            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);
                    objects.Add(getObject(pdf, off, pdf.Length));
                }
            }
        }
Beispiel #16
0
        /**
         * Used to embed PNG images in the PDF document.
         *
         */
        public PNGImage(Stream inputStream)
        {
            ValidatePNG(inputStream);

            List <Chunk> chunks = ProcessPNG(inputStream);

            for (int i = 0; i < chunks.Count; i++)
            {
                Chunk  chunk     = chunks[i];
                String chunkType = System.Text.Encoding.UTF8.GetString(chunk.type);
                if (chunkType.Equals("IHDR"))
                {
                    this.w         = (int)ToUInt32(chunk.GetData(), 0); // Width
                    this.h         = (int)ToUInt32(chunk.GetData(), 4); // Height
                    this.bitDepth  = chunk.GetData()[8];                // Bit Depth
                    this.colorType = chunk.GetData()[9];                // Color Type

                    // Console.WriteLine(
                    //         "Bit Depth == " + chunk.GetData()[8]);
                    // Console.WriteLine(
                    //         "Color Type == " + chunk.GetData()[9]);
                    // Console.WriteLine(chunk.GetData()[10]);
                    // Console.WriteLine(chunk.GetData()[11]);
                    // Console.WriteLine(chunk.GetData()[12]);

                    if (chunk.GetData()[12] == 1)
                    {
                        Console.WriteLine("Interlaced PNG images are not supported.");
                        Console.WriteLine("Convert the image using OptiPNG:\noptipng -i0 -o7 myimage.png\n");
                    }
                }
                else if (chunkType.Equals("IDAT"))
                {
                    iDAT = AppendIdatChunk(iDAT, chunk.GetData());
                }
                else if (chunkType.Equals("PLTE"))
                {
                    pLTE = chunk.GetData();
                    if (pLTE.Length % 3 != 0)
                    {
                        throw new Exception("Incorrect palette length.");
                    }
                }
                else if (chunkType.Equals("gAMA"))
                {
                    // TODO:
                    // Console.WriteLine("gAMA chunk found!");
                }
                else if (chunkType.Equals("tRNS"))
                {
                    // Console.WriteLine("tRNS chunk found!");
                    if (colorType == 3)
                    {
                        tRNS = chunk.GetData();
                    }
                }
                else if (chunkType.Equals("cHRM"))
                {
                    // TODO:
                    // Console.WriteLine("cHRM chunk found!");
                }
                else if (chunkType.Equals("sBIT"))
                {
                    // TODO:
                    // Console.WriteLine("sBIT chunk found!");
                }
                else if (chunkType.Equals("bKGD"))
                {
                    // TODO:
                    // Console.WriteLine("bKGD chunk found!");
                }
            }

            byte[] inflatedImageData = Decompressor.inflate(iDAT);

            byte[] imageData;
            if (colorType == 0)
            {
                // Grayscale Image
                if (bitDepth == 16)
                {
                    imageData = GetImageColorType0BitDepth16(inflatedImageData);
                }
                else if (bitDepth == 8)
                {
                    imageData = GetImageColorType0BitDepth8(inflatedImageData);
                }
                else if (bitDepth == 4)
                {
                    imageData = GetImageColorType0BitDepth4(inflatedImageData);
                }
                else if (bitDepth == 2)
                {
                    imageData = GetImageColorType0BitDepth2(inflatedImageData);
                }
                else if (bitDepth == 1)
                {
                    imageData = GetImageColorType0BitDepth1(inflatedImageData);
                }
                else
                {
                    throw new Exception("Image with unsupported bit depth == " + bitDepth);
                }
            }
            else if (colorType == 6)
            {
                if (bitDepth == 8)
                {
                    imageData = GetImageColorType6BitDepth8(inflatedImageData);
                }
                else
                {
                    throw new Exception("Image with unsupported bit depth == " + bitDepth);
                }
            }
            else
            {
                // Color Image
                if (pLTE == null)
                {
                    // Trucolor Image
                    if (bitDepth == 16)
                    {
                        imageData = GetImageColorType2BitDepth16(inflatedImageData);
                    }
                    else
                    {
                        imageData = GetImageColorType2BitDepth8(inflatedImageData);
                    }
                }
                else
                {
                    // Indexed Image
                    if (bitDepth == 8)
                    {
                        imageData = GetImageColorType3BitDepth8(inflatedImageData);
                    }
                    else if (bitDepth == 4)
                    {
                        imageData = GetImageColorType3BitDepth4(inflatedImageData);
                    }
                    else if (bitDepth == 2)
                    {
                        imageData = GetImageColorType3BitDepth2(inflatedImageData);
                    }
                    else if (bitDepth == 1)
                    {
                        imageData = GetImageColorType3BitDepth1(inflatedImageData);
                    }
                    else
                    {
                        throw new Exception("Image with unsupported bit depth == " + bitDepth);
                    }
                }
            }

            deflatedImageData = Compressor.deflate(imageData);
        }
Beispiel #17
0
        public List<PDFobj> read(Stream inputStream)
        {
            MemoryStream baos = new MemoryStream();
            int ch;
            while ( ( ch = inputStream.ReadByte() ) != -1 ) {
            baos.WriteByte((byte) ch);
            }

            byte[] pdf = baos.ToArray();

            int startxref = indexOfStartXRef(pdf);

            if ( pdf[ startxref + 9 ] == 0x0D &&
                pdf[ startxref + 10 ] == 0x0A ) {
            // Console.WriteLine( "This PDF file is using CR/LF for EOL." );
            CR_LF = true;
            }

            objects = new List< PDFobj >();

            PDFobj obj = getObject( pdf, startxref, pdf.Length );

            String xref = obj.dict[1];

            obj = getObject( pdf, Int32.Parse( xref ), pdf.Length );

            if ( obj.dict[0].Equals( "xref" ) ) {
            for ( int i = 3; i < obj.dict.Count; i += 3 ) {
                String token = obj.dict[i];
                if ( !token.Equals( "trailer" ) ) {
                    int off = Int32.Parse( token );
                    if ( off != 0 ) {
                        objects.Add( getObject( pdf, off, pdf.Length ) );
                    }
                }
            }
            }
            else {
            getObjOffsets( pdf, xref );
            }

            for ( int i = 0; i < objects.Count; i++ ) {
            obj = objects[i];
            int offset = obj.dict.Count - 1;
            if ( obj.dict[offset].Equals( "stream" ) ) {
                obj.setStream( pdf, obj.getLength( objects ) );
            }
            }

            objects2 = new List< PDFobj >();

            for ( int i = 0; i < objects.Count; i++ ) {
            obj = objects[i];
            if ( obj.getValue( "/Type" ).Equals( "/XRef" ) ||
                    !obj.getValue( "/S" ).Equals( "" ) ) {  // Hint Table
                continue;
            }

            if ( obj.getValue( "/Type" ).Equals( "/XObject" ) ||
                    obj.getValue( "/Subtype" ).Equals( "/Type1C" ) ||
                    obj.getValue( "/Subtype" ).Equals( "/CIDFontType0C" ) ) {
                objects2.Add( obj );
                continue;
            }

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

            objects3 = new List< PDFobj >();

            for ( int i = 0; i < objects2.Count; i++ ) {
            obj = objects2[i];
            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());
                    objects3.Add( o3 );
                }
            }
            else {
                objects3.Add( obj );
            }
            }

            objects4 = new List< PDFobj >();

            for ( int i = 0; i < objects.Count; i++ ) {
            obj = objects[i];
            if ( !obj.getValue( "/Type" ).Equals( "/ObjStm" ) &&
                    !obj.getValue( "/Type" ).Equals( "/XRef" ) ) {
                obj.number = Int32.Parse( obj.dict[0] );
                objects4.Add(obj);
            }
            }

            for ( int i = 0; i < objects3.Count; i++ ) {
            obj = objects3[i];
            obj.number = Int32.Parse( obj.dict[0] );
            objects4.Add(obj);
            }

            return objects4;
        }