/// <summary> /// Adds the image previously added using <see cref="AddJpeg(byte[], PdfRectangle)"/> /// or <see cref="AddPng(byte[], PdfRectangle)"/> sharing the same image to prevent duplication. /// </summary> public void AddImage(AddedImage image, PdfRectangle placementRectangle) { var resources = pageDictionary.GetOrCreateDict(NameToken.Resources); var xObjects = resources.GetOrCreateDict(NameToken.Xobject); var key = NameToken.Create($"I{imageKey++}"); xObjects[key] = new IndirectReferenceToken(image.Reference); currentStream.Add(Push.Value); // This needs to be the placement rectangle. currentStream.Add(new ModifyCurrentTransformationMatrix(new[] { (decimal)placementRectangle.Width, 0, 0, (decimal)placementRectangle.Height, (decimal)placementRectangle.BottomLeft.X, (decimal)placementRectangle.BottomLeft.Y })); currentStream.Add(new InvokeNamedXObject(key)); currentStream.Add(Pop.Value); }
/// <summary> /// Adds the JPEG image previously added using <see cref="AddJpeg(byte[],PdfRectangle)"/>, /// this will share the same image data to prevent duplication. /// </summary> /// <param name="image">An image previously added to this page or another page.</param> /// <param name="placementRectangle">The size and location to draw the image on this page.</param> public void AddJpeg(AddedImage image, PdfRectangle placementRectangle) { 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(image.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); }
/// <summary> /// Adds the JPEG image previously added using <see cref="AddJpeg(byte[],PdfRectangle)"/>, /// this will share the same image data to prevent duplication. /// </summary> /// <param name="image">An image previously added to this page or another page.</param> /// <param name="placementRectangle">The size and location to draw the image on this page.</param> public void AddJpeg(AddedImage image, PdfRectangle placementRectangle) => AddImage(image, placementRectangle);