/// <summary> /// Gets a PdfExtGState with the key 'CA' set to the specified alpha value. /// </summary> public PdfExtGState GetExtGStateStroke(double alpha) { string key = MakeKey(alpha); PdfExtGState extGState = this.strokeAlphaValues[key] as PdfExtGState; if (extGState == null) { extGState = new PdfExtGState(this.owner); extGState.Elements[PdfExtGState.Keys.CA] = new PdfReal(alpha); this.strokeAlphaValues[key] = extGState; } return extGState; }
/// <summary> /// Gets a PdfExtGState with the key 'CA' set to the specified alpha value. /// </summary> public PdfExtGState GetExtGStateStroke(double alpha) { string key = MakeKey(alpha); PdfExtGState extGState; if (!this.strokeAlphaValues.TryGetValue(key, out extGState)) { extGState = new PdfExtGState(this.owner); extGState.Elements[PdfExtGState.Keys.CA] = new PdfReal(alpha); this.strokeAlphaValues[key] = extGState; } return extGState; }
/// <summary> /// Gets a PdfExtGState with the key 'ca' set to the specified alpha value. /// </summary> public PdfExtGState GetExtGStateNonStroke(double alpha) { string key = MakeKey(alpha); PdfExtGState extGState;; if (!this.nonStrokeAlphaValues.TryGetValue(key, out extGState)) { extGState = new PdfExtGState(this.owner); extGState.Elements[PdfExtGState.Keys.ca] = new PdfReal(alpha); this.nonStrokeAlphaValues[key] = extGState; } return(extGState); }
/// <summary> /// Gets a PdfExtGState with the key 'ca' set to the specified alpha value. /// </summary> public PdfExtGState GetExtGStateNonStroke(double alpha) { string key = MakeKey(alpha); PdfExtGState extGState = this.nonStrokeAlphaValues[key] as PdfExtGState; if (extGState == null) { extGState = new PdfExtGState(this.document); extGState.Elements[PdfExtGState.Keys.ca] = new PdfReal(alpha); this.nonStrokeAlphaValues[key] = extGState; } return(extGState); }
/// <summary> /// Gets a PdfExtGState with the keys 'CA' and 'ca' set to the specified alpha value. /// </summary> public PdfExtGState GetExtGState(double alpha) { string key = MakeKey(alpha); PdfExtGState extGState; if (!alphaValues.TryGetValue(key, out extGState)) { extGState = new PdfExtGState(owner); extGState.Elements[PdfExtGState.Keys.CA] = new PdfReal(alpha); extGState.Elements[PdfExtGState.Keys.ca] = new PdfReal(alpha); alphaValues[key] = extGState; } return(extGState); }
/// <summary> /// Adds the specified graphics state to this resource dictionary /// and returns its local resource name. /// </summary> public string AddExtGState(PdfExtGState extGState) { string name; if (!_resources.TryGetValue(extGState, out name)) { name = NextExtGStateName; _resources[extGState] = name; if (extGState.Reference == null) { Owner._irefTable.Add(extGState); } ExtGStates.Elements[name] = extGState.Reference; } return(name); }
/// <summary> /// Gets a PdfExtGState with the key 'CA' set to the specified alpha value. /// </summary> public PdfExtGState GetExtGStateStroke(double alpha, bool overprint) { string key = PdfExtGState.MakeKey(alpha, overprint); PdfExtGState extGState; if (!_strokeAlphaValues.TryGetValue(key, out extGState)) { extGState = new PdfExtGState(Owner); //extGState.Elements[PdfExtGState.Keys.CA] = new PdfReal(alpha); extGState.StrokeAlpha = alpha; if (overprint) { extGState.StrokeOverprint = true; extGState.Elements.SetInteger(PdfExtGState.Keys.OPM, 1); } _strokeAlphaValues[key] = extGState; } return extGState; }
/// <summary> /// Gets a PdfExtGState with the key 'CA' set to the specified alpha value. /// </summary> public PdfExtGState GetExtGStateStroke(double alpha, bool overprint) { string key = PdfExtGState.MakeKey(alpha, overprint); PdfExtGState extGState; if (!_strokeAlphaValues.TryGetValue(key, out extGState)) { extGState = new PdfExtGState(Owner); //extGState.Elements[PdfExtGState.Keys.CA] = new PdfReal(alpha); extGState.StrokeAlpha = alpha; if (overprint) { extGState.StrokeOverprint = true; extGState.Elements.SetInteger(PdfExtGState.Keys.OPM, 1); } _strokeAlphaValues[key] = extGState; } return(extGState); }
/// <summary> /// Realizes the specified graphic state. /// </summary> public void RealizeExtGState(PdfExtGState xgState) { string gsName = this.writer.Resources.AddExtGState(xgState); this.writer.WriteLiteral(gsName + " gs\n"); }
/// <summary> /// Writes a Path to the content stream. /// </summary> private void WritePath(Path path) { WriteSaveState("begin Path", path.Name); // Transform also affects clipping and opacity mask if (path.RenderTransform != null && this.renderMode == RenderMode.Default) { MultiplyTransform(path.RenderTransform); WriteRenderTransform(path.RenderTransform); } if (path.Clip != null && this.renderMode == RenderMode.Default) WriteClip(path.Clip); if (path.Opacity < 1) MultiplyOpacity(path.Opacity); if (path.OpacityMask != null) WriteOpacityMask(path.OpacityMask); if (path.Fill == null) { if (path.Stroke != null) { #if true WriteStrokeGeometry(path); #else // Just stroke the path RealizeStroke(path); WriteGeometry(path.Data); WritePathFillStroke(path); #endif } else Debug.Assert(false, "??? Path with neither Stroke nor Fill encountered."); } else { SolidColorBrush sBrush; LinearGradientBrush lgBrush; RadialGradientBrush rgBrush; ImageBrush iBrush; VisualBrush vBrush; if ((sBrush = path.Fill as SolidColorBrush) != null) { Color color = sBrush.Color; double opacity = Opacity * color.ScA; if (opacity < 1) RealizeFillOpacity(opacity); WriteRgb(color, " rg\n"); if (path.Stroke != null) RealizeStroke(path); WriteGeometry(path.Data); WritePathFillStroke(path); } else if ((lgBrush = path.Fill as LinearGradientBrush) != null) { // TODO: For better visual compatibility use a Shading Pattern if Opacity is not 1 and // the Stroke Style is not solid. Acrobat 8 ignores this case. PdfExtGState xgState = Context.PdfDocument.Internals.CreateIndirectObject<PdfExtGState>(); xgState.SetDefault1(); double opacity = Opacity * lgBrush.Opacity; ; if (opacity < 1) { xgState.StrokeAlpha = opacity; xgState.NonStrokeAlpha = opacity; } RealizeExtGState(xgState); // 1st draw fill if (lgBrush.GradientStops.HasTransparency) { // Create a FormXObject with a soft mask PdfFormXObject form = LinearShadingBuilder.BuildFormFromLinearGradientBrush(Context, lgBrush, path.Data); string foName = Resources.AddForm(form); WriteLiteral(foName + " Do\n"); } else { // Create just a shading PdfShading shading = LinearShadingBuilder.BuildShadingFromLinearGradientBrush(Context, lgBrush); string shName = Resources.AddShading(shading); WriteLiteral("q\n"); WriteClip(path.Data); WriteLiteral("BX " + shName + " sh EX Q\n"); } // 2nd draw stroke if (path.Stroke != null) WriteStrokeGeometry(path); } else if ((rgBrush = path.Fill as RadialGradientBrush) != null) { PdfExtGState xgState = Context.PdfDocument.Internals.CreateIndirectObject<PdfExtGState>(); xgState.SetDefault1(); double avGradientOpacity = rgBrush.GradientStops.GetAverageAlpha(); // HACK double opacity = Opacity * rgBrush.Opacity * avGradientOpacity; if (opacity < 1) { xgState.StrokeAlpha = opacity; xgState.NonStrokeAlpha = opacity; } //RealizeExtGState(xgState); #if true XRect boundingBox = path.Data.GetBoundingBox(); // Always creates a pattern, because the background must be filled PdfShadingPattern pattern = RadialShadingBuilder.BuildFromRadialGradientBrush(Context, rgBrush, boundingBox, Transform); string paName = Resources.AddPattern(pattern); // stream // /CS0 cs /P0 scn // /GS0 gs // 0 480 180 -90 re // f* // endstream // endobj WriteLiteral("/Pattern cs " + paName + " scn\n"); // move to here: RealizeExtGState(xgState); RealizeExtGState(xgState); WriteGeometry(path.Data); if (path.Data.FillRule == FillRule.NonZero) // NonZero means Winding WriteLiteral("f\n"); else WriteLiteral("f*\n"); #else #if true // 1st draw fill if (rgBrush.GradientStops.HasTransparentColors) { // Create a FormXObject with a soft mask PdfFormXObject form = ShadingBuilder.BuildFormFromRadialGradientBrush(Context, rgBrush, path.Data); string foName = Resources.AddForm(form); WriteLiteral(foName + " Do\n"); } else { // Create just a shading PdfShading shading = ShadingBuilder.BuildShadingFromRadialGradientBrush(Context, rgBrush); string shName = Resources.AddShading(shading); WriteLiteral("q\n"); WriteClip(path.Data); WriteLiteral("BX " + shName + " sh EX Q\n"); } #else // Establish graphic state dictionary PdfExtGState extGState = new PdfExtGState(Context.PdfDocument); Context.PdfDocument.Internals.AddObject(extGState); string gsName = Resources.AddExtGState(extGState); WriteLiteral(gsName + " gs\n"); // 1st draw fill PdfShading shading = ShadingBuilder.BuildFormFromRadialGradientBrush(Context, rgBrush); //, extGState); string shName = Resources.AddShading(shading); WriteClip(path.Data); WriteLiteral("BX " + shName + " sh EX\n"); #endif #endif // 2nd draw stroke if (path.Stroke != null) WriteStrokeGeometry(path); } else if ((iBrush = path.Fill as ImageBrush) != null) { PdfExtGState xgState = Context.PdfDocument.Internals.CreateIndirectObject<PdfExtGState>(); xgState.SetDefault1(); double opacity = Opacity * iBrush.Opacity; if (opacity < 1) { xgState.StrokeAlpha = opacity; xgState.NonStrokeAlpha = opacity; } RealizeExtGState(xgState); // 1st draw fill PdfTilingPattern pattern = TilingPatternBuilder.BuildFromImageBrush(Context, iBrush, Transform); string name = Resources.AddPattern(pattern); WriteLiteral("/Pattern cs " + name + " scn\n"); WriteGeometry(path.Data); WritePathFillStroke(path); // 2nd draw stroke if (path.Stroke != null) WriteStrokeGeometry(path); } else if ((vBrush = path.Fill as VisualBrush) != null) { PdfExtGState xgState = Context.PdfDocument.Internals.CreateIndirectObject<PdfExtGState>(); xgState.SetDefault1(); double opacity = Opacity * vBrush.Opacity; if (opacity < 1) { xgState.StrokeAlpha = opacity; xgState.NonStrokeAlpha = opacity; } RealizeExtGState(xgState); // 1st draw fill PdfTilingPattern pattern = TilingPatternBuilder.BuildFromVisualBrush(Context, vBrush, Transform); string name = Resources.AddPattern(pattern); WriteLiteral("/Pattern cs " + name + " scn\n"); WriteGeometry(path.Data); WritePathFillStroke(path); // 2nd draw stroke if (path.Stroke != null) WriteStrokeGeometry(path); } else { Debug.Assert(false, "Unknown brush type encountered."); } } WriteRestoreState("end Path", path.Name); }
/// <summary> /// Realizes the specified graphic state. /// </summary> public void RealizeExtGState(PdfExtGState xgState) { this.graphicsState.RealizeExtGState(xgState); }
/// <summary> /// Adds the specified graphics state to this resource dictionary /// and returns its local resource name. /// </summary> public string AddExtGState(PdfExtGState extGState) { string name; if (!this.resources.TryGetValue(extGState, out name)) { name = NextExtGStateName; this.resources[extGState] = name; if (extGState.Reference == null) Owner.irefTable.Add(extGState); ExtGStates.Elements[name] = extGState.Reference; } return name; }
void RealizeLinearGradientBrush(LinearGradientBrush brush, XForm xform) { XMatrix matrix = this.currentTransform; PdfShadingPattern pattern = new PdfShadingPattern(this.writer.Owner); pattern.Elements[PdfShadingPattern.Keys.PatternType] = new PdfInteger(2); // shading pattern // Setup shading PdfShading shading = new PdfShading(this.writer.Owner); PdfColorMode colorMode = PdfColorMode.Rgb; //this.document.Options.ColorMode; PdfDictionary function = BuildShadingFunction(brush.GradientStops, colorMode); function.Elements.SetString("/@", "This is the shading function of a LinearGradientBrush"); shading.Elements[PdfShading.Keys.Function] = function; shading.Elements[PdfShading.Keys.ShadingType] = new PdfInteger(2); // Axial shading //if (colorMode != PdfColorMode.Cmyk) shading.Elements[PdfShading.Keys.ColorSpace] = new PdfName("/DeviceRGB"); //else //shading.Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK"); //double x1 = 0, y1 = 0, x2 = 0, y2 = 0; double x1 = brush.StartPoint.X; double y1 = brush.StartPoint.Y; double x2 = brush.EndPoint.X; double y2 = brush.EndPoint.Y; shading.Elements[PdfShading.Keys.Coords] = new PdfLiteral("[{0:0.###} {1:0.###} {2:0.###} {3:0.###}]", x1, y1, x2, y2); // old: Elements[Keys.Background] = new PdfRawItem("[0 1 1]"); // old: Elements[Keys.Domain] = shading.Elements[PdfShading.Keys.Extend] = new PdfLiteral("[true true]"); // Setup pattern pattern.Elements[PdfShadingPattern.Keys.Shading] = shading; pattern.Elements[PdfShadingPattern.Keys.Matrix] = PdfLiteral.FromMatrix(matrix); // new PdfLiteral("[" + PdfEncoders.ToString(matrix) + "]"); string name = this.writer.Resources.AddPattern(pattern); this.writer.WriteLiteral("/Pattern cs\n", name); this.writer.WriteLiteral("{0} scn\n", name); double alpha = brush.Opacity * brush.GradientStops.GetAverageAlpha(); if (alpha < 1 && this.writer.renderMode == RenderMode.Default) { #if true PdfExtGState extGState = this.writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha); string gs = this.writer.Resources.AddExtGState(extGState); this.writer.WriteLiteral("{0} gs\n", gs); #else #if true if (xform == null) { PdfExtGState extGState = this.writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha); string gs = this.writer.Resources.AddExtGState(extGState); this.writer.WriteGraphicState(extGState); } else { //PdfFormXObject pdfForm = this.writer.Owner.FormTable.GetForm(form); PdfFormXObject pdfForm = xform.pdfForm; pdfForm.Elements.SetString("/@", "This is the Form XObject of the soft mask"); string formName = this.writer.Resources.AddForm(pdfForm); PdfTransparencyGroupAttributes tgAttributes = new PdfTransparencyGroupAttributes(this.writer.Owner); //this.writer.Owner.Internals.AddObject(tgAttributes); tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceRGB"); // Set reference to transparency group attributes pdfForm.Elements.SetObject(PdfFormXObject.Keys.Group, tgAttributes); pdfForm.Elements[PdfFormXObject.Keys.Matrix] = new PdfLiteral("[1.001 0 0 1.001 0.001 0.001]"); PdfSoftMask softmask = new PdfSoftMask(this.writer.Owner); this.writer.Owner.Internals.AddObject(softmask); softmask.Elements.SetString("/@", "This is the soft mask"); softmask.Elements.SetName(PdfSoftMask.Keys.S, "/Luminosity"); softmask.Elements.SetReference(PdfSoftMask.Keys.G, pdfForm); //pdfForm.Elements.SetName(PdfFormXObject.Keys.Type, "Group"); //pdfForm.Elements.SetName(PdfFormXObject.Keys.ss.Ss.Type, "Group"); PdfExtGState extGState = new PdfExtGState(this.writer.Owner); this.writer.Owner.Internals.AddObject(extGState); extGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask); this.writer.WriteGraphicState(extGState); } #else XForm form = new XForm(this.writer.Owner, 220, 140); XGraphics formGfx = XGraphics.FromForm(form); // draw something //XSolidBrush xbrush = new XSolidBrush(XColor.FromArgb(128, 128, 128)); XLinearGradientBrush xbrush = new XLinearGradientBrush(new XPoint(0, 0), new XPoint(220,0), XColors.White, XColors.Black); formGfx.DrawRectangle(xbrush, -10000, -10000, 20000, 20000); //formGfx.DrawString("Text, Graphics, Images, and Forms", new XFont("Verdana", 10, XFontStyle.Regular), XBrushes.Navy, 3, 0, XStringFormat.TopLeft); formGfx.Dispose(); // Close form form.Finish(); PdfFormXObject pdfForm = this.writer.Owner.FormTable.GetForm(form); string formName = this.writer.Resources.AddForm(pdfForm); //double x = 20, y = 20; //double cx = 1; //double cy = 1; //this.writer.AppendFormat("q {2:0.###} 0 0 -{3:0.###} {0:0.###} {4:0.###} cm 100 Tz {5} Do Q\n", // x, y, cx, cy, y + 0, formName); //this.writer.AppendFormat("q {2:0.###} 0 0 -{3:0.###} {0:0.###} {4:0.###} cm 100 Tz {5} Do Q\n", // x, y, cx, cy, y + 220/1.5, formName); PdfTransparencyGroupAttributes tgAttributes = new PdfTransparencyGroupAttributes(this.writer.Owner); this.writer.Owner.Internals.AddObject(tgAttributes); tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceRGB"); // Set reference to transparency group attributes pdfForm.Elements.SetReference(PdfFormXObject.Keys.Group, tgAttributes); PdfSoftMask softmask = new PdfSoftMask(this.writer.Owner); this.writer.Owner.Internals.AddObject(softmask); softmask.Elements.SetName(PdfSoftMask.Keys.S, "/Luminosity"); softmask.Elements.SetReference(PdfSoftMask.Keys.G, pdfForm); //pdfForm.Elements.SetName(PdfFormXObject.Keys.Type, "Group"); //pdfForm.Elements.SetName(PdfFormXObject.Keys.ss.Ss.Type, "Group"); PdfExtGState extGState = new PdfExtGState(this.writer.Owner); this.writer.Owner.Internals.AddObject(extGState); extGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask); this.writer.WriteGraphicState(extGState); #endif #endif } }
void RealizeRadialGradientBrush(RadialGradientBrush brush, XForm xform) { XMatrix matrix = new XMatrix(); // this.renderer.defaultViewMatrix; //matrix.MultiplyPrepend(this.Transform); matrix = this.currentTransform; //matrix.MultiplyPrepend(XMatrix.CreateScaling(1.3, 1)); PdfShadingPattern pattern = new PdfShadingPattern(this.writer.Owner); pattern.Elements[PdfShadingPattern.Keys.PatternType] = new PdfInteger(2); // shading pattern // Setup shading PdfShading shading = new PdfShading(this.writer.Owner); PdfColorMode colorMode = PdfColorMode.Rgb; //this.document.Options.ColorMode; PdfDictionary function = BuildShadingFunction(brush.GradientStops, colorMode); shading.Elements[PdfShading.Keys.ShadingType] = new PdfInteger(3); // Radial shading //if (colorMode != PdfColorMode.Cmyk) shading.Elements[PdfShading.Keys.ColorSpace] = new PdfName("/DeviceRGB"); //else //shading.Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK"); double x0 = brush.GradientOrigin.X; double y0 = brush.GradientOrigin.Y; double r0 = 0; double x1 = brush.Center.X; double y1 = brush.Center.Y; double r1 = brush.RadiusX; shading.Elements[PdfShading.Keys.Coords] = new PdfLiteral("[{0:0.###} {1:0.###} {2:0.###} {3:0.###} {4:0.###} {5:0.###}]", x0, y0, r0, x1, y1, r1); // old: Elements[Keys.Background] = new PdfRawItem("[0 1 1]"); // old: Elements[Keys.Domain] = shading.Elements[PdfShading.Keys.Function] = function; shading.Elements[PdfShading.Keys.Extend] = new PdfLiteral("[true true]"); // Setup pattern pattern.Elements[PdfShadingPattern.Keys.Shading] = shading; pattern.Elements[PdfShadingPattern.Keys.Matrix] = PdfLiteral.FromMatrix(matrix); // new PdfLiteral("[" + PdfEncoders.ToString(matrix) + "]"); string name = this.writer.Resources.AddPattern(pattern); this.writer.WriteLiteral("/Pattern cs\n", name); this.writer.WriteLiteral("{0} scn\n", name); double alpha = brush.Opacity * brush.GradientStops.GetAverageAlpha(); if (alpha < 1) { #if true PdfExtGState extGState = this.writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha); string gs = this.writer.Resources.AddExtGState(extGState); this.writer.WriteLiteral("{0} gs\n", gs); #else if (xform == null) { PdfExtGState extGState = this.writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha); string gs = this.writer.Resources.AddExtGState(extGState); this.writer.WriteGraphicState(extGState); } else { //PdfFormXObject pdfForm = this.writer.Owner.FormTable.GetForm(form); PdfFormXObject pdfForm = xform.pdfForm; pdfForm.Elements.SetString("/@", "This is the Form XObject of the soft mask"); string formName = this.writer.Resources.AddForm(pdfForm); PdfTransparencyGroupAttributes tgAttributes = new PdfTransparencyGroupAttributes(this.writer.Owner); //this.writer.Owner.Internals.AddObject(tgAttributes); tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceRGB"); // Set reference to transparency group attributes pdfForm.Elements.SetObject(PdfFormXObject.Keys.Group, tgAttributes); pdfForm.Elements[PdfFormXObject.Keys.Matrix] = new PdfLiteral("[1.001 0 0 1.001 0.001 0.001]"); PdfSoftMask softmask = new PdfSoftMask(this.writer.Owner); this.writer.Owner.Internals.AddObject(softmask); softmask.Elements.SetString("/@", "This is the soft mask"); softmask.Elements.SetName(PdfSoftMask.Keys.S, "/Luminosity"); softmask.Elements.SetReference(PdfSoftMask.Keys.G, pdfForm); //pdfForm.Elements.SetName(PdfFormXObject.Keys.Type, "Group"); //pdfForm.Elements.SetName(PdfFormXObject.Keys.ss.Ss.Type, "Group"); PdfExtGState extGState = new PdfExtGState(this.writer.Owner); this.writer.Owner.Internals.AddObject(extGState); extGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask); this.writer.WriteGraphicState(extGState); } #endif } }
/// <summary> /// Adds the specified graphics state to this resource dictionary /// and returns its local resource name. /// </summary> public string AddExtGState(PdfExtGState extGState) { string name = (string)this.resources[extGState]; if (name == null) { name = NextExtGStateName; this.resources[extGState] = name; if (extGState.Reference == null) this.Owner.irefTable.Add(extGState); ExtGStates.Elements[name] = extGState.Reference; } return name; }
public void WriteGraphicsState(PdfExtGState extGState) { string name = Resources.AddExtGState(extGState); WriteLiteral(name + " gs\n"); }