private PdfDictionary getCIDFont(PdfIndirectReference fontDescriptor, IntHashtable cjkTag) { PdfDictionary dic = new PdfDictionary(PdfName.FONT); dic.put(PdfName.SUBTYPE, new PdfName("CIDFontType0")); dic.put(new PdfName("BaseFont"), new PdfName(fontName + style)); dic.put(new PdfName("FontDescriptor"), fontDescriptor); int[] keys = cjkTag.toOrderedKeys(); string w = convertToHCIDMetrics(keys, hMetrics); if (w != null) { dic.put(new PdfName("W"), new PdfLiteral(w)); } if (vertical) { w = convertToVCIDMetrics(keys, vMetrics, hMetrics);; if (w != null) { dic.put(new PdfName("W2"), new PdfLiteral(w)); } } PdfDictionary cdic = new PdfDictionary(); cdic.put(PdfName.REGISTRY, new PdfString((string)fontDesc["Registry"], null)); cdic.put(PdfName.ORDERING, new PdfString((string)fontDesc["Ordering"], null)); cdic.put(PdfName.SUPPLEMENT, new PdfLiteral((string)fontDesc["Supplement"])); dic.put(new PdfName("CIDSystemInfo"), cdic); return(dic); }
/** Launchs an application or a document. * @param application the application to be launched or the document to be opened or printed. * @param parameters (Windows-specific) A parameter string to be passed to the application. * It can be <CODE>null</CODE>. * @param operation (Windows-specific) the operation to perform: "open" - Open a document, * "print" - Print a document. * It can be <CODE>null</CODE>. * @param defaultDir (Windows-specific) the default directory in standard DOS syntax. * It can be <CODE>null</CODE>. */ public PdfAction(string application, string parameters, string operation, string defaultDir) { put(PdfName.S, PdfName.LAUNCH); if (parameters == null && operation == null && defaultDir == null) { put(PdfName.F, new PdfString(application)); } else { PdfDictionary dic = new PdfDictionary(); dic.put(PdfName.F, new PdfString(application)); if (parameters != null) { dic.put(PdfName.P, new PdfString(parameters)); } if (operation != null) { dic.put(PdfName.O, new PdfString(operation)); } if (defaultDir != null) { dic.put(PdfName.D, new PdfString(defaultDir)); } put(PdfName.WIN, dic); } }
/** Generates the CIDFontTyte2 dictionary. * @param fontDescriptor the indirect reference to the font descriptor * @param subsetPrefix the subset prefix * @param metrics the horizontal width metrics * @return a stream */ private PdfDictionary getCIDFontType2(PdfIndirectReference fontDescriptor, string subsetPrefix, Object[] metrics) { PdfDictionary dic = new PdfDictionary(PdfName.FONT); dic.put(PdfName.SUBTYPE, new PdfName("CIDFontType2")); dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix + fontName)); dic.put(PdfName.FONTDESCRIPTOR, fontDescriptor); dic.put(new PdfName("CIDToGIDMap"), new PdfName("Identity")); PdfDictionary cdic = new PdfDictionary(); cdic.put(PdfName.REGISTRY, new PdfString("Adobe")); cdic.put(PdfName.ORDERING, new PdfString("Identity")); cdic.put(PdfName.SUPPLEMENT, new PdfNumber(0)); dic.put(new PdfName("CIDSystemInfo"), cdic); if (!vertical) { dic.put(new PdfName("DW"), new PdfNumber(1000)); StringBuilder buf = new StringBuilder("["); int lastNumber = -10; bool firstTime = true; for (int k = 0; k < metrics.Length; ++k) { int[] metric = (int[])metrics[k]; if (metric[1] == 1000) { continue; } int m = metric[0]; if (m == lastNumber + 1) { buf.Append(" ").Append(metric[1]); } else { if (!firstTime) { buf.Append("]"); } firstTime = false; buf.Append(m).Append("[").Append(metric[1]); } lastNumber = m; } if (buf.Length > 1) { buf.Append("]]"); dic.put(PdfName.W, new PdfLiteral(buf.ToString())); } } return(dic); }
/** Generates the font dictionary. * @param descendant the descendant dictionary * @param subsetPrefix the subset prefix * @param toUnicode the ToUnicode stream * @return the stream */ private PdfDictionary getFontBaseType(PdfIndirectReference descendant, string subsetPrefix, PdfIndirectReference toUnicode) { PdfDictionary dic = new PdfDictionary(PdfName.FONT); dic.put(PdfName.SUBTYPE, new PdfName("Type0")); dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix + fontName)); dic.put(PdfName.ENCODING, new PdfName(encoding)); dic.put(new PdfName("DescendantFonts"), new PdfArray(descendant)); if (toUnicode != null) { dic.put(new PdfName("ToUnicode"), toUnicode); } return(dic); }
protected PdfDictionary readDictionary() { PdfDictionary dic = new PdfDictionary(); while (true) { tokens.nextValidToken(); if (tokens.TokenType == PRTokeniser.TK_END_DIC) { break; } if (tokens.TokenType != PRTokeniser.TK_NAME) { tokens.throwError("Dictionary key is not a name."); } PdfName name = new PdfName(tokens.StringValue); PdfObject obj = readPRObject(); int type = obj.Type; if (-type == PRTokeniser.TK_END_DIC) { tokens.throwError("Unexpected '>>'"); } if (-type == PRTokeniser.TK_END_ARRAY) { tokens.throwError("Unexpected ']'"); } dic.put(name, obj); } return(dic); }
protected void iteratePages(PdfDictionary page) { PdfName type = (PdfName)getPdfObject(page.get(PdfName.TYPE)); if (type.Equals(PdfName.PAGE)) { PdfDictionary dic = (PdfDictionary)pageInh[pageInh.Count - 1]; foreach (PdfName key in dic.Keys) { if (page.get(key) == null) { page.put(key, dic.get(key)); } } pages[pagesCount++] = page; } else { pushPageAttributes(page); PdfArray kidsPR = (PdfArray)getPdfObject(page.get(PdfName.KIDS)); ArrayList kids = kidsPR.ArrayList; for (int k = 0; k < kids.Count; ++k) { PdfDictionary kid = (PdfDictionary)getPdfObject((PdfObject)kids[k]); iteratePages(kid); } popPageAttributes(); } }
private PdfDictionary getFontBaseType(PdfIndirectReference CIDFont) { PdfDictionary dic = new PdfDictionary(PdfName.FONT); dic.put(PdfName.SUBTYPE, new PdfName("Type0")); string name = fontName; if (style.Length > 0) { name += "-" + style.Substring(1); } name += "-" + CMap; dic.put(new PdfName("BaseFont"), new PdfName(name)); dic.put(new PdfName("Encoding"), new PdfName(CMap)); dic.put(new PdfName("DescendantFonts"), new PdfArray(CIDFont)); return(dic); }
public static PdfAction createSubmitForm(string file, Object[] names, int flags) { PdfAction action = new PdfAction(); action.put(PdfName.S, PdfName.SUBMITFORM); PdfDictionary dic = new PdfDictionary(); dic.put(PdfName.F, new PdfString(file)); dic.put(PdfName.FS, PdfName.URL); action.put(PdfName.F, dic); if (names != null) { action.put(PdfName.FIELDS, buildArray(names)); } action.put(PdfName.FLAGS, new PdfNumber(flags)); return(action); }
public void setMKIconFit(PdfName scale, PdfName scalingType, float leftoverLeft, float leftoverBottom) { PdfDictionary dic = new PdfDictionary(); if (!scale.Equals(PdfName.A)) { dic.put(PdfName.SW, scale); } if (!scalingType.Equals(PdfName.P)) { dic.put(PdfName.S, scalingType); } if (leftoverLeft != 0.5f || leftoverBottom != 0.5f) { PdfArray array = new PdfArray(new PdfNumber(leftoverLeft)); array.Add(new PdfNumber(leftoverBottom)); dic.put(PdfName.A, array); } getMK().put(PdfName.IF, dic); }
private PdfDictionary getFontDescriptor() { PdfDictionary dic = new PdfDictionary(new PdfName("FontDescriptor")); dic.put(new PdfName("Ascent"), new PdfLiteral((string)fontDesc["Ascent"])); dic.put(new PdfName("CapHeight"), new PdfLiteral((string)fontDesc["CapHeight"])); dic.put(new PdfName("Descent"), new PdfLiteral((string)fontDesc["Descent"])); dic.put(new PdfName("Flags"), new PdfLiteral((string)fontDesc["Flags"])); dic.put(new PdfName("FontBBox"), new PdfLiteral((string)fontDesc["FontBBox"])); dic.put(new PdfName("FontName"), new PdfName(fontName + style)); dic.put(new PdfName("ItalicAngle"), new PdfLiteral((string)fontDesc["ItalicAngle"])); dic.put(new PdfName("StemV"), new PdfLiteral((string)fontDesc["StemV"])); PdfDictionary pdic = new PdfDictionary(); pdic.put(PdfName.PANOSE, new PdfString((string)fontDesc["Panose"], null)); dic.put(new PdfName("Style"), pdic); return(dic); }
/** Generates the font descriptor for this font or <CODE>null</CODE> if it is * one of the 14 built in fonts. * @param fontStream the indirect reference to a PdfStream containing the font or <CODE>null</CODE> * @return the PdfDictionary containing the font descriptor or <CODE>null</CODE> */ public PdfDictionary getFontDescriptor(PdfIndirectReference fontStream) { if (builtinFont) { return(null); } PdfDictionary dic = new PdfDictionary(new PdfName("FontDescriptor")); dic.put(new PdfName("Ascent"), new PdfNumber(Ascender)); dic.put(new PdfName("CapHeight"), new PdfNumber(CapHeight)); dic.put(new PdfName("Descent"), new PdfNumber(Descender)); dic.put(new PdfName("FontBBox"), new PdfRectangle(llx, lly, urx, ury)); dic.put(new PdfName("FontName"), new PdfName(FontName)); dic.put(new PdfName("ItalicAngle"), new PdfNumber(ItalicAngle)); dic.put(new PdfName("StemV"), new PdfNumber(StdVW)); if (fontStream != null) { dic.put(new PdfName("FontFile"), fontStream); } int flags = 0; if (IsFixedPitch) { flags |= 1; } flags |= fontSpecific ? 4 : 32; if (ItalicAngle < 0) { flags |= 64; } if (FontName.IndexOf("Caps") >= 0 || FontName.EndsWith("SC")) { flags |= 131072; } if (Weight.Equals("Bold")) { flags |= 262144; } dic.put(new PdfName("Flags"), new PdfNumber(flags)); return(dic); }
protected void setColorSpace(Color color) { int type = ExtendedColor.getType(color); PdfObject colorSpace = null; switch (type) { case ExtendedColor.TYPE_GRAY: { colorSpace = PdfName.DEVICEGRAY; break; } case ExtendedColor.TYPE_CMYK: { colorSpace = PdfName.DEVICECMYK; break; } case ExtendedColor.TYPE_SEPARATION: { SpotColor spot = (SpotColor)color; colorDetails = writer.addSimple(spot.getPdfSpotColor()); colorSpace = colorDetails.IndirectReference; break; } case ExtendedColor.TYPE_PATTERN: case ExtendedColor.TYPE_SHADING: { throwColorSpaceError(); break; } default: colorSpace = PdfName.DEVICERGB; break; } shading.put(PdfName.COLORSPACE, colorSpace); }
protected void pushPageAttributes(PdfDictionary nodePages) { PdfDictionary dic = new PdfDictionary(); if (pageInh.Count != 0) { dic.putAll((PdfDictionary)pageInh[pageInh.Count - 1]); } for (int k = 0; k < pageInhCandidates.Length; ++k) { PdfObject obj = nodePages.get(pageInhCandidates[k]); if (obj != null) { dic.put(pageInhCandidates[k], obj); } } pageInh.Add(dic); }
internal static void mergeResources(PdfDictionary result, PdfDictionary source) { PdfDictionary dic = null; PdfDictionary res = null; PdfName target = null; for (int k = 0; k < mergeTarget.Length; ++k) { target = mergeTarget[k]; if ((dic = (PdfDictionary)source.get(target)) != null) { if ((res = (PdfDictionary)result.get(target)) == null) { res = new PdfDictionary(); } res.merge(dic); result.put(target, res); } } }
internal PdfStream getFormXObject(int pageNumber) { PdfDictionary page = pages[pageNumber - 1]; PdfObject contents = reader.getPdfObject(page.get(PdfName.CONTENTS)); int length = 0; int offset = 0; PdfDictionary dic = new PdfDictionary(); MemoryStream bout = null; ArrayList filters = null; if (contents != null) { if (contents.Type == PdfObject.STREAM) { PRStream stream = (PRStream)contents; length = stream.Length; offset = stream.Offset; dic.putAll(stream); } else { PdfArray array = (PdfArray)contents; ArrayList list = array.ArrayList; bout = new MemoryStream(); for (int k = 0; k < list.Count; ++k) { PRStream stream = (PRStream)reader.getPdfObject((PdfObject)list[k]); PdfObject filter = stream.get(PdfName.FILTER); byte[] b = new byte[stream.Length]; file.seek(stream.Offset); file.readFully(b); filters = new ArrayList(); if (filter != null) { if (filter.Type == PdfObject.NAME) { filters.Add(filter); } else if (filter.Type == PdfObject.ARRAY) { filters = ((PdfArray)filter).ArrayList; } } string name; for (int j = 0; j < filters.Count; ++j) { name = ((PdfName)filters[j]).ToString(); if (name.Equals("/FlateDecode") || name.Equals("/Fl")) { b = PdfReader.FlateDecode(b); } else if (name.Equals("/ASCIIHexDecode") || name.Equals("/AHx")) { b = PdfReader.ASCIIHexDecode(b); } else if (name.Equals("/ASCII85Decode") || name.Equals("/A85")) { b = PdfReader.ASCII85Decode(b); } else if (name.Equals("/LZWDecode")) { b = PdfReader.LZWDecode(b); } else { throw new IOException("The filter " + name + " is not supported."); } } bout.Write(b, 0, b.Length); if (k != list.Count - 1) { bout.WriteByte((byte)'\n'); } } } } dic.put(PdfName.RESOURCES, reader.getPdfObject(page.get(PdfName.RESOURCES))); dic.put(PdfName.TYPE, PdfName.XOBJECT); dic.put(PdfName.SUBTYPE, PdfName.FORM); dic.put(PdfName.BBOX, new PdfRectangle(((PdfImportedPage)importedPages[pageNumber]).BoundingBox)); dic.put(PdfName.MATRIX, IDENTITYMATRIX); dic.put(PdfName.FORMTYPE, ONE); PRStream str; if (bout == null) { str = new PRStream(reader, offset); str.putAll(dic); str.Length = length; } else { str = new PRStream(reader, bout.ToArray()); str.putAll(dic); } return(str); }
// constructor /** * Constructs a <CODE>PdfImage</CODE>-object. * * @param image the <CODE>Image</CODE>-object * @param name the <CODE>PdfName</CODE> for this image * @throws BadPdfFormatException on error */ public PdfImage(Image image, string name, PdfIndirectReference maskRef) : base() { this.name = new PdfName(name); put(PdfName.TYPE, PdfName.XOBJECT); put(PdfName.SUBTYPE, PdfName.IMAGE); put(PdfName.NAME, this.name); put(PdfName.WIDTH, new PdfNumber(image.Width)); put(PdfName.HEIGHT, new PdfNumber(image.Height)); if (maskRef != null) { put(PdfName.MASK, maskRef); } if (image.isMask() && image.isInvertMask()) { put(PdfName.DECODE, new PdfLiteral("[1 0]")); } if (image.isInterpolation()) { put(PdfName.INTERPOLATE, PdfBoolean.PDFTRUE); } Stream istr = null; try { // Raw Image data if (image.isImgRaw()) { // will also have the CCITT parameters int colorspace = image.Colorspace; int[] transparency = image.Transparency; if (transparency != null && !image.isMask() && maskRef == null) { string s = "["; for (int k = 0; k < transparency.Length; ++k) { s += transparency[k] + " "; } s += "]"; put(PdfName.MASK, new PdfLiteral(s)); } bytes = image.RawData; put(PdfName.LENGTH, new PdfNumber(bytes.Length)); int bpc = image.Bpc; if (bpc > 0xff) { if (!image.isMask()) { put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); } put(PdfName.BITSPERCOMPONENT, new PdfNumber(1)); put(PdfName.FILTER, PdfName.CCITTFAXDECODE); int k = bpc - Element.CCITTG3_1D; PdfDictionary decodeparms = new PdfDictionary(); if (k != 0) { decodeparms.put(PdfName.K, new PdfNumber(k)); } if ((colorspace & Element.CCITT_BLACKIS1) != 0) { decodeparms.put(PdfName.BLACKIS1, PdfBoolean.PDFTRUE); } if ((colorspace & Element.CCITT_ENCODEDBYTEALIGN) != 0) { decodeparms.put(PdfName.ENCODEDBYTEALIGN, PdfBoolean.PDFTRUE); } if ((colorspace & Element.CCITT_ENDOFLINE) != 0) { decodeparms.put(PdfName.ENDOFLINE, PdfBoolean.PDFTRUE); } if ((colorspace & Element.CCITT_ENDOFBLOCK) != 0) { decodeparms.put(PdfName.ENDOFBLOCK, PdfBoolean.PDFFALSE); } decodeparms.put(PdfName.COLUMNS, new PdfNumber(image.Width)); decodeparms.put(PdfName.ROWS, new PdfNumber(image.Height)); put(PdfName.DECODEPARMS, decodeparms); } else { if (!image.isMask()) { switch (colorspace) { case 1: put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); break; case 3: put(PdfName.COLORSPACE, PdfName.DEVICERGB); break; case 4: default: put(PdfName.COLORSPACE, PdfName.DEVICECMYK); break; } } put(PdfName.BITSPERCOMPONENT, new PdfNumber(image.Bpc)); try { flateCompress(); } catch (PdfException pe) { throw pe; } } if (image.isMask()) { put(PdfName.IMAGEMASK, PdfBoolean.PDFTRUE); } return; } // GIF, JPEG or PNG string errorID; if (image.RawData == null) { WebRequest w = WebRequest.Create(image.Url); istr = w.GetResponse().GetResponseStream(); errorID = image.Url.ToString(); } else { istr = new MemoryStream(image.RawData); errorID = "Byte array"; } streamBytes = new MemoryStream(); int i = 0; switch (image.Type) { case Element.PNG: put(PdfName.FILTER, PdfName.FLATEDECODE); for (int j = 0; j < Png.PNGID.Length; j++) { if (Png.PNGID[j] != istr.ReadByte()) { throw new BadPdfFormatException(errorID + " is not a PNG file."); } } int colorType = 0; while (true) { int len = Png.getInt(istr); string marker = Png.getstring(istr); if (Png.IDAT.Equals(marker)) { transferBytes(istr, streamBytes, len); Png.getInt(istr); } else if (Png.tRNS.Equals(marker) && !image.isMask() && maskRef == null) { switch (colorType) { case 0: if (len >= 2) { len -= 2; int gray = Png.getWord(istr); put(PdfName.MASK, new PdfLiteral("[" + gray + " " + gray + "]")); } break; case 2: if (len >= 6) { len -= 6; int red = Png.getWord(istr); int green = Png.getWord(istr); int blue = Png.getWord(istr); put(PdfName.MASK, new PdfLiteral("[" + red + " " + red + " " + green + " " + green + " " + blue + " " + blue + "]")); } break; case 3: if (len > 0) { int idx = 0; while (len-- != 0) { if (istr.ReadByte() == 0) { put(PdfName.MASK, new PdfLiteral("[" + idx + " " + idx + "]")); break; } ++idx; } } break; } Image.skip(istr, len + 4); } else if (Png.IHDR.Equals(marker)) { int w = Png.getInt(istr); int h = Png.getInt(istr); int bitDepth = istr.ReadByte(); if (bitDepth == 16) { throw new BadPdfFormatException(errorID + " Bit depth 16 is not suported."); } put(PdfName.BITSPERCOMPONENT, new PdfNumber(bitDepth)); colorType = istr.ReadByte(); if (!(colorType == 0 || colorType == 2 || colorType == 3)) { throw new BadPdfFormatException(errorID + " Colortype " + colorType + " is not suported."); } if (colorType == 0) { put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); } else if (colorType == 2) { put(PdfName.COLORSPACE, PdfName.DEVICERGB); } int compressionMethod = istr.ReadByte(); int filterMethod = istr.ReadByte(); int interlaceMethod = istr.ReadByte(); if (interlaceMethod != 0) { throw new BadPdfFormatException(errorID + " Interlace method " + interlaceMethod + " is not suported."); } PdfDictionary decodeparms = new PdfDictionary(); decodeparms.put(PdfName.BITSPERCOMPONENT, new PdfNumber(bitDepth)); decodeparms.put(PdfName.PREDICTOR, new PdfNumber(15)); decodeparms.put(PdfName.COLUMNS, new PdfNumber(w)); decodeparms.put(PdfName.COLORS, new PdfNumber((colorType == 2) ? 3: 1)); put(PdfName.DECODEPARMS, decodeparms); Png.getInt(istr); } else if (Png.PLTE.Equals(marker)) { if (colorType == 3) { PdfArray colorspace = new PdfArray(); colorspace.Add(PdfName.INDEXED); colorspace.Add(PdfName.DEVICERGB); colorspace.Add(new PdfNumber(len / 3 - 1)); ByteBuffer colortable = new ByteBuffer(); while ((len--) > 0) { colortable.Append_i(istr.ReadByte()); } colorspace.Add(new PdfString(colortable.toByteArray())); put(PdfName.COLORSPACE, colorspace); Png.getInt(istr); } else { Image.skip(istr, len + 4); } } else if (Png.IEND.Equals(marker)) { break; } else { for (int j = -4; j < len; j++) { istr.ReadByte(); } } } break; case Element.JPEG: put(PdfName.FILTER, PdfName.DCTDECODE); switch (image.Colorspace) { case 1: put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); break; case 3: put(PdfName.COLORSPACE, PdfName.DEVICERGB); break; default: put(PdfName.COLORSPACE, PdfName.DEVICECMYK); if (image.isInvertedJPEG()) { put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0 1 0]")); } break; } put(PdfName.BITSPERCOMPONENT, new PdfNumber(8)); if (image.RawData != null) { bytes = image.RawData; streamBytes = null; put(PdfName.LENGTH, new PdfNumber(bytes.Length)); return; } transferBytes(istr, streamBytes, -1); break; case Element.GIF: // HEADER + INFO + COLORTABLE // Byte 0-2: header // checks if the file really is a GIF-file if (istr.ReadByte() != 'G' || istr.ReadByte() != 'I' || istr.ReadByte() != 'F') { throw new BadPdfFormatException(errorID + " is not a GIF-file (GIF header not found)."); } put(PdfName.FILTER, PdfName.LZWDECODE); PdfDictionary decodeparams = new PdfDictionary(); decodeparams.put(PdfName.EARLYCHANGE, new PdfNumber(0)); put(PdfName.DECODEPARMS, decodeparams); PdfArray cspace = new PdfArray(); cspace.Add(PdfName.INDEXED); cspace.Add(PdfName.DEVICERGB); // Byte 3-5: version // Byte 6-7: logical screen width // Byte 8-9: logical screen height // Byte 10: Packed Fields for (int j = 0; j < 8; j++) { i = istr.ReadByte(); } // Byte 10: bit 1: Global Color Table Flag if ((i & 0x80) == 0) { throw new BadPdfFormatException(errorID + " is not a supported GIF-file (there is no global color table present)."); } // Byte 10: bit 6-8: Size of Global Color Table int nColors = 1 << ((i & 7) + 1); cspace.Add(new PdfNumber(nColors - 1)); // Byte 11: Background color index istr.ReadByte(); // Byte 12: Pixel aspect ratio istr.ReadByte(); // Byte 13-...: Global color table ByteBuffer ctable = new ByteBuffer(); for (int j = 0; j < nColors; j++) { ctable.Append_i(istr.ReadByte()); // red ctable.Append_i(istr.ReadByte()); // green ctable.Append_i(istr.ReadByte()); // blue } cspace.Add(new PdfString(ctable.toByteArray())); put(PdfName.COLORSPACE, cspace); put(PdfName.BITSPERCOMPONENT, new PdfNumber(8)); // IMAGE DESCRIPTOR // Byte 0: Image separator // only simple gif files with image immediate following global color table are supported // 0x2c is a fixed value for the image separator if (istr.ReadByte() != 0x2c) { throw new BadPdfFormatException(errorID + " is not a supported GIF-file (the image separator '0x2c' is not found after reading the color table)."); } // Byte 1-2: Image Left Position // Byte 3-4: Image Top Position // Byte 5-6: Image Width // Byte 7-8: Image Height // ignore position and size for (int j = 0; j < 8; j++) { istr.ReadByte(); } // Byte 9: Packed Fields // Byte 9: bit 1: Local Color Table Flag // Byte 9: bit 2: Interlace Flag if ((istr.ReadByte() & 0xc0) > 0) { throw new BadPdfFormatException(errorID + " is not a supported GIF-file (interlaced gifs or gifs using local color table can't be inserted)."); } // Byte 10: LZW initial code if (istr.ReadByte() != 0x08) { throw new BadPdfFormatException(errorID + " is not a supported GIF-file (initial LZW code not supported)."); } // Read the Image Data int code = 0; int codelength = 9; int tablelength = 257; int bitsread = 0; int bitstowrite = 0; int bitsdone = 0; int bitsleft = 23; int bytesdone = 0; int bytesread = 0; int byteswritten = 0; // read the size of the first Data Block int size = istr.ReadByte(); // Check if there is any data in the GIF if (size < 1) { throw new BadPdfFormatException(errorID + " is not a supported GIF-file. (no image data found)."); } // if possible, we read the first 24 bits of data size--; bytesread++; bitsread = istr.ReadByte(); if (size > 0) { size--; bytesread++; bitsread += (istr.ReadByte() << 8); if (size > 0) { size--; bytesread++; bitsread += (istr.ReadByte() << 16); } } while (bytesread > byteswritten) { tablelength++; // we extract a code with length=codelength code = (bitsread >> bitsdone) & ((1 << codelength) - 1); // we delete the bytesdone in bitsread and append the next byte(s) bytesdone = (bitsdone + codelength) / 8; bitsdone = (bitsdone + codelength) % 8; while (bytesdone > 0) { bytesdone--; bitsread = (bitsread >> 8); if (size > 0) { size--; bytesread++; bitsread += (istr.ReadByte() << 16); } else { size = istr.ReadByte(); if (size > 0) { size--; bytesread++; bitsread += (istr.ReadByte() << 16); } } } // we package all the bits that are done into bytes and write them to the stream bitstowrite += (code << (bitsleft - codelength + 1)); bitsleft -= codelength; while (bitsleft < 16) { streamBytes.WriteByte((byte)(bitstowrite >> 16)); byteswritten++; bitstowrite = (bitstowrite & 0xFFFF) << 8; bitsleft += 8; } if (code == 256) { codelength = 9; tablelength = 257; } if (code == 257) { break; } if (tablelength == (1 << codelength)) { codelength++; } } if (bytesread - byteswritten > 2) { throw new BadPdfFormatException(errorID + " is not a supported GIF-file (unexpected end of data block)."); } break; default: throw new BadPdfFormatException(errorID + " is an unknown Image format."); } put(PdfName.LENGTH, new PdfNumber(streamBytes.Length)); } catch (IOException ioe) { throw new BadPdfFormatException(ioe.Message); } finally { if (istr != null) { try{ istr.Close(); } catch (Exception ee) { ee.GetType(); // empty on purpose } } } }
/** Generates the font dictionary for this font. * @return the PdfDictionary containing the font dictionary * @param firstChar the first valid character * @param lastChar the last valid character * @param shortTag a 256 bytes long <CODE>byte</CODE> array where each unused byte is represented by 0 * @param fontDescriptor the indirect reference to a PdfDictionary containing the font descriptor or <CODE>null</CODE> */ private PdfDictionary getFontBaseType(PdfIndirectReference fontDescriptor, int firstChar, int lastChar, byte[] shortTag) { PdfDictionary dic = new PdfDictionary(PdfName.FONT); dic.put(PdfName.SUBTYPE, PdfName.TYPE1); dic.put(PdfName.BASEFONT, new PdfName(FontName)); bool stdEncoding = encoding.Equals("windows-1252") || encoding.Equals("MacRoman"); if (!fontSpecific) { for (int k = firstChar; k <= lastChar; ++k) { if (!differences[k].Equals(notdef)) { firstChar = k; break; } } if (stdEncoding) { dic.put(PdfName.ENCODING, encoding.Equals("windows-1252") ? PdfName.WIN_ANSI_ENCODING : PdfName.MAC_ROMAN_ENCODING); } else { PdfDictionary enc = new PdfDictionary(new PdfName("Encoding")); PdfArray dif = new PdfArray(); bool gap = true; for (int k = firstChar; k <= lastChar; ++k) { if (shortTag[k] != 0) { if (gap) { dif.Add(new PdfNumber(k)); gap = false; } dif.Add(new PdfName(differences[k])); } else { gap = true; } } enc.put(new PdfName("Differences"), dif); dic.put(PdfName.ENCODING, enc); } } if (forceWidthsOutput || !(builtinFont && (fontSpecific || stdEncoding))) { dic.put(new PdfName("FirstChar"), new PdfNumber(firstChar)); dic.put(new PdfName("LastChar"), new PdfNumber(lastChar)); PdfArray wd = new PdfArray(); for (int k = firstChar; k <= lastChar; ++k) { if (shortTag[k] == 0) { wd.Add(new PdfNumber(0)); } else { wd.Add(new PdfNumber(widths[k])); } } dic.put(new PdfName("Widths"), wd); } if (!builtinFont && fontDescriptor != null) { dic.put(new PdfName("FontDescriptor"), fontDescriptor); } return(dic); }