Exemplo n.º 1
0
        private static StreamToken WriteContentStream(IReadOnlyList <IGraphicsStateOperation> content)
        {
            using (var memoryStream = new MemoryStream())
            {
                foreach (var operation in content)
                {
                    operation.Write(memoryStream);
                }

                var bytes = memoryStream.ToArray();

                var stream = DataCompresser.CompressToStream(bytes);

                return(stream);
            }
        }
Exemplo n.º 2
0
            public IndirectReferenceToken Write(IPdfStreamWriter writer)
            {
                using (var memoryStream = new MemoryStream())
                {
                    foreach (var operation in operations)
                    {
                        operation.Write(memoryStream);
                    }

                    var bytes = memoryStream.ToArray();

                    var stream = DataCompresser.CompressToStream(bytes);

                    return(writer.WriteToken(stream));
                }
            }
Exemplo n.º 3
0
        /// <summary>
        /// Adds the PNG image represented by the input stream at the specified location.
        /// </summary>
        public AddedImage AddPng(Stream pngStream, PdfRectangle placementRectangle)
        {
            var png = Png.Open(pngStream);

            byte[] data;
            var    pixelBuffer = new byte[3];

            using (var memoryStream = new MemoryStream())
            {
                for (var rowIndex = 0; rowIndex < png.Height; rowIndex++)
                {
                    for (var colIndex = 0; colIndex < png.Width; colIndex++)
                    {
                        var pixel = png.GetPixel(colIndex, rowIndex);

                        pixelBuffer[0] = pixel.R;
                        pixelBuffer[1] = pixel.G;
                        pixelBuffer[2] = pixel.B;

                        memoryStream.Write(pixelBuffer, 0, pixelBuffer.Length);
                    }
                }

                data = memoryStream.ToArray();
            }

            var compressed = DataCompresser.CompressBytes(data);

            var imgDictionary = new Dictionary <NameToken, IToken>
            {
                { NameToken.Type, NameToken.Xobject },
                { NameToken.Subtype, NameToken.Image },
                { NameToken.Width, new NumericToken(png.Width) },
                { NameToken.Height, new NumericToken(png.Height) },
                { NameToken.BitsPerComponent, new NumericToken(png.Header.BitDepth) },
                { NameToken.ColorSpace, NameToken.Devicergb },
                { NameToken.Filter, NameToken.FlateDecode },
                { NameToken.Length, new NumericToken(compressed.Length) }
            };

            var reference = documentBuilder.AddImage(new DictionaryToken(imgDictionary), compressed);

            var resources = pageDictionary.GetOrCreateDict(NameToken.Resources);
            var xObjects  = resources.GetOrCreateDict(NameToken.Xobject);

            var key = NameToken.Create($"I{imageKey++}");

            xObjects[key] = 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);

            return(new AddedImage(reference.Data, png.Width, png.Height));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds the PNG image represented by the input stream at the specified location.
        /// </summary>
        public AddedImage AddPng(Stream pngStream, PdfRectangle placementRectangle)
        {
            var png = Png.Open(pngStream);

            byte[] data;
            var    pixelBuffer = new byte[3];

            using (var memoryStream = new MemoryStream())
            {
                for (var rowIndex = 0; rowIndex < png.Height; rowIndex++)
                {
                    for (var colIndex = 0; colIndex < png.Width; colIndex++)
                    {
                        var pixel = png.GetPixel(colIndex, rowIndex);

                        pixelBuffer[0] = pixel.R;
                        pixelBuffer[1] = pixel.G;
                        pixelBuffer[2] = pixel.B;

                        memoryStream.Write(pixelBuffer, 0, pixelBuffer.Length);
                    }
                }

                data = memoryStream.ToArray();
            }

            var widthToken  = new NumericToken(png.Width);
            var heightToken = new NumericToken(png.Height);

            IndirectReferenceToken smaskReference = null;

            if (png.HasAlphaChannel && documentBuilder.ArchiveStandard != PdfAStandard.A1B && documentBuilder.ArchiveStandard != PdfAStandard.A1A)
            {
                var smaskData = new byte[data.Length / 3];
                for (var rowIndex = 0; rowIndex < png.Height; rowIndex++)
                {
                    for (var colIndex = 0; colIndex < png.Width; colIndex++)
                    {
                        var pixel = png.GetPixel(colIndex, rowIndex);

                        var index = rowIndex * png.Width + colIndex;
                        smaskData[index] = pixel.A;
                    }
                }

                var compressedSmask = DataCompresser.CompressBytes(smaskData);

                // Create a soft-mask.
                var smaskDictionary = new Dictionary <NameToken, IToken>
                {
                    { NameToken.Type, NameToken.Xobject },
                    { NameToken.Subtype, NameToken.Image },
                    { NameToken.Width, widthToken },
                    { NameToken.Height, heightToken },
                    { NameToken.ColorSpace, NameToken.Devicegray },
                    { NameToken.BitsPerComponent, new NumericToken(png.Header.BitDepth) },
                    { NameToken.Decode, new ArrayToken(new IToken[] { new NumericToken(0), new NumericToken(1) }) },
                    { NameToken.Length, new NumericToken(compressedSmask.Length) },
                    { NameToken.Filter, NameToken.FlateDecode }
                };

                smaskReference = documentBuilder.AddImage(new DictionaryToken(smaskDictionary), compressedSmask);
            }

            var compressed = DataCompresser.CompressBytes(data);

            var imgDictionary = new Dictionary <NameToken, IToken>
            {
                { NameToken.Type, NameToken.Xobject },
                { NameToken.Subtype, NameToken.Image },
                { NameToken.Width, widthToken },
                { NameToken.Height, heightToken },
                { NameToken.BitsPerComponent, new NumericToken(png.Header.BitDepth) },
                { NameToken.ColorSpace, NameToken.Devicergb },
                { NameToken.Filter, NameToken.FlateDecode },
                { NameToken.Length, new NumericToken(compressed.Length) }
            };

            if (smaskReference != null)
            {
                imgDictionary.Add(NameToken.Smask, smaskReference);
            }

            var reference = documentBuilder.AddImage(new DictionaryToken(imgDictionary), compressed);

            var resources = pageDictionary.GetOrCreateDict(NameToken.Resources);
            var xObjects  = resources.GetOrCreateDict(NameToken.Xobject);

            var key = NameToken.Create($"I{imageKey++}");

            xObjects[key] = 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);

            return(new AddedImage(reference.Data, png.Width, png.Height));
        }