private void initAU(Stream streamSound, PDFDictionaryStream streamOut) { byte[] word = new byte[4]; streamSound.Position = 12; streamSound.Read(word, 0, word.Length); int compression = BitConverter.ToInt32(word, 0); if (compression == 0) { streamOut.Dictionary.AddItem("E", new PDFName("Signed")); } else if (compression == 1) { streamOut.Dictionary.AddItem("E", new PDFName("muLaw")); } else if (compression == 27) { streamOut.Dictionary.AddItem("E", new PDFName("ALaw")); } streamSound.Read(word, 0, word.Length); streamOut.Dictionary.AddItem("R", new PDFNumber(BitConverter.ToInt32(word, 0))); streamSound.Read(word, 0, word.Length); int channel = BitConverter.ToInt32(word, 0); streamOut.Dictionary.AddItem("B", new PDFNumber(channel)); streamOut.GetStream().SetLength(streamSound.Length); streamSound.Read(streamOut.GetStream().GetBuffer(), 0, streamOut.GetStream().GetBuffer().Length); }
private void initData(Stream fs, PDFDictionaryStream stream) { stream.Dictionary.AddItem("Type", new PDFName("3D")); stream.Dictionary.AddItem("Subtype", new PDFName("U3D")); stream.GetStream().SetLength(fs.Length); fs.Read(stream.GetStream().GetBuffer(), 0, (int)fs.Length); }
internal ToUnicode(PDFDictionaryStream toUnicode) { if (toUnicode != null) { toUnicode.Decode(); toUnicode.GetStream().Position = 0; m_chars = new List <BfChar>(); m_ranges = new List <BfRange>(); Lexer lexer = new Lexer(toUnicode.GetStream(), null, null, 256); parse(lexer); } }
public ICCBased(string filename) { FileStream fstream = new FileStream(filename, FileMode.Open); AddItem(new PDFName("ICCBased")); PDFDictionaryStream dstream = new PDFDictionaryStream(); byte[] buf = new byte[4]; fstream.Position = 16; fstream.Read(buf, 0, buf.Length); switch (Encoding.GetString(buf)) { case "CMYK": WriteDictionary(dstream.Dictionary, 4, "DeviceCMYK"); break; case "GRAY": WriteDictionary(dstream.Dictionary, 1, "DeviceGRAY"); break; case "RGB ": WriteDictionary(dstream.Dictionary, 3, "DeviceRGB"); break; case "Lab ": //TODO break; } buf = new byte[fstream.Length]; fstream.Read(buf, 0, buf.Length); dstream.GetStream().Write(buf, 0, buf.Length); AddItem(dstream); }
private void initFormat32bppArgb(Bitmap bmp) { int f = bmp.Flags; BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat); byte[] buf = new byte[bmpData.Stride * bmpData.Height]; System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, buf, 0, buf.Length); _dict.Dictionary.AddItem("BitsPerComponent", new PDFNumber(8)); _dict.Dictionary.AddItem("ColorSpace", new PDFName("DeviceRGB")); PDFDictionaryStream dictSMask = new PDFDictionaryStream(new PDFDictionary(), new MemoryStream()); initSMask(dictSMask); _dict.Dictionary.AddItem("SMask", dictSMask); _isSMask = true; MemoryStream stream = _dict.GetStream(); stream.SetLength(buf.Length); MemoryStream maskStream = dictSMask.GetStream(); for (int i = 0; i < buf.Length; i += 4) { stream.WriteByte(buf[i + 2]); stream.WriteByte(buf[i + 1]); stream.WriteByte(buf[i]); maskStream.WriteByte(buf[i + 3]); } bmp.UnlockBits(bmpData); }
private void loadCanvas() { IPDFObject contents = _dictionary["Contents"]; if (contents is PDFArray) { addFirstq(contents as PDFArray); addLast(contents as PDFArray); } else if (contents is PDFDictionaryStream) { PDFArray arr = new PDFArray(); addFirstq(arr); arr.AddItem(contents); addLast(arr); _dictionary.AddItem("Contents", arr); } else { PDFArray arr = new PDFArray(); PDFDictionaryStream ds = new PDFDictionaryStream(); MemoryStream s = ds.GetStream(); s.WriteByte((byte)'q'); s.WriteByte((byte)'\r'); s.WriteByte((byte)'Q'); arr.AddItem(ds); _dictionary.AddItem("Contents", arr); _canvas = new Canvas(s, Resources, PageRect); _canvas.ChangeGroup += new ChangeGroupEventHandler(m_canvas_ChangeGroup); } }
public ICCBased(byte[] array) { AddItem(new PDFName("ICCBased")); PDFDictionaryStream dstream = new PDFDictionaryStream(); byte[] buf = new byte[4]; for (int i = 16; i < 20; ++i) { buf[i - 16] = array[i]; } switch (Encoding.GetString(buf)) { case "CMYK": WriteDictionary(dstream.Dictionary, 4, "DeviceCMYK"); break; case "GRAY": WriteDictionary(dstream.Dictionary, 1, "DeviceGRAY"); break; case "RGB ": WriteDictionary(dstream.Dictionary, 3, "DeviceRGB"); break; case "Lab ": //TODO break; } dstream.GetStream().Write(array, 0, array.Length); AddItem(dstream); }
internal static PDFDictionaryStream Copy(PDFDictionaryStream dictStream) { Stream stream = dictStream.GetStream(); byte[] buf = new byte[stream.Length]; stream.Position = 0; stream.Read(buf, 0, buf.Length); MemoryStream newStream = new MemoryStream(); newStream.Write(buf, 0, buf.Length); PDFDictionary dict = dictStream.Dictionary; PDFDictionary newDict = new PDFDictionary(); string[] keys = { "Type", "Subtype", "FormType", "BBox", "Matrix", "Ref", "Metadata", "LastModified", "Name" }; for (int i = 0; i < keys.Length; ++i) { IPDFObject obj = dict[keys[i]]; if (obj != null) { newDict.AddItem(keys[i], obj.Clone()); } } PDFDictionary resources = dict["Resources"] as PDFDictionary; if (resources != null) { newDict.AddItem("Resources", ResourcesBase.Copy(resources)); } PDFDictionary group = dict["Group"] as PDFDictionary; if (group != null) { newDict.AddItem("Group", GroupBase.Copy(group)); } // PieceInfo, OPI, OC - still unknown // StructParent, StructParents - do not IPDFObject filter = dict["Filter"]; if (filter != null) { newDict.AddItem("Filter", filter.Clone()); } IPDFObject decodeParms = dict["DecodeParms"]; if (decodeParms != null) { newDict.AddItem("DecodeParms", decodeParms.Clone()); } return(new PDFDictionaryStream(newDict, newStream)); }
private void readCrossReferencesEntries(PDFDictionaryStream xrefStream) { int[] W = getW(xrefStream.Dictionary); int[] Index = getIndex(xrefStream.Dictionary); xrefStream.Decode(); Stream stream = xrefStream.GetStream(); stream.Position = 0; byte[] bType = new byte[W[0]]; byte[] bOffset = new byte[W[1]]; byte[] bGeneration = new byte[W[2]]; int prevCount, count, type, offset, generation; for (int i = 0; i < Index.Length / 2; ++i) { prevCount = Index[2 * i]; count = Index[2 * i + 1]; addEntries(prevCount + count); for (int j = 0; j < count; ++j) { if (stream.Read(bType, 0, bType.Length) < 0) { return; } type = bytesToInt(bType); if (stream.Read(bOffset, 0, bOffset.Length) < 0) { return; } offset = bytesToInt(bOffset); if (stream.Read(bGeneration, 0, bGeneration.Length) < 0) { return; } if (W[2] == 0) { generation = 0; } else { generation = bytesToInt(bGeneration); } if (_xref.Entries[prevCount + j] == null) { _xref.Entries[prevCount + j] = new Entry((byte)type, offset, generation); } } } }
public FullFileSpecification(Stream stream, string name) { FileName = UF = name; PDFDictionary embeddedFS = new PDFDictionary(); PDFDictionaryStream dictF = new PDFDictionaryStream(); byte[] data = new byte[stream.Length]; stream.Read(data, 0, data.Length); dictF.GetStream().Write(data, 0, data.Length); embeddedFS.AddItem("F", dictF); EF = embeddedFS; }
private void writeContents(PDFDictionaryStream dictStream, byte[] buf, Stream output) { if (dictStream != null) { dictStream.Decode(); Stream str = dictStream.GetStream(); str.Position = 0; int numread; while ((numread = str.Read(buf, 0, buf.Length)) > 0) { output.Write(buf, 0, numread); } } }
public FullFileSpecification(string filename) { FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); FileName = UF = System.IO.Path.GetFileName(filename); PDFDictionary embeddedFS = new PDFDictionary(); PDFDictionaryStream dictF = new PDFDictionaryStream(); byte[] data = new byte[fs.Length]; fs.Read(data, 0, data.Length); dictF.GetStream().Write(data, 0, data.Length); embeddedFS.AddItem("F", dictF); EF = embeddedFS; fs.Close(); }
private void saveEmbedded(Stream stream) { try { FileSpecification fs = FileSpecification; PDFDictionaryStream dictStream = (PDFDictionaryStream)fs.EF["F"]; dictStream.Decode(); MemoryStream s = dictStream.GetStream(); s.Position = 0; s.WriteTo(stream); } catch { // ignored } }
private void initAIFF(Stream streamSound, PDFDictionaryStream streamOut) { byte[] word = new byte[4]; streamOut.Dictionary.AddItem("E", new PDFName("Signed")); streamSound.Read(word, 0, word.Length); if (PDF.Encoding.GetString(word) == "COMM") { streamSound.Position += 4; byte[] buf = new byte[2]; streamSound.Read(buf, 0, buf.Length); invert(buf); streamOut.Dictionary.AddItem("C", new PDFNumber(BitConverter.ToInt16(buf, 0))); streamSound.Position += 4; streamSound.Read(buf, 0, buf.Length); invert(buf); int bitPerDepth = BitConverter.ToInt16(buf, 0); streamOut.Dictionary.AddItem("B", new PDFNumber(bitPerDepth)); buf = new byte[10]; streamSound.Read(buf, 0, buf.Length); streamOut.Dictionary.AddItem("R", new PDFNumber((float)BinaryUtility.ConvertFromIeeeExtended(buf))); streamSound.Read(word, 0, word.Length); if (PDF.Encoding.GetString(word) == "SSND") { streamSound.Read(word, 0, word.Length); invert(word); int size = BitConverter.ToInt32(word, 0); streamSound.Read(word, 0, word.Length); invert(word); int offset = BitConverter.ToInt32(word, 0); streamSound.Read(word, 0, word.Length); invert(word); int blockSize = BitConverter.ToInt32(word, 0); streamSound.Position += offset; byte[] data = new byte[size]; streamSound.Read(data, 0, data.Length); streamOut.GetStream().Write(data, 0, data.Length); } else { throw new PDFUnsupportedSoundFormatException(); } } else { throw new PDFUnsupportedSoundFormatException(); } }
public JBIG2Parameters(PDFDictionary dict) { m_stream = null; if (dict == null) { return; } PDFDictionaryStream dictStream = dict["JBIG2Globals"] as PDFDictionaryStream; if (dictStream == null) { return; } dictStream.Decode(); m_stream = dictStream.GetStream(); }
internal Type1Font(byte[] buffer) { Type1Parser parser = new Type1Parser(new Reader(buffer)); initialize(parser); initFontEncoding(parser); PDFDictionaryStream fontFile = new PDFDictionaryStream(); fontFile.Dictionary.AddItem("Length1", new PDFNumber(buffer.Length)); fontFile.Dictionary.AddItem("Length2", new PDFNumber(0)); fontFile.Dictionary.AddItem("Length3", new PDFNumber(0)); Stream stream = fontFile.GetStream(); stream.Position = 0; stream.Write(buffer, 0, buffer.Length); ((PDFDictionary)GetDictionary()["FontDescriptor"]).AddItem("FontFile", fontFile); GetDictionary().Tag = this; }
/// <summary> /// Initializes a new instance of the Bytescout.PDF.Image class from the specified file or URL. /// </summary> /// <param name="fileName" href="http://msdn.microsoft.com/en-us/library/system.string.aspx">Path or URL of the image file.</param> /// <param name="compression">The compression filter used for this image.</param> /// <param name="jpegQuality">The JPEG quality.</param> public Image(string fileName, ImageCompression compression, int jpegQuality) : this(fileName) { Compression = compression; if (compression == ImageCompression.DCT) { System.Drawing.Image image = System.Drawing.Image.FromFile(fileName); DCTDecoder.Encode(image, _dict.GetStream(), jpegQuality); image.Dispose(); } }
private void parseGenerationEntry(Entry entry) { PDFDictionaryStream dictStream = GetObject(entry.Offset) as PDFDictionaryStream; if (dictStream != null) { PDFNumber n = dictStream.Dictionary["N"] as PDFNumber; if (n == null) { return; } int count = (int)n.GetValue(); if (count <= 0) { return; } n = dictStream.Dictionary["First"] as PDFNumber; if (n == null) { return; } int first = (int)n.GetValue(); if (first < 0) { return; } dictStream.Decode(); List <int> objects = new List <int>(); List <int> offsets = new List <int>(); Lexer lexer = new Lexer(dictStream.GetStream(), this, null, 512); lexer.Position = 0; for (int i = 0; i < count; ++i) { bool succes; int objNo = lexer.ReadInteger(out succes); if (!succes) { break; } int offset = lexer.ReadInteger(out succes); if (!succes) { break; } objects.Add(objNo); offsets.Add(offset); } int l = objects.Count; for (int i = 0; i < l; ++i) { lexer.Position = offsets[i] + first; IPDFObject obj = lexer.ReadObject(); if (obj == null) { obj = new PDFNull(); } int index = objects[i]; if (index >= 0 && index < _entries.Count) { if (_entries[index] == null) { _entries[index] = new Entry(0, 0); } if (entry.Offset == _entries[index].Offset) { _entries[index].Object = obj; } } } } }
private void initIndexed(Bitmap bmp) { int index = 32; switch (bmp.PixelFormat) { case PixelFormat.Format1bppIndexed: index /= 1; _dict.Dictionary.AddItem("BitsPerComponent", new PDFNumber(1)); break; case PixelFormat.Format4bppIndexed: index /= 4; _dict.Dictionary.AddItem("BitsPerComponent", new PDFNumber(4)); break; case PixelFormat.Indexed: case PixelFormat.Format8bppIndexed: index /= 8; _dict.Dictionary.AddItem("BitsPerComponent", new PDFNumber(8)); break; } System.Drawing.Color[] colors = bmp.Palette.Entries; byte[] buf = new byte[colors.Length * 3]; for (int i = 0; i < colors.Length; ++i) { buf[i * 3] = colors[i].R; buf[i * 3 + 1] = colors[i].G; buf[i * 3 + 2] = colors[i].B; } PDFArray array = new PDFArray(); array.AddItem(new PDFName("Indexed")); PDFArray arrayICC = new PDFArray(); arrayICC.AddItem(new PDFName("ICCBased")); PDFDictionaryStream stream = new PDFDictionaryStream(); stream.Dictionary.AddItem("Alternate", new PDFName("DeviceRGB")); stream.Dictionary.AddItem("N", new PDFNumber(3)); if (bmp.PropertyItems.Length > 0) { stream.GetStream().Write(bmp.PropertyItems[0].Value, 0, bmp.PropertyItems[0].Value.Length); } arrayICC.AddItem(stream); array.AddItem(new PDFName("DeviceRGB")); array.AddItem(new PDFNumber(colors.Length - 1)); array.AddItem(new PDFString(buf, true)); _dict.Dictionary.AddItem("ColorSpace", array); BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat); buf = new byte[bmpData.Stride * bmpData.Height]; System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, buf, 0, buf.Length); int countByte = (_width - _width % index) / index + 1; if (_width % index == 0) { --countByte; } int width = _width - (countByte - 1) * index; int countByteW = (width - width % (index / 4)) / (index / 4) + 1; if (width % (index / 4) == 0) { --countByteW; } MemoryStream memoryStream = _dict.GetStream(); for (int i = 0; i < _height; ++i) { memoryStream.Write(buf, i * bmpData.Stride, (countByte - 1) * 4 + countByteW); } bmp.UnlockBits(bmpData); }
private void save(Stream stream) { _stream.Decode(); _stream.GetStream().WriteTo(stream); }
private void initWAVE(Stream streamSound, PDFDictionaryStream streamOut) { byte[] word = new byte[4]; int bitPerDepth = 8; streamSound.Position = 12; int load = 0; while (load < 2 && streamSound.Position < streamSound.Length) { streamSound.Read(word, 0, word.Length); string chunkName = PDF.Encoding.GetString(word); long position; if (chunkName == "fmt ") { streamSound.Read(word, 0, word.Length); position = streamSound.Position + BitConverter.ToInt32(word, 0); streamSound.Read(word, 0, word.Length); int compression = BitConverter.ToInt16(word, 0); int channel = BitConverter.ToInt16(word, 2); streamOut.Dictionary.AddItem("C", new PDFNumber(channel)); streamSound.Read(word, 0, word.Length); streamOut.Dictionary.AddItem("R", new PDFNumber(BitConverter.ToInt32(word, 0))); streamSound.Read(word, 0, word.Length); streamSound.Read(word, 0, word.Length); bitPerDepth = BitConverter.ToInt16(word, 2); streamOut.Dictionary.AddItem("B", new PDFNumber(bitPerDepth)); if (compression == 6) { streamOut.Dictionary.AddItem("E", new PDFName("ALaw")); } if (compression == 7) { streamOut.Dictionary.AddItem("E", new PDFName("muLaw")); } if (compression == 1 && channel >= 2) { streamOut.Dictionary.AddItem("E", new PDFName("Signed")); } streamSound.Position = position; ++load; } else if (chunkName == "data") { streamSound.Read(word, 0, word.Length); position = streamSound.Position + BitConverter.ToInt32(word, 0); byte[] data = new byte[BitConverter.ToInt32(word, 0)]; streamSound.Read(data, 0, data.Length); if (bitPerDepth > 8) { int countByte = bitPerDepth / 8; for (int i = 0; i < data.Length; i += countByte) { for (int j = 0; j < countByte; ++j) { streamOut.GetStream().WriteByte(data[i + countByte - 1 - j]); } } } else { streamOut.GetStream().Write(data, 0, data.Length); } ++load; streamSound.Position = position; } } if (load != 2) { throw new PDFUnsupportedSoundFormatException(); } }