Beispiel #1
0
 private byte[] GetDecompressedData()
 {
     Decompressor decompressor = new Decompressor(data);
     return decompressor.GetDecompressedData();
 }
Beispiel #2
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 #3
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);
            }
            }
        }
        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 #5
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;
        }