private void AddSoftMask( List <PDFobj> objects, byte[] data, String colorSpace, int bitsPerComponent) { PDFobj obj = new PDFobj(); obj.dict.Add("<<"); obj.dict.Add("/Type"); obj.dict.Add("/XObject"); obj.dict.Add("/Subtype"); obj.dict.Add("/Image"); obj.dict.Add("/Filter"); obj.dict.Add("/FlateDecode"); obj.dict.Add("/Width"); obj.dict.Add(((int)w).ToString()); obj.dict.Add("/Height"); obj.dict.Add(((int)h).ToString()); obj.dict.Add("/ColorSpace"); obj.dict.Add("/" + colorSpace); obj.dict.Add("/BitsPerComponent"); obj.dict.Add(bitsPerComponent.ToString()); obj.dict.Add("/Length"); obj.dict.Add(data.Length.ToString()); obj.dict.Add(">>"); obj.SetStream(data); obj.number = objects.Count + 1; objects.Add(obj); objNumber = obj.number; }
private int GetMaxGSNumber(PDFobj obj) { List <Int32> numbers = new List <Int32>(); foreach (String token in obj.dict) { if (token.StartsWith("/GS")) { numbers.Add(Int32.Parse(token.Substring(3))); } } if (numbers.Count == 0) { return(0); } int maxGSNumber = -1; foreach (Int32 number in numbers) { if (number > maxGSNumber) { maxGSNumber = number; } } return(maxGSNumber); }
private void AddSoftMask( SortedDictionary <Int32, PDFobj> objects, byte[] data, String colorSpace, int bitsPerComponent) { PDFobj obj = new PDFobj(); List <String> dict = obj.GetDict(); dict.Add("<<"); dict.Add("/Type"); dict.Add("/XObject"); dict.Add("/Subtype"); dict.Add("/Image"); dict.Add("/Filter"); dict.Add("/FlateDecode"); dict.Add("/Width"); dict.Add(((int)w).ToString()); dict.Add("/Height"); dict.Add(((int)h).ToString()); dict.Add("/ColorSpace"); dict.Add("/" + colorSpace); dict.Add("/BitsPerComponent"); dict.Add(bitsPerComponent.ToString()); dict.Add("/Length"); dict.Add(data.Length.ToString()); dict.Add(">>"); obj.SetStream(data); obj.number = MaxKey(objects.Keys) + 1; objects.Add(obj.number, obj); objNumber = obj.number; }
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; }
private bool Append(PDFobj obj, StringBuilder sb, int offset) { String token = sb.ToString().Trim(); if (!token.Equals("")) { obj.dict.Add(token); } sb.Length = 0; if (token.Equals("stream") || token.Equals("endobj") || token.Equals("trailer")) { if (token.Equals("stream")) { if (CR_LF) { obj.stream_offset = offset + 1; } else { obj.stream_offset = offset; } } return(false); } return(true); }
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)); } } } } }
private bool Process(PDFobj obj, StringBuilder buf, int off) { string text = buf.ToString().Trim(); if (!text.Equals("")) { obj.dict.Add(text); } buf.Length = 0; if (text.Equals("stream") || text.Equals("endobj") || text.Equals("startxref")) { if (text.Equals("stream")) { if (this.endOfLine == PDF.CR_LF) { obj.stream_offset = off + 1; } else { if (this.endOfLine == PDF.CR || this.endOfLine == PDF.LF) { obj.stream_offset = off; } } } return(true); } return(false); }
private bool Process(PDFobj obj, StringBuilder buf, int off) { String token = buf.ToString().Trim(); if (!token.Equals("")) { obj.dict.Add(token); } buf.Length = 0; if (token.Equals("stream") || token.Equals("endobj") || token.Equals("startxref")) { if (token.Equals("stream")) { if (endOfLine == CR_LF) { obj.stream_offset = off + 1; } else if (endOfLine == CR || endOfLine == LF) { obj.stream_offset = off; } } return(true); } return(false); }
private void AddFontResource( PDFobj obj, List <PDFobj> objects, String fontID, int number) { bool fonts = false; for (int i = 0; i < obj.dict.Count; i++) { if (obj.dict[i].Equals("/Font")) { fonts = true; break; } } if (!fonts) { for (int i = 0; i < obj.dict.Count; i++) { if (obj.dict[i].Equals("/Resources")) { obj.dict.Insert(i + 2, "/Font"); obj.dict.Insert(i + 3, "<<"); obj.dict.Insert(i + 4, ">>"); break; } } } for (int i = 0; i < obj.dict.Count; i++) { if (obj.dict[i].Equals("/Font")) { String token = obj.dict[i + 1]; if (token.Equals("<<")) { obj.dict.Insert(i + 2, "/" + fontID); obj.dict.Insert(i + 3, number.ToString()); obj.dict.Insert(i + 4, "0"); obj.dict.Insert(i + 5, "R"); return; } else if (Char.IsDigit(token[0])) { PDFobj o2 = objects[Int32.Parse(token) - 1]; for (int j = 0; j < o2.dict.Count; j++) { if (o2.dict[j].Equals("<<")) { o2.dict.Insert(j + 1, "/" + fontID); o2.dict.Insert(j + 2, number.ToString()); o2.dict.Insert(j + 3, "0"); o2.dict.Insert(j + 4, "R"); return; } } } } } }
private static void AddToUnicodeCMapObject( SortedDictionary <Int32, PDFobj> objects, Font font) { StringBuilder sb = new StringBuilder(); sb.Append("/CIDInit /ProcSet findresource begin\n"); sb.Append("12 dict begin\n"); sb.Append("begincmap\n"); sb.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>> def\n"); sb.Append("/CMapName /Adobe-Identity def\n"); sb.Append("/CMapType 2 def\n"); sb.Append("1 begincodespacerange\n"); sb.Append("<0000> <FFFF>\n"); sb.Append("endcodespacerange\n"); List <String> list = new List <String>(); StringBuilder buf = new StringBuilder(); for (int cid = 0; cid <= 0xffff; cid++) { int gid = font.unicodeToGID[cid]; if (gid > 0) { buf.Append('<'); buf.Append(ToHexString(gid)); buf.Append("> <"); buf.Append(ToHexString(cid)); buf.Append(">\n"); list.Add(buf.ToString()); buf.Length = 0; if (list.Count == 100) { WriteListToBuffer(list, sb); } } } if (list.Count > 0) { WriteListToBuffer(list, sb); } sb.Append("endcmap\n"); sb.Append("CMapName currentdict /CMap defineresource pop\n"); sb.Append("end\nend"); PDFobj obj = new PDFobj(); List <String> dict = obj.GetDict(); dict.Add("<<"); dict.Add("/Length"); dict.Add(sb.Length.ToString()); dict.Add(">>"); obj.SetStream((new System.Text.UTF8Encoding()).GetBytes(sb.ToString())); obj.number = MaxKey(objects.Keys) + 1; objects.Add(obj.number, obj); font.SetToUnicodeCMapObjNumber(obj.number); }
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; }
/** * Adds new content object before the existing content objects. * The original code was provided by Stefan Ostermann author of ScribMaster and HandWrite Pro. * Additional code to handle PDFs with indirect array of stream objects was written by EDragoev. * * @param content * @param objects */ public void AddPrefixContent(byte[] content, List <PDFobj> objects) { PDFobj obj = new PDFobj(); obj.SetNumber(objects.Count + 1); obj.SetStream(content); objects.Add(obj); String objNumber = obj.number.ToString(); for (int i = 0; i < dict.Count; i++) { if (dict[i].Equals("/Contents")) { i += 1; String token = dict[i]; if (token.Equals("[")) { // Array of content object streams i += 1; dict.Insert(i, "R"); dict.Insert(i, "0"); dict.Insert(i, objNumber); return; } else { // Single content object PDFobj obj2 = objects[Int32.Parse(token) - 1]; if (obj2.data == null && obj2.stream == null) { // This is not a stream object! for (int j = 0; j < obj2.dict.Count; j++) { if (obj2.dict[j].Equals("[")) { j += 1; obj2.dict.Insert(j, "R"); obj2.dict.Insert(j, "0"); obj2.dict.Insert(j, objNumber); return; } } } dict.Insert(i, "["); dict.Insert(i + 4, "]"); i += 1; dict.Insert(i, "R"); dict.Insert(i, "0"); dict.Insert(i, objNumber); return; } } } }
public Font AddFontResource(CoreFont coreFont, SortedDictionary <Int32, PDFobj> objects) { Font font = new Font(coreFont); font.fontID = font.name.Replace('-', '_').ToUpper(); PDFobj obj = new PDFobj(); int maxObjNumber = -1; foreach (int number in objects.Keys) { if (number > maxObjNumber) { maxObjNumber = number; } } obj.number = maxObjNumber + 1; obj.dict.Add("<<"); obj.dict.Add("/Type"); obj.dict.Add("/Font"); obj.dict.Add("/Subtype"); obj.dict.Add("/Type1"); obj.dict.Add("/BaseFont"); obj.dict.Add("/" + font.name); if (!font.name.Equals("Symbol") && !font.name.Equals("ZapfDingbats")) { obj.dict.Add("/Encoding"); obj.dict.Add("/WinAnsiEncoding"); } obj.dict.Add(">>"); objects.Add(obj.number, obj); for (int i = 0; i < dict.Count; i++) { if (dict[i].Equals("/Resources")) { String token = dict[++i]; if (token.Equals("<<")) // Direct resources object { AddFontResource(this, objects, font.fontID, obj.number); } else if (Char.IsDigit(token[0])) // Indirect resources object { AddFontResource(objects[Int32.Parse(token)], objects, font.fontID, obj.number); } } } return(font); }
public int getLength(List <PDFobj> objects, int number) { for (int i = 0; i < objects.Count; i++) { PDFobj obj = objects[i]; if (obj.number == number) { return(Int32.Parse(obj.dict[3])); } } return(0); }
internal int GetLength(List <PDFobj> objects, int number) { for (int i = 0; i < objects.Count; i++) { PDFobj pDFobj = objects[i]; int num = int.Parse(pDFobj.dict[0]); if (num == number) { return(int.Parse(pDFobj.dict[3])); } } return(0); }
/** * * * */ internal int GetLength(List <PDFobj> objects, int number) { for (int i = 0; i < objects.Count; i++) { PDFobj obj = objects[i]; int objNumber = Int32.Parse(obj.dict[0]); if (objNumber == number) { return(Int32.Parse(obj.dict[3])); } } return(0); }
public void AddContent(byte[] content, SortedDictionary<Int32, PDFobj> objects) { PDFobj obj = new PDFobj(); int maxObjNumber = -1; foreach (int number in objects.Keys) { if (number > maxObjNumber) { maxObjNumber = number; } } obj.SetNumber(maxObjNumber + 1); obj.SetStream(content); objects.Add(obj.GetNumber(), obj); int index = -1; bool single = false; for (int i = 0; i < dict.Count; i++) { if (dict[i].Equals("/Contents")) { String str = dict[++i]; if (str.Equals("[")) { for (;;) { str = dict[++i]; if (str.Equals("]")) { index = i; break; } ++i; // 0 ++i; // R } } else { // Single content object index = i; single = true; } break; } } if (single) { dict.Insert(index, "["); dict.Insert(index + 4, "]"); dict.Insert(index + 4, "R"); dict.Insert(index + 4, "0"); dict.Insert(index + 4, obj.number.ToString()); } else { dict.Insert(index, "R"); dict.Insert(index, "0"); dict.Insert(index, obj.number.ToString()); } }
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)); } } }
private static void AddCIDFontDictionaryObject( SortedDictionary <Int32, PDFobj> objects, Font font) { PDFobj obj = new PDFobj(); List <String> dict = obj.GetDict(); dict.Add("<<"); dict.Add("/Type"); dict.Add("/Font"); dict.Add("/Subtype"); dict.Add("/CIDFontType" + (font.cff ? "0" : "2")); dict.Add("/BaseFont"); dict.Add("/" + font.name); dict.Add("/CIDSystemInfo"); dict.Add("<<"); dict.Add("/Registry"); dict.Add("(Adobe)"); dict.Add("/Ordering"); dict.Add("(Identity)"); dict.Add("/Supplement"); dict.Add("0"); dict.Add(">>"); dict.Add("/FontDescriptor"); dict.Add(font.GetFontDescriptorObjNumber().ToString()); dict.Add("0"); dict.Add("R"); dict.Add("/DW"); dict.Add(((Int32) ((1000f / font.unitsPerEm) * font.advanceWidth[0])).ToString()); dict.Add("/W"); dict.Add("["); dict.Add("0"); dict.Add("["); for (int i = 0; i < font.advanceWidth.Length; i++) { dict.Add(((int) ((1000f / font.unitsPerEm) * font.advanceWidth[i])).ToString()); } dict.Add("]"); dict.Add("]"); dict.Add("/CIDToGIDMap"); dict.Add("/Identity"); dict.Add(">>"); obj.number = MaxKey(objects.Keys) + 1; objects.Add(obj.number, obj); font.SetCidFontDictObjNumber(obj.number); }
private static void EmbedFontFile( SortedDictionary <Int32, PDFobj> objects, Font font, Stream inputStream) { int metadataObjNumber = AddMetadataObject(objects, font); PDFobj obj = new PDFobj(); List <String> dict = obj.GetDict(); dict.Add("<<"); dict.Add("/Metadata"); dict.Add(metadataObjNumber.ToString()); dict.Add("0"); dict.Add("R"); dict.Add("/Filter"); dict.Add("/FlateDecode"); dict.Add("/Length"); dict.Add(font.compressed_size.ToString()); if (font.cff) { dict.Add("/Subtype"); dict.Add("/CIDFontType0C"); } else { dict.Add("/Length1"); dict.Add(font.uncompressed_size.ToString()); } dict.Add(">>"); MemoryStream buf2 = new MemoryStream(); byte[] buf = new byte[16384]; int len; while ((len = inputStream.Read(buf, 0, buf.Length)) > 0) { buf2.Write(buf, 0, len); } inputStream.Close(); obj.SetStream(buf2.ToArray()); obj.number = MaxKey(objects.Keys) + 1; objects.Add(obj.number, obj); font.fileObjNumber = obj.number; }
private static void AddCIDFontDictionaryObject(List <PDFobj> objects, Font font) { PDFobj obj = new PDFobj(); obj.dict.Add("<<"); obj.dict.Add("/Type"); obj.dict.Add("/Font"); obj.dict.Add("/Subtype"); obj.dict.Add("/CIDFontType" + (font.cff ? "0" : "2")); obj.dict.Add("/BaseFont"); obj.dict.Add("/" + font.name); obj.dict.Add("/CIDSystemInfo"); obj.dict.Add("<<"); obj.dict.Add("/Registry"); obj.dict.Add("(Adobe)"); obj.dict.Add("/Ordering"); obj.dict.Add("(Identity)"); obj.dict.Add("/Supplement"); obj.dict.Add("0"); obj.dict.Add(">>"); obj.dict.Add("/FontDescriptor"); obj.dict.Add(font.fontDescriptorObjNumber.ToString()); obj.dict.Add("0"); obj.dict.Add("R"); obj.dict.Add("/DW"); obj.dict.Add(((Int32) ((1000f / font.unitsPerEm) * font.advanceWidth[0])).ToString()); obj.dict.Add("/W"); obj.dict.Add("["); obj.dict.Add("0"); obj.dict.Add("["); for (int i = 0; i < font.advanceWidth.Length; i++) { obj.dict.Add(((int) ((1000f / font.unitsPerEm) * font.advanceWidth[i])).ToString()); } obj.dict.Add("]"); obj.dict.Add("]"); obj.dict.Add("/CIDToGIDMap"); obj.dict.Add("/Identity"); obj.dict.Add(">>"); obj.number = objects.Count + 1; objects.Add(obj); font.cidFontDictObjNumber = obj.number; }
private static void EmbedFontFile( List <PDFobj> objects, Font font, Stream inputStream) { int metadataObjNumber = AddMetadataObject(objects, font); PDFobj obj = new PDFobj(); obj.dict.Add("<<"); obj.dict.Add("/Metadata"); obj.dict.Add(metadataObjNumber.ToString()); obj.dict.Add("0"); obj.dict.Add("R"); obj.dict.Add("/Filter"); obj.dict.Add("/FlateDecode"); obj.dict.Add("/Length"); obj.dict.Add(font.compressedSize.ToString()); if (font.cff) { obj.dict.Add("/Subtype"); obj.dict.Add("/CIDFontType0C"); } else { obj.dict.Add("/Length1"); obj.dict.Add(font.uncompressedSize.ToString()); } obj.dict.Add(">>"); MemoryStream buf2 = new MemoryStream(); byte[] buf = new byte[4096]; int len; while ((len = inputStream.Read(buf, 0, buf.Length)) > 0) { buf2.Write(buf, 0, len); } inputStream.Close(); obj.SetStream(buf2.ToArray()); obj.number = objects.Count + 1; objects.Add(obj); font.fileObjNumber = obj.number; }
private void AddResource( String type, PDFobj obj, List <PDFobj> objects, Int32 objNumber) { String tag = type.Equals("/Font") ? "/F" : "/Im"; String number = objNumber.ToString(); String[] list = { tag + number, number, "0", "R" }; for (int i = 0; i < obj.dict.Count; i++) { String token = obj.dict[i]; if (token.Equals(type)) { token = obj.dict[i + 1]; if (token.Equals("<<")) { InsertNewObject(obj.dict, list, type); } else { InsertNewObject(objects[Int32.Parse(token) - 1].dict, list, type); } return; } } // Handle the case where the page originally does not have any font resources. String[] array = { type, "<<", tag + number, number, "0", "R", ">>" }; for (int i = 0; i < obj.dict.Count; i++) { if (obj.dict[i].Equals("/Resources")) { obj.dict.InsertRange(i + 2, array); return; } } for (int i = 0; i < obj.dict.Count; i++) { if (obj.dict[i].Equals("<<")) { obj.dict.InsertRange(i + 1, array); return; } } }
private static int AddMetadataObject( SortedDictionary <Int32, PDFobj> objects, Font font) { StringBuilder sb = new StringBuilder(); sb.Append("<?xpacket begin='\uFEFF' id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"); sb.Append("<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n"); sb.Append("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"); sb.Append("<rdf:Description rdf:about=\"\" xmlns:xmpRights=\"http://ns.adobe.com/xap/1.0/rights/\">\n"); sb.Append("<xmpRights:UsageTerms>\n"); sb.Append("<rdf:Alt>\n"); sb.Append("<rdf:li xml:lang=\"x-default\">\n"); sb.Append(font.info); sb.Append("</rdf:li>\n"); sb.Append("</rdf:Alt>\n"); sb.Append("</xmpRights:UsageTerms>\n"); sb.Append("</rdf:Description>\n"); sb.Append("</rdf:RDF>\n"); sb.Append("</x:xmpmeta>\n"); sb.Append("<?xpacket end=\"w\"?>"); byte[] xml = (new System.Text.UTF8Encoding()).GetBytes(sb.ToString()); // This is the metadata object PDFobj obj = new PDFobj(); List <String> dict = obj.GetDict(); dict.Add("<<"); dict.Add("/Type"); dict.Add("/Metadata"); dict.Add("/Subtype"); dict.Add("/XML"); dict.Add("/Length"); dict.Add(xml.Length.ToString()); dict.Add(">>"); obj.SetStream(xml); obj.number = MaxKey(objects.Keys) + 1; objects.Add(obj.number, obj); return(obj.number); }
private static void AddFontDescriptorObject( SortedDictionary <Int32, PDFobj> objects, Font font) { float factor = 1000f / font.unitsPerEm; PDFobj obj = new PDFobj(); List <String> dict = obj.GetDict(); dict.Add("<<"); dict.Add("/Type"); dict.Add("/FontDescriptor"); dict.Add("/FontName"); dict.Add("/" + font.name); dict.Add("/FontFile" + (font.cff ? "3" : "2")); dict.Add(font.fileObjNumber.ToString()); dict.Add("0"); dict.Add("R"); dict.Add("/Flags"); dict.Add("32"); dict.Add("/FontBBox"); dict.Add("["); dict.Add(((Int32)(font.bBoxLLx * factor)).ToString()); dict.Add(((Int32)(font.bBoxLLy * factor)).ToString()); dict.Add(((Int32)(font.bBoxURx * factor)).ToString()); dict.Add(((Int32)(font.bBoxURy * factor)).ToString()); dict.Add("]"); dict.Add("/Ascent"); dict.Add(((Int32)(font.ascent * factor)).ToString()); dict.Add("/Descent"); dict.Add(((Int32)(font.descent * factor)).ToString()); dict.Add("/ItalicAngle"); dict.Add("0"); dict.Add("/CapHeight"); dict.Add(((Int32)(font.capHeight * factor)).ToString()); dict.Add("/StemV"); dict.Add("79"); dict.Add(">>"); obj.number = MaxKey(objects.Keys) + 1; objects.Add(obj.number, obj); font.SetFontDescriptorObjNumber(obj.number); }
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)); } } } } }
public PDFobj GetResourcesObject(SortedDictionary <Int32, PDFobj> objects) { for (int i = 0; i < dict.Count; i++) { if (dict[i].Equals("/Resources")) { String token = dict[i + 1]; if (token.Equals("<<")) { PDFobj obj = new PDFobj(); obj.dict.Add("0"); obj.dict.Add("0"); obj.dict.Add("obj"); obj.dict.Add(token); int level = 1; i++; while (i < dict.Count && level > 0) { token = dict[i]; obj.dict.Add(token); if (token.Equals("<<")) { level++; } else if (token.Equals(">>")) { level--; } i++; } return(obj); } return(objects[Int32.Parse(token)]); } } return(null); }
private static void AddFontDescriptorObject(List <PDFobj> objects, Font font) { PDFobj obj = new PDFobj(); obj.dict.Add("<<"); obj.dict.Add("/Type"); obj.dict.Add("/FontDescriptor"); obj.dict.Add("/FontName"); obj.dict.Add("/" + font.name); obj.dict.Add("/FontFile" + (font.cff ? "3" : "2")); obj.dict.Add(font.fileObjNumber.ToString()); obj.dict.Add("0"); obj.dict.Add("R"); obj.dict.Add("/Flags"); obj.dict.Add("32"); obj.dict.Add("/FontBBox"); obj.dict.Add("["); obj.dict.Add(font.bBoxLLx.ToString()); obj.dict.Add(font.bBoxLLy.ToString()); obj.dict.Add(font.bBoxURx.ToString()); obj.dict.Add(font.bBoxURy.ToString()); obj.dict.Add("]"); obj.dict.Add("/Ascent"); obj.dict.Add(font.fontAscent.ToString()); obj.dict.Add("/Descent"); obj.dict.Add(font.fontDescent.ToString()); obj.dict.Add("/ItalicAngle"); obj.dict.Add("0"); obj.dict.Add("/CapHeight"); obj.dict.Add(font.capHeight.ToString()); obj.dict.Add("/StemV"); obj.dict.Add("79"); obj.dict.Add(">>"); obj.number = objects.Count + 1; objects.Add(obj); font.fontDescriptorObjNumber = obj.number; }
internal static void Register(List <PDFobj> objects, Font font, Stream inputStream) { FontStream1.GetFontData(font, inputStream); EmbedFontFile(objects, font, inputStream); AddFontDescriptorObject(objects, font); AddCIDFontDictionaryObject(objects, font); AddToUnicodeCMapObject(objects, font); // Type0 Font Dictionary PDFobj obj = new PDFobj(); obj.dict.Add("<<"); obj.dict.Add("/Type"); obj.dict.Add("/Font"); obj.dict.Add("/Subtype"); obj.dict.Add("/Type0"); obj.dict.Add("/BaseFont"); obj.dict.Add("/" + font.name); obj.dict.Add("/Encoding"); obj.dict.Add("/Identity-H"); obj.dict.Add("/DescendantFonts"); obj.dict.Add("["); obj.dict.Add(font.cidFontDictObjNumber.ToString()); obj.dict.Add("0"); obj.dict.Add("R"); obj.dict.Add("]"); obj.dict.Add("/ToUnicode"); obj.dict.Add(font.toUnicodeCMapObjNumber.ToString()); obj.dict.Add("0"); obj.dict.Add("R"); obj.dict.Add(">>"); obj.number = objects.Count + 1; font.objNumber = obj.number; objects.Add(obj); }
private void AddFontResource( PDFobj obj, SortedDictionary <Int32, PDFobj> objects, String fontID, int number) { for (int i = 0; i < obj.dict.Count; i++) { String token = null; if (obj.dict[i].Equals("/Font")) { token = obj.dict[++i]; if (token.Equals("<<")) { obj.dict.Insert(++i, "/" + fontID); obj.dict.Insert(++i, number.ToString()); obj.dict.Insert(++i, "0"); obj.dict.Insert(++i, "R"); break; } else if (Char.IsDigit(token[0])) { PDFobj o2 = objects[Int32.Parse(token)]; for (int j = 0; j < o2.dict.Count; j++) { token = o2.dict[j]; if (token.Equals("<<")) { o2.dict.Insert(++j, "/" + fontID); o2.dict.Insert(++j, number.ToString()); o2.dict.Insert(++j, "0"); o2.dict.Insert(++j, "R"); break; } } } } } }
public Font AddFontResource(CoreFont coreFont, SortedDictionary<Int32, PDFobj> objects) { Font font = new Font(coreFont); font.fontID = font.name.Replace('-', '_').ToUpper(); PDFobj obj = new PDFobj(); int maxObjNumber = -1; foreach (int number in objects.Keys) { if (number > maxObjNumber) { maxObjNumber = number; } } obj.number = maxObjNumber + 1; obj.dict.Add("<<"); obj.dict.Add("/Type"); obj.dict.Add("/Font"); obj.dict.Add("/Subtype"); obj.dict.Add("/Type1"); obj.dict.Add("/BaseFont"); obj.dict.Add("/" + font.name); if (!font.name.Equals("Symbol") && !font.name.Equals("ZapfDingbats")) { obj.dict.Add("/Encoding"); obj.dict.Add("/WinAnsiEncoding"); } obj.dict.Add(">>"); objects.Add(obj.number, obj); for (int i = 0; i < dict.Count; i++) { if (dict[i].Equals("/Resources")) { String token = dict[++i]; if (token.Equals("<<")) { // Direct resources object AddFontResource(this, objects, font.fontID, obj.number); } else if (Char.IsDigit(token[0])) { // Indirect resources object AddFontResource(objects[Int32.Parse(token)], objects, font.fontID, obj.number); } } } return font; }
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); objNumber = pdf.objNumber; }
private PDFobj getObject( byte[] buf, int off, int end ) { PDFobj obj = new PDFobj( off ); StringBuilder sb = new StringBuilder(); int n = 0; char c1 = ' '; while ( true ) { if ( off == end ) { Append( obj, sb, off ); break; } char c2 = (char) buf[off++]; if ( c2 == '(' ) { if ( n == 0 ) { if ( !Append( obj, sb, off ) ) break; } sb.Append( c2 ); ++n; } else if ( c2 == ')' ) { sb.Append( c2 ); --n; if ( n == 0 ) { if ( !Append( obj, sb, off ) ) break; } } else if ( n > 0 ) { sb.Append( c2 ); } else if ( c2 == 0x00 // Null || c2 == 0x09 // Horizontal Tab || c2 == 0x0A // Line Feed (LF) || c2 == 0x0C // Form Feed || c2 == 0x0D // Carriage Return (CR) || c2 == 0x20 ) { // Space if ( !Append( obj, sb, off ) ) break; c1 = ' '; } else if ( c2 == '/' ) { if ( !Append( obj, sb, off ) ) break; sb.Append( c2 ); c1 = c2; } else if ( c2 == '<' || c2 == '>' ) { if ( c2 != c1 ) { if ( !Append( obj, sb, off ) ) break; sb.Append( c2 ); c1 = c2; } else { sb.Append( c2 ); if ( !Append( obj, sb, off ) ) break; c1 = ' '; } } else if ( c2 == '[' || c2 == ']' ) { if ( !Append( obj, sb, off ) ) break; obj.dict.Add( c2.ToString() ); c1 = c2; } else if ( c2 == '{' || c2 == '}' ) { if ( !Append( obj, sb, off ) ) break; obj.dict.Add( c2.ToString() ); c1 = c2; } else if ( c2 == '%' ) { if ( c2 != c1 ) { if ( !Append( obj, sb, off ) ) break; sb.Append( c2 ); c1 = c2; } else { sb.Append( c2 ); if ( !Append( obj, sb, off ) ) break; c1 = ' '; } } else { sb.Append( c2 ); c1 = c2; } } return obj; }
private void AddFontResource( PDFobj obj, SortedDictionary<Int32, PDFobj> objects, String fontID, int number) { for (int i = 0; i < obj.dict.Count; i++) { String token = null; if (obj.dict[i].Equals("/Font")) { token = obj.dict[++i]; if (token.Equals("<<")) { obj.dict.Insert(++i, "/" + fontID); obj.dict.Insert(++i, number.ToString()); obj.dict.Insert(++i, "0"); obj.dict.Insert(++i, "R"); break; } else if (Char.IsDigit(token[0])) { PDFobj o2 = objects[Int32.Parse(token)]; for (int j = 0; j < o2.dict.Count; j++) { token = o2.dict[j]; if (token.Equals("<<")) { o2.dict.Insert(++j, "/" + fontID); o2.dict.Insert(++j, number.ToString()); o2.dict.Insert(++j, "0"); o2.dict.Insert(++j, "R"); break; } } } } } }
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); } } } }
private PDFobj GetObject(byte[] buf, int off, int len) { PDFobj obj = new PDFobj(off); StringBuilder token = new StringBuilder(); int p = 0; char c1 = ' '; bool done = false; while (!done && off < len) { char c2 = (char) buf[off++]; if (c2 == '(') { if (p == 0) { done = Process(obj, token, off); } if (!done) { token.Append(c2); ++p; } } else if (c2 == ')') { token.Append(c2); --p; if (p == 0) { done = Process(obj, token, off); } } else if (c2 == 0x00 // Null || c2 == 0x09 // Horizontal Tab || c2 == 0x0A // Line Feed (LF) || c2 == 0x0C // Form Feed || c2 == 0x0D // Carriage Return (CR) || c2 == 0x20) { // Space done = Process(obj, token, off); if (!done) { c1 = ' '; } } else if (c2 == '/') { done = Process(obj, token, off); if (!done) { token.Append(c2); c1 = c2; } } else if (c2 == '<' || c2 == '>' || c2 == '%') { if (c2 != c1) { done = Process(obj, token, off); if (!done) { token.Append(c2); c1 = c2; } } else { token.Append(c2); done = Process(obj, token, off); if (!done) { c1 = ' '; } } } else if (c2 == '[' || c2 == ']' || c2 == '{' || c2 == '}') { done = Process(obj, token, off); if (!done) { obj.dict.Add(c2.ToString()); c1 = c2; } } else { token.Append(c2); if (p == 0) { c1 = c2; } } } return obj; }
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); } } }
private void GetPageObjects(PDFobj pdfObj, SortedDictionary<Int32, PDFobj> objects, List<PDFobj> pages) { List<Int32> kids = pdfObj.GetObjectNumbers("/Kids"); foreach (Int32 number in kids) { PDFobj obj = objects[number]; if (IsPageObject(obj)) { pages.Add(obj); } else { GetPageObjects(obj, objects, pages); } } }
private bool IsPageObject(PDFobj obj) { bool isPage = false; for (int i = 0; i < obj.dict.Count; i++) { if (obj.dict[i].Equals("/Type") && obj.dict[i + 1].Equals("/Page")) { isPage = true; } } return isPage; }
public void AddObjects(SortedDictionary<Int32, PDFobj> objects) { foreach (PDFobj obj in objects.Values) { if (obj.GetValue("/Type").Equals("/Pages") && obj.GetValue("/Parent").Equals("")) { this.pagesObjNumber = Int32.Parse(obj.dict[0]); } } int maxObjNumber = -1; foreach (int number in objects.Keys) { if (number > maxObjNumber) { maxObjNumber = number; } } for (int i = 1; i < maxObjNumber; i++) { if (!objects.ContainsKey(i)) { PDFobj obj = new PDFobj(); obj.number = i; objects.Add(obj.number, obj); } } foreach (PDFobj obj in objects.Values) { objNumber = obj.number; objOffset.Add(byte_count); if (obj.offset == 0) { Append(obj.number); Append(" 0 obj\n"); if (obj.dict != null) { for (int i = 0; i < obj.dict.Count; i++) { Append(obj.dict[i]); Append(' '); } } if (obj.stream != null) { Append("<< /Length "); Append(obj.stream.Length); Append(" >>"); Append("\nstream\n"); for (int i = 0; i < obj.stream.Length; i++) { Append(obj.stream[i]); } Append("\nendstream\n"); } Append("endobj\n"); continue; } int n = obj.dict.Count; String token = null; for (int i = 0; i < n; i++) { token = obj.dict[i]; Append(token); if (i < (n - 1)) { Append(' '); } else { Append('\n'); } } if (obj.stream != null) { for (int i = 0; i < obj.stream.Length; i++) { Append(obj.stream[i]); } Append("\nendstream\n"); } if (!token.Equals("endobj")) { Append("endobj\n"); } } }
private bool Process(PDFobj obj, StringBuilder buf, int off) { String token = buf.ToString().Trim(); if (!token.Equals("")) { obj.dict.Add(token); } buf.Length = 0; if (token.Equals("stream") || token.Equals("endobj") || token.Equals("startxref")) { if (token.Equals("stream")) { if (endOfLine == CR_LF) { obj.stream_offset = off + 1; } else if (endOfLine == CR || endOfLine == LF) { obj.stream_offset = off; } } return true; } return false; }
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); } }
private bool Append(PDFobj obj, StringBuilder sb, int offset) { String token = sb.ToString().Trim(); if ( !token.Equals( "" ) ) { obj.dict.Add( token ); } sb.Length = 0; if ( token.Equals( "stream" ) || token.Equals( "endobj" ) || token.Equals( "trailer" ) ) { if ( token.Equals( "stream" ) ) { if ( CR_LF ) { obj.stream_offset = offset + 1; } else { obj.stream_offset = offset; } } return false; } return true; }