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)); } } }
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); }