Пример #1
0
        public virtual void EgsTest1()
        {
            String      destinationDocument = destinationFolder + "egsTest1.pdf";
            FileStream  fos      = new FileStream(destinationDocument, FileMode.Create);
            PdfWriter   writer   = new PdfWriter(fos);
            PdfDocument document = new PdfDocument(writer);
            //Create page and canvas
            PdfPage   page   = document.AddNewPage();
            PdfCanvas canvas = new PdfCanvas(page);
            //Create ExtGState and fill it with line width and font
            PdfExtGState egs = new PdfExtGState();

            egs.GetPdfObject().Put(PdfName.LW, new PdfNumber(5));
            PdfArray font    = new PdfArray();
            PdfFont  pdfFont = PdfFontFactory.CreateFont(FontConstants.COURIER);

            pdfFont.Flush();
            font.Add(pdfFont.GetPdfObject());
            font.Add(new PdfNumber(24));
            egs.GetPdfObject().Put(PdfName.Font, font);
            //Write ExtGState
            canvas.SetExtGState(egs);
            //Write text to check that font from ExtGState is applied
            canvas.BeginText();
            canvas.MoveText(50, 600);
            canvas.ShowText("Courier, 24pt");
            canvas.EndText();
            //Draw line to check if ine width is applied
            canvas.MoveTo(50, 500);
            canvas.LineTo(300, 500);
            canvas.Stroke();
            //Write text again to check that font from page resources and font from ExtGState is the same.
            canvas.BeginText();
            canvas.SetFontAndSize(pdfFont, 36);
            canvas.MoveText(50, 400);
            canvas.ShowText("Courier, 36pt");
            canvas.EndText();
            canvas.Release();
            page.Flush();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationDocument, sourceFolder + "cmp_egsTest1.pdf"
                                                                             , destinationFolder, "diff_"));
        }
 /// <summary>Operations to perform before drawing an element.</summary>
 /// <remarks>
 /// Operations to perform before drawing an element.
 /// This includes setting stroke color and width, fill color.
 /// </remarks>
 /// <param name="context">the svg draw context</param>
 internal virtual void PreDraw(SvgDrawContext context)
 {
     if (this.attributesAndStyles != null)
     {
         PdfCanvas    currentCanvas        = context.GetCurrentCanvas();
         PdfExtGState opacityGraphicsState = new PdfExtGState();
         if (!partOfClipPath)
         {
             float generalOpacity = GetOpacity();
             {
                 // fill
                 String fillRawValue = GetAttributeOrDefault(SvgConstants.Attributes.FILL, "black");
                 this.doFill = !SvgConstants.Values.NONE.EqualsIgnoreCase(fillRawValue);
                 if (doFill && CanElementFill())
                 {
                     float            fillOpacity      = GetOpacityByAttributeName(SvgConstants.Attributes.FILL_OPACITY, generalOpacity);
                     Color            fillColor        = null;
                     TransparentColor transparentColor = GetColorFromAttributeValue(context, fillRawValue, 0, fillOpacity);
                     if (transparentColor != null)
                     {
                         fillColor   = transparentColor.GetColor();
                         fillOpacity = transparentColor.GetOpacity();
                     }
                     if (!CssUtils.CompareFloats(fillOpacity, 1f))
                     {
                         opacityGraphicsState.SetFillOpacity(fillOpacity);
                     }
                     // set default if no color has been parsed
                     if (fillColor == null)
                     {
                         fillColor = ColorConstants.BLACK;
                     }
                     currentCanvas.SetFillColor(fillColor);
                 }
             }
             {
                 // stroke
                 String strokeRawValue = GetAttributeOrDefault(SvgConstants.Attributes.STROKE, SvgConstants.Values.NONE);
                 if (!SvgConstants.Values.NONE.EqualsIgnoreCase(strokeRawValue))
                 {
                     String strokeWidthRawValue = GetAttribute(SvgConstants.Attributes.STROKE_WIDTH);
                     // 1 px = 0,75 pt
                     float strokeWidth = 0.75f;
                     if (strokeWidthRawValue != null)
                     {
                         strokeWidth = CssUtils.ParseAbsoluteLength(strokeWidthRawValue);
                     }
                     float            strokeOpacity    = GetOpacityByAttributeName(SvgConstants.Attributes.STROKE_OPACITY, generalOpacity);
                     Color            strokeColor      = null;
                     TransparentColor transparentColor = GetColorFromAttributeValue(context, strokeRawValue, strokeWidth / 2, strokeOpacity
                                                                                    );
                     if (transparentColor != null)
                     {
                         strokeColor   = transparentColor.GetColor();
                         strokeOpacity = transparentColor.GetOpacity();
                     }
                     if (!CssUtils.CompareFloats(strokeOpacity, 1f))
                     {
                         opacityGraphicsState.SetStrokeOpacity(strokeOpacity);
                     }
                     // as default value for stroke is 'none' we should not set
                     // it in case when value obtaining fails
                     if (strokeColor != null)
                     {
                         currentCanvas.SetStrokeColor(strokeColor);
                     }
                     currentCanvas.SetLineWidth(strokeWidth);
                     doStroke = true;
                 }
             }
             {
                 // opacity
                 if (!opacityGraphicsState.GetPdfObject().IsEmpty())
                 {
                     currentCanvas.SetExtGState(opacityGraphicsState);
                 }
             }
         }
     }
 }
Пример #3
0
        /// <summary>Updates current graphic state with values from extended graphic state dictionary.</summary>
        /// <param name="extGState">the wrapper around the extended graphic state dictionary</param>
        /// <param name="pdfDocument">the document to retrieve fonts from. Needed when the newly created fonts are used
        ///     </param>
        internal virtual void UpdateFromExtGState(PdfExtGState extGState, PdfDocument pdfDocument)
        {
            float?lw = extGState.GetLineWidth();

            if (lw != null)
            {
                lineWidth = (float)lw;
            }
            int?lc = extGState.GetLineCapStyle();

            if (lc != null)
            {
                lineCapStyle = (int)lc;
            }
            int?lj = extGState.GetLineJoinStyle();

            if (lj != null)
            {
                lineJoinStyle = (int)lj;
            }
            float?ml = extGState.GetMiterLimit();

            if (ml != null)
            {
                miterLimit = (float)ml;
            }
            PdfArray d = extGState.GetDashPattern();

            if (d != null)
            {
                dashPattern = d;
            }
            PdfName ri = extGState.GetRenderingIntent();

            if (ri != null)
            {
                renderingIntent = ri;
            }
            bool?op = extGState.GetStrokeOverprintFlag();

            if (op != null)
            {
                strokeOverprint = (bool)op;
            }
            op = extGState.GetFillOverprintFlag();
            if (op != null)
            {
                fillOverprint = (bool)op;
            }
            int?opm = extGState.GetOverprintMode();

            if (opm != null)
            {
                overprintMode = (int)opm;
            }
            PdfArray fnt = extGState.GetFont();

            if (fnt != null)
            {
                PdfDictionary fontDictionary = fnt.GetAsDictionary(0);
                if (this.font == null || this.font.GetPdfObject() != fontDictionary)
                {
                    this.font = pdfDocument.GetFont(fontDictionary);
                }
                PdfNumber fntSz = fnt.GetAsNumber(1);
                if (fntSz != null)
                {
                    this.fontSize = fntSz.FloatValue();
                }
            }
            PdfObject bg = extGState.GetBlackGenerationFunction();

            if (bg != null)
            {
                blackGenerationFunction = bg;
            }
            PdfObject bg2 = extGState.GetBlackGenerationFunction2();

            if (bg2 != null)
            {
                blackGenerationFunction2 = bg2;
            }
            PdfObject ucr = extGState.GetUndercolorRemovalFunction();

            if (ucr != null)
            {
                underColorRemovalFunction = ucr;
            }
            PdfObject ucr2 = extGState.GetUndercolorRemovalFunction2();

            if (ucr2 != null)
            {
                underColorRemovalFunction2 = ucr2;
            }
            PdfObject tr = extGState.GetTransferFunction();

            if (tr != null)
            {
                transferFunction = tr;
            }
            PdfObject tr2 = extGState.GetTransferFunction2();

            if (tr2 != null)
            {
                transferFunction2 = tr2;
            }
            PdfObject ht = extGState.GetHalftone();

            if (ht != null)
            {
                halftone = ht;
            }
            PdfObject local_htp = extGState.GetPdfObject().Get(PdfName.HTP);

            if (local_htp != null)
            {
                this.htp = local_htp;
            }
            float?fl = extGState.GetFlatnessTolerance();

            if (fl != null)
            {
                flatnessTolerance = (float)fl;
            }
            float?sm = extGState.GetSmothnessTolerance();

            if (sm != null)
            {
                smoothnessTolerance = sm;
            }
            bool?sa = extGState.GetAutomaticStrokeAdjustmentFlag();

            if (sa != null)
            {
                automaticStrokeAdjustment = (bool)sa;
            }
            PdfObject bm = extGState.GetBlendMode();

            if (bm != null)
            {
                blendMode = bm;
            }
            PdfObject sMask = extGState.GetSoftMask();

            if (sMask != null)
            {
                softMask = sMask;
            }
            float?ca = extGState.GetStrokeOpacity();

            if (ca != null)
            {
                strokeAlpha = (float)ca;
            }
            ca = extGState.GetFillOpacity();
            if (ca != null)
            {
                fillAlpha = (float)ca;
            }
            bool?ais = extGState.GetAlphaSourceFlag();

            if (ais != null)
            {
                alphaIsShape = (bool)ais;
            }
            bool?tk = extGState.GetTextKnockoutFlag();

            if (tk != null)
            {
                textKnockout = (bool)tk;
            }
        }
Пример #4
0
 /// <summary>Operations to perform before drawing an element.</summary>
 /// <remarks>
 /// Operations to perform before drawing an element.
 /// This includes setting stroke color and width, fill color.
 /// </remarks>
 /// <param name="context">the svg draw context</param>
 internal virtual void PreDraw(SvgDrawContext context)
 {
     if (this.attributesAndStyles != null)
     {
         PdfCanvas    currentCanvas        = context.GetCurrentCanvas();
         PdfExtGState opacityGraphicsState = new PdfExtGState();
         if (!partOfClipPath)
         {
             float generalOpacity = GetOpacity();
             {
                 // fill
                 String fillRawValue = GetAttribute(SvgConstants.Attributes.FILL);
                 this.doFill = !SvgConstants.Values.NONE.EqualsIgnoreCase(fillRawValue);
                 if (doFill && CanElementFill())
                 {
                     Color  rgbColor     = ColorConstants.BLACK;
                     float  fillOpacity  = generalOpacity;
                     String opacityValue = GetAttribute(SvgConstants.Attributes.FILL_OPACITY);
                     if (opacityValue != null && !SvgConstants.Values.NONE.EqualsIgnoreCase(opacityValue))
                     {
                         fillOpacity *= float.Parse(opacityValue, System.Globalization.CultureInfo.InvariantCulture);
                     }
                     if (fillRawValue != null)
                     {
                         fillOpacity *= GetAlphaFromRGBA(fillRawValue);
                         rgbColor     = WebColors.GetRGBColor(fillRawValue);
                     }
                     if (!CssUtils.CompareFloats(fillOpacity, 1f))
                     {
                         opacityGraphicsState.SetFillOpacity(fillOpacity);
                     }
                     currentCanvas.SetFillColor(rgbColor);
                 }
             }
             {
                 // stroke
                 String strokeRawValue = GetAttribute(SvgConstants.Attributes.STROKE);
                 if (!SvgConstants.Values.NONE.EqualsIgnoreCase(strokeRawValue))
                 {
                     if (strokeRawValue != null)
                     {
                         Color  rgbColor      = WebColors.GetRGBColor(strokeRawValue);
                         float  strokeOpacity = generalOpacity;
                         String opacityValue  = GetAttribute(SvgConstants.Attributes.STROKE_OPACITY);
                         if (opacityValue != null && !SvgConstants.Values.NONE.EqualsIgnoreCase(opacityValue))
                         {
                             strokeOpacity *= float.Parse(opacityValue, System.Globalization.CultureInfo.InvariantCulture);
                         }
                         strokeOpacity *= GetAlphaFromRGBA(strokeRawValue);
                         if (!CssUtils.CompareFloats(strokeOpacity, 1f))
                         {
                             opacityGraphicsState.SetStrokeOpacity(strokeOpacity);
                         }
                         currentCanvas.SetStrokeColor(rgbColor);
                         String strokeWidthRawValue = GetAttribute(SvgConstants.Attributes.STROKE_WIDTH);
                         // 1 px = 0,75 pt
                         float strokeWidth = 0.75f;
                         if (strokeWidthRawValue != null)
                         {
                             strokeWidth = CssUtils.ParseAbsoluteLength(strokeWidthRawValue);
                         }
                         currentCanvas.SetLineWidth(strokeWidth);
                         doStroke = true;
                     }
                 }
             }
             {
                 // opacity
                 if (!opacityGraphicsState.GetPdfObject().IsEmpty())
                 {
                     currentCanvas.SetExtGState(opacityGraphicsState);
                 }
             }
         }
     }
 }