public static void Run() { File file = new File("my-image", ".pdf"); IFileHandler rootOfChain = new JpegHandler(); IFileHandler pngHandler = new PngHandler(); IFileHandler pdfHandler = new PdfHandler(); rootOfChain.SetNext(pngHandler); pngHandler.SetNext(pdfHandler); rootOfChain.Handle(file); }
/// <summary> /// Adds the JPEG image represented by the input stream at the specified location. /// </summary> public AddedImage AddJpeg(Stream fileStream, PdfRectangle placementRectangle) { var startFrom = fileStream.Position; var info = JpegHandler.GetInformation(fileStream); byte[] data; using (var memory = new MemoryStream()) { fileStream.Seek(startFrom, SeekOrigin.Begin); fileStream.CopyTo(memory); data = memory.ToArray(); } var imgDictionary = new Dictionary <NameToken, IToken> { { NameToken.Type, NameToken.Xobject }, { NameToken.Subtype, NameToken.Image }, { NameToken.Width, new NumericToken(info.Width) }, { NameToken.Height, new NumericToken(info.Height) }, { NameToken.BitsPerComponent, new NumericToken(info.BitsPerComponent) }, { NameToken.ColorSpace, NameToken.Devicergb }, { NameToken.Filter, NameToken.DctDecode }, { NameToken.Length, new NumericToken(data.Length) } }; var reference = documentBuilder.AddImage(new DictionaryToken(imgDictionary), data); if (!resourcesDictionary.TryGetValue(NameToken.Xobject, out var xobjectsDict) || !(xobjectsDict is DictionaryToken xobjects)) { xobjects = new DictionaryToken(new Dictionary <NameToken, IToken>()); resourcesDictionary[NameToken.Xobject] = xobjects; } var key = NameToken.Create($"I{imageKey++}"); resourcesDictionary[NameToken.Xobject] = xobjects.With(key, new IndirectReferenceToken(reference)); operations.Add(Push.Value); // This needs to be the placement rectangle. operations.Add(new ModifyCurrentTransformationMatrix(new [] { (decimal)placementRectangle.Width, 0, 0, (decimal)placementRectangle.Height, (decimal)placementRectangle.BottomLeft.X, (decimal)placementRectangle.BottomLeft.Y })); operations.Add(new InvokeNamedXObject(key)); operations.Add(Pop.Value); return(new AddedImage(reference, info.Width, info.Height)); }