Exemplo n.º 1
0
        /// <summary>
        /// Run the XObject with the given name
        /// </summary>
        private void RunXObject(string name)
        {
            var xobjectDict = xObjectManager.GetXObject(name);
            var subType     = xobjectDict.Elements.GetName(PdfKeys.Subtype);

            if (subType != PdfKeys.Form)
            {
                return;
            }

            var cloneCurrentGraphicsState = currentGraphicsState.Clone();

            var    matrixArray = xobjectDict.Elements.GetArray(PdfKeys.Matrix);
            Matrix matrix      = Matrix.Identity;

            if (matrixArray != null)
            {
                matrix = PdfUtilities.GetMatrix(matrixArray);
                cloneCurrentGraphicsState.TransformationMatrix *= matrix;
            }

            cloneCurrentGraphicsState.FillAlpha.Layer  = 1.0;
            cloneCurrentGraphicsState.FillAlpha.Object = 1.0;

            cloneCurrentGraphicsState.StrokeAlpha.Layer  = 1.0;
            cloneCurrentGraphicsState.StrokeAlpha.Object = 1.0;

            cloneCurrentGraphicsState.SoftMask = null;

            CSequence sequence = ContentReader.ReadContent(xobjectDict.Stream.UnfilteredValue);

            var interpreter = new ContentInterpreter();
            var group       = interpreter.Run(xobjectDict, sequence, cloneCurrentGraphicsState);

            // do some optimizations that the post-processor cannot do

            if (group.Children.Count == 1 && group.Clip == null && !DoubleUtilities.IsEqual(currentGraphicsState.FillAlpha.Object, 1.0))
            {
                // the layer has only 1 child and the layer has an opacity set other than 1 ->
                // recreate the layer but with the layer opacity set which gets "added" to each single object
                // on that layer. Because there is only 1 object the result is the same as if the object sits
                // on a semi transparent layer. That saves a group for a single object.

                cloneCurrentGraphicsState = currentGraphicsState.Clone();

                if (matrixArray != null)
                {
                    cloneCurrentGraphicsState.TransformationMatrix *= matrix;
                }

                cloneCurrentGraphicsState.FillAlpha.Layer   = currentGraphicsState.FillAlpha.Object;
                cloneCurrentGraphicsState.StrokeAlpha.Layer = currentGraphicsState.StrokeAlpha.Object;

                group = interpreter.Run(xobjectDict, sequence, cloneCurrentGraphicsState);
                graphicGroup.Children.Add(group.Children[0]);
            }
            else
            {
                group.Opacity = currentGraphicsState.FillAlpha.Object;
                graphicGroup.Children.Add(group);
            }
        }