// =========================================================================== public override void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 PdfContentByte canvas = writer.DirectContent; PdfShading axial = PdfShading.SimpleAxial(writer, 36, 716, 396, 788, BaseColor.ORANGE, BaseColor.BLUE ); canvas.PaintShading(axial); document.NewPage(); PdfShading radial = PdfShading.SimpleRadial(writer, 200, 700, 50, 300, 700, 100, new BaseColor(0xFF, 0xF7, 0x94), new BaseColor(0xF7, 0x8A, 0x6B), false, false ); canvas.PaintShading(radial); PdfShadingPattern shading = new PdfShadingPattern(axial); ColorRectangle(canvas, new ShadingColor(shading), 150, 420, 126, 126); canvas.SetShadingFill(shading); canvas.Rectangle(300, 420, 126, 126); canvas.FillStroke(); } }
/// <summary> /// Builds a shading from a linear gradient brush. /// </summary> public static PdfShading BuildShadingFromLinearGradientBrush(DocumentRenderingContext context, LinearGradientBrush brush) { LinearShadingBuilder builder = new LinearShadingBuilder(context); PdfShading shading = builder.BuildShading(brush); return(shading); }
private static Color?GetColor(PdfPattern.Shading shading, float alpha) { var shadingDict = shading.GetShading(); var shadingConstructed = PdfShading.MakeShading(shadingDict); switch (shadingConstructed) { case PdfShading.Axial axial: var axialFunction = (PdfDictionary)axial.GetFunction(); GetFunctionDetails(axialFunction); var coords = GetItemsFloat(axial.GetCoords()); var domain = GetItemsFloat(axial.GetDomain()); var extend = GetItemsBool(axial.GetExtend()); break; case PdfShading.CoonsPatchMesh coonsPatchMesh: break; case PdfShading.FreeFormGouraudShadedTriangleMesh freeFormGouraudShadedTriangleMesh: break; case PdfShading.FunctionBased functionBased: break; case PdfShading.LatticeFormGouraudShadedTriangleMesh latticeFormGouraudShadedTriangleMesh: break; case PdfShading.Radial radial: break; case PdfShading.TensorProductPatchMesh tensorProductPatchMesh: break; } return(null); }
/// <summary> /// Adds the shading. /// </summary> /// <param name="shading">The shading.</param> /// <returns></returns> public PdfName AddShading(PdfShading shading) { if (this.shadings.ContainsKey(shading)) { return(this.shadings[shading]); } PdfName name = new PdfName("S" + ((int)this.shadings.Count), true); this.shadings.Add(shading, name); return(name); }
private void setPaint(Paint paint, boolean invert, double xoffset, double yoffset) { this.paint = paint; if (paint is Color) { cb.setColorFill((Color)paint); } else if (paint is GradientPaint) { GradientPaint gp = (GradientPaint)paint; Point2D p1 = gp.getPoint1(); transform.transform(p1, p1); Point2D p2 = gp.getPoint2(); transform.transform(p2, p2); Color c1 = gp.getColor1(); Color c2 = gp.getColor2(); PdfShading shading = PdfShading.simpleAxial(cb.getPdfWriter(), (float)p1.getX(), (float)p1.getY(), (float)p2.getX(), (float)p2.getY(), c1, c2); PdfShadingPattern pat = new PdfShadingPattern(shading); cb.setShadingFill(pat); } else { try { BufferedImage img = null; int type = BufferedImage.TYPE_4BYTE_ABGR; if (paint.getTransparency() == Transparency.OPAQUE) { type = BufferedImage.TYPE_3BYTE_BGR; } img = new BufferedImage((int)width, (int)height, type); Graphics2D g = (Graphics2D)img.getGraphics(); Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()); g.setPaint(paint); g.fill(fillRect); if (invert) { AffineTransform tx = new AffineTransform(); tx.scale(1, -1); tx.translate(-xoffset, -yoffset); g.drawImage(img, tx, null); } com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null); PdfPatternPainter pattern = cb.createPattern(width, height); image.setAbsolutePosition(0, 0); pattern.addImage(image); cb.setPatternFill(pattern); } catch (Exception ex) { cb.setColorFill(Color.gray); } } }
private void Form1_Load(object sender, EventArgs e) { //Test file name string TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"); //Standard iTextSharp setup using (FileStream fs = new FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (Document doc = new Document(PageSize.LETTER)) { using (PdfWriter w = PdfWriter.GetInstance(doc, fs)) { //Open the document for writing doc.Open(); //Create a shading object. The (x,y)'s appear to be document-level instead of cell-level so they need to be played with PdfShading shading = PdfShading.SimpleAxial(w, 0, 700, 300, 700, BaseColor.BLUE, BaseColor.RED); //Create a pattern from our shading object PdfShadingPattern pattern = new PdfShadingPattern(shading); //Create a color from our patter ShadingColor color = new ShadingColor(pattern); //Create a standard two column table PdfPTable t = new PdfPTable(2); //Add a text cell setting the background color through object initialization t.AddCell(new PdfPCell(new Phrase("Hello")) { BackgroundColor = color }); //Add another cell with everything inline. Notice that the (x,y)'s have been updated t.AddCell(new PdfPCell(new Phrase("World")) { BackgroundColor = new ShadingColor(new PdfShadingPattern(PdfShading.SimpleAxial(w, 400, 700, 600, 700, BaseColor.PINK, BaseColor.CYAN))) }); //Add the table to the document doc.Add(t); //Close the document doc.Close(); } } } this.Close(); }
public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { BaseColor gradientStart = ...; BaseColor gradientEnd = ...); float[] topWidths = widths[0]; PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; Rectangle rectangle = new Rectangle(topWidths[0], heights[heights.Length - 1], topWidths[topWidths.Length - 1], heights[0]); PdfShading shading = PdfShading.SimpleAxial(writer, rectangle.Left, rectangle.Top, rectangle.Left, rectangle.Bottom, gradientStart, gradientEnd); PdfShadingPattern pattern = new PdfShadingPattern(shading); cb.SetShadingFill(pattern); cb.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height); cb.Fill(); }
PdfShading BuildShading(RadialGradientBrush brush, double scaleX, double scaleY) { // Setup shading //<< // /AntiAlias false // /ColorSpace 12 0 R // /Coords [120 120 120 120 120 0] // /Domain [0 1] // /Extend [false false] // /Function 14 0 R // /ShadingType 3 //>> PdfShading shading = Context.PdfDocument.Internals.CreateIndirectObject <PdfShading>(); shading.Elements.SetInteger(PdfShading.Keys.ShadingType, 3); // Radial shading PdfColorMode colorMode = PdfColorMode.Rgb; //this.document.Options.ColorMode; if (colorMode != PdfColorMode.Cmyk) { shading.Elements[PdfShading.Keys.ColorSpace] = new PdfName("/DeviceRGB"); } else { shading.Elements[PdfShading.Keys.ColorSpace] = new PdfName("/DeviceCMYK"); } //bool invers = true; PdfDictionary function = null; // BuildShadingFunction(brush.GradientStops, false, colorMode); #if DEBUG_ if (DevHelper.RenderComments) { funcRegular.Elements.SetString("/@comment", "This is the shading function of a RadialGradientBrushPattern"); } #endif shading.Elements[PdfShading.Keys.Function] = function; shading.Elements[PdfShading.Keys.Extend] = new PdfLiteral("[false false]"); double x0 = brush.Center.X / scaleX; double y0 = brush.Center.Y / scaleY; double r0 = Math.Max(brush.RadiusX, brush.RadiusY); double x1 = brush.GradientOrigin.X / scaleX; double y1 = brush.GradientOrigin.Y / scaleY; double r1 = 0; 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); return(shading); }
/// <summary> /// Builds a PdfShading from the specified brush. If a gradient contains transparency, a soft mask is created an added to the /// specified graphic state. /// </summary> PdfShading BuildShading(LinearGradientBrush brush) { // Setup shading PdfShading shading = new PdfShading(Context.PdfDocument); #if DEBUG if (DevHelper.RenderComments) { shading.Elements.SetString("/@comment", "This is the shading of a LinearGradientBrush"); } #endif PdfColorMode colorMode = PdfColorMode.Rgb; //this.document.Options.ColorMode; PdfDictionary function = BuildShadingFunction(brush.GradientStops, false, colorMode); #if DEBUG if (DevHelper.RenderComments) { function.Elements.SetString("/@comment", "This is the shading function of a LinearGradientBrush"); } #endif shading.Elements.SetBoolean(PdfShading.Keys.AntiAlias, false); shading.Elements[PdfShading.Keys.Function] = function; shading.Elements.SetInteger(PdfShading.Keys.ShadingType, 2); // Axial shading shading.Elements.SetName(PdfShading.Keys.ColorSpace, "/DeviceRGB"); // TODO: respect ColorMode 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); shading.Elements[PdfShading.Keys.Domain] = new PdfLiteral("[0 1]"); if (brush.SpreadMethod == SpreadMethod.Pad) { shading.Elements[PdfShading.Keys.Extend] = new PdfLiteral("[true true]"); } else { DevHelper.NotImplemented("SpreadMethod." + brush.SpreadMethod.ToString() + " Background painted in green."); #if DEBUG // Note from PDF reference: The background color is applied only when the shading is used as part of // a shading pattern, not when it is painted directly with the sh operator. shading.Elements[PdfShading.Keys.Background] = new PdfLiteral("[0 1 0]"); // TODO: respect ColorMode #else // Best we can currently do shading.Elements[PdfShading.Keys.Extend] = new PdfLiteral("[true true]"); #endif } return(shading); }
// --------------------------------------------------------------------------- /** * Prints a square and fills half of it with a gray rectangle. * * @param x * @param y * @param cb * @throws Exception */ public static void PictureBackdrop(float x, float y, PdfContentByte cb, PdfWriter writer) { PdfShading axial = PdfShading.SimpleAxial(writer, x, y, x + 200, y, BaseColor.YELLOW, BaseColor.RED ); PdfShadingPattern axialPattern = new PdfShadingPattern(axial); cb.SetShadingFill(axialPattern); cb.SetColorStroke(BaseColor.BLACK); cb.SetLineWidth(2); cb.Rectangle(x, y, 200, 200); cb.FillStroke(); }
/// <summary> /// Builds a monochrome shading for a form XObject of a soft mask. /// </summary> PdfShading BuildShadingForSoftMask(RadialGradientBrush brush) { // Setup shading //<< // /ShadingType 2 // /AntiAlias false // /BBox [0 0 510.236 680.315] // /ColorSpace /DeviceGray // /Coords [5 5 153.492 -86.924] // /Domain [0 1] // /Extend [true true] // /Function 18 0 R //>> PdfShading shading = Context.PdfDocument.Internals.CreateIndirectObject <PdfShading>(); #if DEBUG if (DevHelper.RenderComments) { shading.Elements.SetString("/@comment", "This is the shading function of a soft mask"); } #endif shading.Elements.SetInteger(PdfShading.Keys.ShadingType, 2); // Axial shading shading.Elements.SetBoolean(PdfShading.Keys.AntiAlias, false); // TODO: BBox full page //shading.Elements.SetValue(PdfShading.Keys.BBox, new PdfLiteral("[0 0 480 640]")); shading.Elements.SetName(PdfShading.Keys.ColorSpace, "/DeviceGray"); ////double x1 = brush.StartPoint.X; ////double y1 = brush.StartPoint.Y; ////double x2 = brush.EndPoint.X; ////double y2 = brush.EndPoint.Y; ////shading.Elements.SetValue(PdfShading.Keys.Coords, new PdfLiteral("[{0:0.####} {1:0.####} {2:0.####} {3:0.####}]", x1, y1, x2, y2)); shading.Elements.SetValue(PdfShading.Keys.Domain, new PdfLiteral("[0 1]")); shading.Elements.SetValue(PdfShading.Keys.Extend, new PdfLiteral("[true true]")); PdfColorMode colorMode = PdfColorMode.Rgb; //this.document.Options.ColorMode; PdfDictionary func; PdfDictionary function = BuildShadingFunction(brush.GradientStops, true, colorMode, true, out func); #if true shading.Elements.SetValue(PdfShading.Keys.Function, function); #else Context.PdfDocument.Internals.AddObject(function); shading.Elements.SetReference(PdfShading.Keys.Function, function); #endif return(shading); }
public void Verify_ShadingPatternColor_CanBeCreated() { var pdfFilePath = TestUtils.GetOutputFileName(); var stream = new FileStream(pdfFilePath, FileMode.Create); // step 1 var document = new Document(); // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.AddAuthor(TestUtils.Author); document.Open(); // step 4 PdfContentByte canvas = writer.DirectContent; PdfShading axial = PdfShading.SimpleAxial(writer, 36, 716, 396, 788, BaseColor.Orange, BaseColor.Blue ); canvas.PaintShading(axial); document.NewPage(); PdfShading radial = PdfShading.SimpleRadial(writer, 200, 700, 50, 300, 700, 100, new BaseColor(0xFF, 0xF7, 0x94), new BaseColor(0xF7, 0x8A, 0x6B), false, false ); canvas.PaintShading(radial); PdfShadingPattern shading = new PdfShadingPattern(axial); colorRectangle(canvas, new ShadingColor(shading), 150, 420, 126, 126); canvas.SetShadingFill(shading); canvas.Rectangle(300, 420, 126, 126); canvas.FillStroke(); document.Close(); stream.Dispose(); TestUtils.VerifyPdfFileIsReadable(pdfFilePath); }
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { //Create a shading object with cell-specific coords PdfShading shading = PdfShading.SimpleAxial(w, position.Left, position.Bottom, position.Right, position.Top, BaseColor.BLUE, BaseColor.RED); //Create a pattern from our shading object PdfShadingPattern pattern = new PdfShadingPattern(shading); //Create a color from our patter ShadingColor color = new ShadingColor(pattern); //Get the background canvas. NOTE, If using an older version of iTextSharp (4.x) you might need to get the canvas in a different way PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; //Set the background color of the given rectable to our shading pattern position.BackgroundColor = color; //Fill the rectangle cb.Rectangle(position); }
/// <summary> /// Builds a PdfShadingPattern from the specified brush. /// </summary> PdfShadingPattern BuildShadingPattern(LinearGradientBrush brush, XMatrix transform) { //<< // /Matrix [0.75 0 0 -0.75 8 470.4] // /PatternType 2 // /Shading 22 0 R // /Type /Pattern //>> XMatrix matrix = transform; PdfShadingPattern pattern = Context.PdfDocument.Internals.CreateIndirectObject <PdfShadingPattern>(); pattern.Elements.SetInteger(PdfShadingPattern.Keys.PatternType, 2); // Shading pattern.Elements.SetMatrix(PdfShadingPattern.Keys.Matrix, matrix); PdfShading shading = BuildShading(brush); Context.PdfDocument.Internals.AddObject(shading); pattern.Elements.SetReference(PdfShadingPattern.Keys.Shading, shading); return(pattern); }
public virtual void DeviceNCmykRedRgbBlueGradient() { Document document = new Document(); // step 2 String dest_file = DEST_FOLDER + "/device_n_gradient_CmykRedRgbBlue.pdf"; PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(dest_file, FileMode.Create)); // step 3 document.Open(); // step 4 PdfContentByte canvas = writer.DirectContent; PdfSpotColor psc_red = new PdfSpotColor("Spot Red", new CMYKColor(0f, 1f, 1f, 0f)); PdfSpotColor psc_blue = new PdfSpotColor("Spot Blue", new BaseColor(0, 0, 255)); PdfDeviceNColor deviceNColor = new PdfDeviceNColor(new PdfSpotColor[] { psc_red, psc_blue }); canvas.SaveState(); canvas.Rectangle(418, 412, -329, 189); canvas.Clip(); canvas.NewPath(); canvas.SaveState(); canvas.ConcatCTM(329f, 0f, 0f, -329f, 89f, 506.5f); canvas.PaintShading(PdfShading.SimpleAxial(writer, 0, 0, 1, 0, new DeviceNColor(deviceNColor, new float[] { 1, 0 }), new DeviceNColor(deviceNColor, new float[] { 0, 1 }))); canvas.RestoreState(); canvas.RestoreState(); canvas.SetCMYKColorStroke(0, 0, 0, 0xFF); canvas.Rectangle(418, 412, -329, 189); canvas.Stroke(); document.Close(); CompareTool compareTool = new CompareTool(); String error = compareTool.Compare(dest_file, TEST_RESOURCES_PATH + "cmp_device_n_gradient_CmykRedRgbBlue.pdf", DEST_FOLDER, "diff_"); if (error != null) { Assert.Fail(error); } }
/// <summary> /// Draws a rectangular gradient background color. /// </summary> /// <param name="position">The coordinates of the cell</param> /// <param name="canvases">An array of PdfContentByte to add text or graphics</param> /// <param name="startColor">Gradient's Start Color</param> /// <param name="endColor">Gradient's End Color</param> public static void DrawGradientBackground(this Rectangle position, PdfContentByte[] canvases, BaseColor startColor, BaseColor endColor) { if (startColor == null || endColor == null) { return; } var cb = canvases[PdfPTable.BACKGROUNDCANVAS]; cb.SaveState(); var shading = PdfShading.SimpleAxial( cb.PdfWriter, position.Left, position.Top, position.Left, position.Bottom, startColor, endColor); var shadingPattern = new PdfShadingPattern(shading); cb.SetShadingFill(shadingPattern); cb.Rectangle(position.Left, position.Bottom, position.Width, position.Height); cb.Fill(); cb.RestoreState(); }
internal void AddSimpleShading(PdfShading shading) { if (!documentShadings.ContainsKey(shading)) { documentShadings[shading] = null; shading.Name = documentShadings.Count; } }
public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle position, PdfContentByte[] canvases) { if (Helper.IsTransparent(_backgroundBrush)) position.BackgroundColor = null; var gradientBrush = _backgroundBrush as GradientBrush; if (gradientBrush != null) { PdfShading shading = null; var fromColor = PdfPrinter.GetBaseColor(gradientBrush.GradientStops.FirstOrDefault().Color); var toColor = PdfPrinter.GetBaseColor(gradientBrush.GradientStops.LastOrDefault().Color); var linearGradientBrush = _backgroundBrush as LinearGradientBrush; if (linearGradientBrush != null) { float x0, y0, x1, y1; var brushSlopeAbs = Math.Abs(linearGradientBrush.EndPoint.Y - linearGradientBrush.StartPoint.Y) / Math.Abs(linearGradientBrush.EndPoint.X - linearGradientBrush.StartPoint.X); var cellSlopeAbs = position.Height / position.Width; if (linearGradientBrush.EndPoint.X > linearGradientBrush.StartPoint.X) // from left to right { x0 = position.Left; if (linearGradientBrush.EndPoint.Y > linearGradientBrush.StartPoint.Y) // from top to bottom { y0 = position.Top; if (cellSlopeAbs >= brushSlopeAbs) { x1 = position.Left + (float)(position.Height / brushSlopeAbs); y1 = position.Bottom; } else { x1 = position.Right; y1 = position.Top - (float)(position.Width * brushSlopeAbs); } } else // from bottom to top { y0 = position.Bottom; if (cellSlopeAbs >= brushSlopeAbs) { x1 = position.Left + (float)(position.Height / brushSlopeAbs); y1 = position.Top; } else { x1 = position.Right; y1 = position.Bottom + (float)(position.Width * brushSlopeAbs); } } } else // from right to left { x0 = position.Right; if (linearGradientBrush.EndPoint.Y > linearGradientBrush.StartPoint.Y) // from top to bottom { y0 = position.Top; if (cellSlopeAbs >= brushSlopeAbs) { x1 = position.Right - (float)(position.Height / brushSlopeAbs); y1 = position.Bottom; } else { x1 = position.Right; y1 = position.Top - (float)(position.Width * brushSlopeAbs); } } else // from bottom to top { y0 = position.Bottom; if (cellSlopeAbs >= brushSlopeAbs) { x1 = position.Right - (float)(position.Height / brushSlopeAbs); y1 = position.Top; } else { x1 = position.Right; y1 = position.Bottom + (float)(position.Width * brushSlopeAbs); } } } shading = PdfShading.SimpleAxial(_writer, x0, y0, x1, y1, fromColor, toColor, false, false); } var radialGradientBrush = _backgroundBrush as RadialGradientBrush; if (radialGradientBrush != null) { float xCenter = 0, yCenter = 0, radius = 0; shading = PdfShading.SimpleRadial(_writer, xCenter, yCenter, 0, xCenter, yCenter, radius, fromColor, toColor, true, true); } if (shading != null) position.BackgroundColor = new ShadingColor(new PdfShadingPattern(shading)); } //Fill the rectangle canvases[PdfPTable.BACKGROUNDCANVAS].Rectangle(position); }
PdfShadingPattern BuildPattern(RadialGradientBrush brush, XRect boundingBox, XMatrix transform) { //<< // /BBox [0 0 600 760] // /Length 123 // /Matrix [0.75 0 0 -0.75 0 480] // /PaintType 1 // /PatternType 1 // /Resources // << // /ColorSpace // << // /CS0 12 0 R // /CS1 12 0 R // >> // /ExtGState // << // /GS0 10 0 R // /GS1 16 0 R // >> // /Shading // << // /Sh0 15 0 R // >> // >> // /TilingType 3 // /Type /Pattern // /XStep 600 // /YStep 1520 //>> //stream // /CS0 cs 1 0 0 scn // 1 i // /GS0 gs // 0 0 600 759.999 re // f // q // 0 0 600 760 re // W n // q // 0 g // /GS1 gs // 1 0 0 0.5 0 0 cm // BX /Sh0 sh EX Q // Q //endstream XRect bbox = new XRect(-600, -700, 1200, 1520); // HACK XMatrix matrix = transform; //matrix.Prepend(brush.Transform.Matrix); double xStep = 600; double yStep = 1520; // HACK PdfShadingPattern pattern = Context.PdfDocument.Internals.CreateIndirectObject <PdfShadingPattern>(); pattern.Elements.SetInteger(PdfTilingPattern.Keys.PatternType, 1); // Tiling pattern.Elements.SetInteger(PdfTilingPattern.Keys.PaintType, 1); // Color pattern.Elements.SetInteger(PdfTilingPattern.Keys.TilingType, 3); // Constant spacing and faster tiling pattern.Elements.SetMatrix(PdfTilingPattern.Keys.Matrix, matrix); pattern.Elements.SetRectangle(PdfTilingPattern.Keys.BBox, new PdfRectangle(bbox)); pattern.Elements.SetReal(PdfTilingPattern.Keys.XStep, xStep); pattern.Elements.SetReal(PdfTilingPattern.Keys.YStep, yStep); double dx = 2 * brush.RadiusX; double dy = 2 * brush.RadiusY; XRect brushBox = new XRect(brush.Center.X - brush.RadiusX, brush.Center.Y - brush.RadiusY, dx, dy); Debug.Assert(dx * dy > 0, "Radius is 0."); double scaleX, scaleY; if (dx > dy) { scaleX = 1; scaleY = dy / dx; } else { scaleX = dx / dy; scaleY = 1; } PdfColorMode colorMode = PdfColorMode.Rgb; PdfDictionary funcReflected; PdfDictionary funcRegular = BuildShadingFunction(brush.GradientStops, false, colorMode, true, out funcReflected); if (brush.SpreadMethod != SpreadMethod.Pad) { if (CanOptimizeForTwoColors(brush.GradientStops)) { PdfDictionary dummy; funcReflected = BuildShadingFunction(brush.GradientStops, false, colorMode, false, out dummy); } else { Context.PdfDocument.Internals.AddObject(funcRegular); } } //PdfShading shading = BuildShading(brush, scaleX, scaleY); int shadingCount = 1; if (brush.SpreadMethod != SpreadMethod.Pad) { // TODO: Calculate number of required shadings shadingCount = Convert.ToInt32(Math.Max(boundingBox.Width / (2 * brush.RadiusX), boundingBox.Height / (2 * brush.RadiusY)) + 1); // HACK: Rule of thumb, better than nothing shadingCount *= 2; } PdfShading[] shadings = new PdfShading[shadingCount]; // Create the shadings for (int idx = 0; idx < shadingCount; idx++) { PdfShading shading = BuildShading2(brush, scaleX, scaleY, idx, brush.SpreadMethod == SpreadMethod.Reflect && idx % 2 == 1 ? funcReflected : funcRegular); shadings[idx] = shading; } PdfContentWriter writer = new PdfContentWriter(Context, pattern); writer.BeginContentRaw(); // Fill background (even if spread method is not pad) writer.WriteLiteral("q /DeviceRGB cs\n"); //writer.WriteLiteral(PdfEncoders.ToString(clr0, colorMode) + " rg 1 i\n"); writer.WriteLiteral(PdfEncoders.ToString(brush.GradientStops[brush.GradientStops.Count - 1].Color, PdfColorMode.Rgb) + " rg 1 i\n"); writer.WriteLiteral("1 i\n"); PdfExtGState gs = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>(); gs.SetDefault1(); writer.WriteGraphicsState(gs); writer.WriteLiteral("0 0 600 760 re f\n"); XMatrix mat = brush.Transform.Matrix; if (!mat.IsIdentity) { writer.WriteMatrix(mat); } // Just work: silly loop thru shadings for (int idx = shadingCount - 1; idx >= 0; idx--) { writer.WriteLiteral("q 0 0 600 760 re W n\n"); writer.WriteLiteral("q 0 g\n"); gs = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>(); gs.SetDefault2(); writer.WriteGraphicsState(gs); XMatrix transformation = new XMatrix(scaleX, 0, 0, scaleY, 0, 0); writer.WriteMatrix(transformation); string shName = writer.Resources.AddShading(shadings[idx]); writer.WriteLiteral("BX " + shName + " sh EX Q Q\n"); } writer.WriteLiteral("Q\n"); writer.EndContent(); return(pattern); }
/// <summary> /// Builds the soft mask. /// </summary> PdfSoftMask BuildSoftMask(RadialGradientBrush brush) { Debug.Assert(brush.GradientStops.HasTransparency); XRect viewBox = new XRect(0, 0, 360, 480); // HACK //XForm xform = new XForm(Context.PdfDocument, viewBox); PdfFormXObject form = Context.PdfDocument.Internals.CreateIndirectObject <PdfFormXObject>(); #if DEBUG if (DevHelper.RenderComments) { form.Elements.SetString("/@comment", "This is the Form XObject of the soft mask"); } #endif form.Elements.SetRectangle(PdfFormXObject.Keys.BBox, new PdfRectangle(viewBox)); // Transparency group of mask form //<< // /CS /DeviceGray // /I false // /K false // /S /Transparency // /Type /Group //>> PdfTransparencyGroupAttributes tgAttributes = Context.PdfDocument.Internals.CreateIndirectObject <PdfTransparencyGroupAttributes>(); tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceGray"); tgAttributes.Elements.SetBoolean(PdfTransparencyGroupAttributes.Keys.I, false); tgAttributes.Elements.SetBoolean(PdfTransparencyGroupAttributes.Keys.K, false); // ExtGState of mask form //<< // /AIS false // /BM /Normal // /ca 1 // /CA 1 // /op false // /OP false // /OPM 1 // /SA true // /SMask /None // /Type /ExtGState //>> PdfExtGState pdfStateMaskFrom = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>(); pdfStateMaskFrom.SetDefault1(); // Shading of mask form PdfShading shadingFrom = BuildShadingForSoftMask(brush); ////// 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]"); // Soft mask //<< // /G 21 0 R % form // /S /Luminosity // /Type /Mask //>> PdfSoftMask softmask = Context.PdfDocument.Internals.CreateIndirectObject <PdfSoftMask>(); // new PdfSoftMask(this.writer.Owner); //extGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask); //this.writer.Owner.Internals.AddObject(softmask); #if DEBUG if (DevHelper.RenderComments) { softmask.Elements.SetString("/@comment", "This is the soft mask"); } #endif softmask.Elements.SetName(PdfSoftMask.Keys.S, "/Luminosity"); softmask.Elements.SetReference(PdfSoftMask.Keys.G, form); // Create content of mask form //<< // /BBox [200.118 369.142 582.795 -141.094] // /Group 16 0 R // /Length 121 // /Matrix [1 0 0 1 0 0] // /Resources // << // /ExtGState // << // /GS0 20 0 R // >> // /Shading // << // /Sh0 19 0 R // >> // >> // /Subtype /Form //>> //stream // q // 200.118 369.142 382.677 -510.236 re // W n // q // 0 g // 1 i // GS0 gs // 0.75 0 0 -0.75 200.1181183 369.1417236 cm // BX /Sh0 sh EX Q // Q //endstream form.Elements.SetReference(PdfFormXObject.Keys.Group, tgAttributes); PdfContentWriter writer = new PdfContentWriter(Context, form); writer.BeginContentRaw(); // Acrobat 8 clips to bounding box, so we should do // why 0 480 360 -480 re ?? //writer.WriteClip(bbox); //writer.WriteGraphicsState(extGState); writer.WriteLiteral("1 i 0 g\n"); writer.WriteLiteral(writer.Resources.AddExtGState(pdfStateMaskFrom) + " gs\n"); XMatrix transform = new XMatrix(); //(brush.Viewport.Width / viewBoxForm.width, 0, 0, brush.Viewport.Height / viewBoxForm.height, 0, 0); writer.WriteMatrix(transform); writer.WriteLiteral("BX " + writer.Resources.AddShading(shadingFrom) + " sh EX\n"); writer.EndContent(); return(softmask); }
/// <summary> /// Writes a Path to the content stream. /// </summary> private void WritePath(Path path) { if (WriteSingleLineStrokeWithSpecialCaps(path)) { return; } 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) { WriteStrokeGeometry(path); } 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); 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"); } // 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> /// Builds a PdfFormXObject from the specified brush. /// // If a gradient contains transparency, a soft mask is created an added to the specified graphic state. /// </summary> PdfFormXObject BuildForm(RadialGradientBrush brush, PathGeometry geometry) { PdfFormXObject pdfForm = Context.PdfDocument.Internals.CreateIndirectObject <PdfFormXObject>(); // HACK pdfForm.Elements.SetRectangle(PdfFormXObject.Keys.BBox, new PdfRectangle(0, 640, 480, 0)); // Transparency group of the form //<< // /I true // /K false // /S /Transparency // /Type /Group //>> PdfTransparencyGroupAttributes tgPrimaryForm = Context.PdfDocument.Internals.CreateIndirectObject <PdfTransparencyGroupAttributes>(); // not set by Acrobat: tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceRGB"); tgPrimaryForm.Elements.SetBoolean(PdfTransparencyGroupAttributes.Keys.I, true); tgPrimaryForm.Elements.SetBoolean(PdfTransparencyGroupAttributes.Keys.K, false); pdfForm.Elements.SetReference(PdfFormXObject.Keys.Group, tgPrimaryForm); // Shading PdfShading shading = BuildShading(brush, 1, 1); // ExtGState //<< // /AIS false // /BM /Normal // /ca 1 // /CA 1 // /op false // /OP false // /OPM 1 // /SA true // /SMask 22 0 R // /Type /ExtGState //>> PdfExtGState pdfExtGState = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>(); pdfExtGState.SetDefault1(); // Soft mask PdfSoftMask softmask = BuildSoftMask(brush); pdfExtGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask); // PdfFormXObject //<< // /BBox [200.118 369.142 582.795 -141.094] // /Group 11 0 R // /Length 117 // /Matrix [1 0 0 1 0 0] // /Resources // << // /ColorSpace // << // /CS0 8 0 R // >> // /ExtGState // << // /GS0 23 0 R // >> // /Shading // << // /Sh0 14 0 R // >> // >> // /Subtype /Form //>> //stream //q //203.868 365.392 157.5 -97.5 re //W* n //q //0 g //1 i ///GS0 gs //0.75 0 0 -0.75 200.1181183 369.1417236 cm //BX /Sh0 sh EX Q //Q //endstream PdfContentWriter writer = new PdfContentWriter(Context, pdfForm); writer.BeginContentRaw(); // Acrobat 8 clips to bounding box, so we should do writer.WriteClip(geometry); //writer.WriteGraphicsState(extGState); //writer.WriteLiteral("0 g\n"); writer.WriteLiteral(writer.Resources.AddExtGState(pdfExtGState) + " gs\n"); XMatrix transform = new XMatrix(); //(brush.Viewport.Width / viewBoxForm.width, 0, 0, brush.Viewport.Height / viewBoxForm.height, 0, 0); writer.WriteMatrix(transform); writer.WriteLiteral("BX " + writer.Resources.AddShading(shading) + " sh EX\n"); writer.EndContent(); return(pdfForm); }
public virtual void LabSpotBasedGradient() { // step 1 Document document = new Document(PageSize.A3); // step 2 String dest_file = DEST_FOLDER + "/lab_spot_based_gradient.pdf"; PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(dest_file, FileMode.Create)); // step 3 document.Open(); // step 4 PdfContentByte canvas = writer.DirectContent; PdfLabColor pdfLabColor = new PdfLabColor(new float[] { 0.9505f, 1.0f, 1.0890f }, new float[] { 0f, 0.5f, 1.5f }, new float[] { -10, 100, -10, 100 }); PdfDeviceNColor pdfDeviceNNChannelColor = new PdfDeviceNColor(new PdfSpotColor[] { new PdfSpotColor("lab3", pdfLabColor.Rgb2lab(new BaseColor(0, 217, 83))), new PdfSpotColor("labBlue", pdfLabColor.Rgb2lab(new BaseColor(0, 0, 255))) }); PdfSpotColor psc_lab3 = new PdfSpotColor("lab3", pdfLabColor.Rgb2lab(new BaseColor(0, 217, 83))); PdfSpotColor psc_lab2 = new PdfSpotColor("lab2", pdfLabColor.Rgb2lab(new BaseColor(70, 138, 96))); PdfSpotColor psc_lab1 = new PdfSpotColor("lab1", pdfLabColor.Rgb2lab(new BaseColor(255, 0, 0))); PdfSpotColor psc_lab_blue = new PdfSpotColor("labBlue", new BaseColor(0, 0, 100)); PdfDeviceNColor pdfDeviceNNChannelColor2 = new PdfDeviceNColor(new PdfSpotColor[] { psc_lab_blue, psc_lab2, psc_lab1 }); ColorRectangle(canvas, pdfLabColor.Rgb2lab(new BaseColor(0, 0, 255)), 36, 878, 36, 36); ColorRectangle(canvas, pdfLabColor.Rgb2lab(new BaseColor(70, 138, 96)), 90, 878, 36, 36); ColorRectangle(canvas, pdfLabColor.Rgb2lab(new BaseColor(0, 217, 83)), 144, 878, 36, 36); ColorRectangle(canvas, pdfLabColor.Rgb2lab(new BaseColor(0, 50, 50)), 198, 878, 36, 36); ColorRectangle(canvas, pdfLabColor.Rgb2lab(new BaseColor(0, 0, 255)).ToRgb(), 36, 824, 36, 36); ColorRectangle(canvas, pdfLabColor.Rgb2lab(new BaseColor(70, 138, 96)).ToRgb(), 90, 824, 36, 36); ColorRectangle(canvas, pdfLabColor.Rgb2lab(new BaseColor(0, 217, 83)).ToRgb(), 144, 824, 36, 36); ColorRectangle(canvas, pdfLabColor.Rgb2lab(new BaseColor(0, 50, 50)).ToRgb(), 198, 824, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab1", pdfLabColor.Rgb2lab(new BaseColor(255, 0, 0))), 1f), 36, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab1", pdfLabColor.Rgb2lab(new BaseColor(255, 0, 0))), 0.8f), 90, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab1", pdfLabColor.Rgb2lab(new BaseColor(255, 0, 0))), 0.6f), 144, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab1", pdfLabColor.Rgb2lab(new BaseColor(255, 0, 0))), 0.2f), 198, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab2", pdfLabColor.Rgb2lab(new BaseColor(70, 138, 96))), 1f), 252, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab2", pdfLabColor.Rgb2lab(new BaseColor(70, 138, 96))), 0.8f), 306, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab2", pdfLabColor.Rgb2lab(new BaseColor(70, 138, 96))), 0.6f), 360, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab2", pdfLabColor.Rgb2lab(new BaseColor(70, 138, 96))), 0.2f), 416, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab3", pdfLabColor.Rgb2lab(new BaseColor(0, 217, 83))), 1.0f), 470, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab3", pdfLabColor.Rgb2lab(new BaseColor(0, 217, 83))), 0.8f), 524, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab3", pdfLabColor.Rgb2lab(new BaseColor(0, 217, 83))), 0.6f), 578, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("lab3", pdfLabColor.Rgb2lab(new BaseColor(0, 217, 83))), 0.2f), 634, 986, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb1", new BaseColor(255, 0, 0)), 1f), 36, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb1", new BaseColor(255, 0, 0)), 0.8f), 90, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb1", new BaseColor(255, 0, 0)), 0.6f), 144, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb1", new BaseColor(255, 0, 0)), 0.2f), 198, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb2", new BaseColor(70, 138, 96)), 1f), 252, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb2", new BaseColor(70, 138, 96)), 0.8f), 306, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb2", new BaseColor(70, 138, 96)), 0.6f), 360, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb2", new BaseColor(70, 138, 96)), 0.2f), 416, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb3", new BaseColor(0, 217, 83)), 1.0f), 470, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb3", new BaseColor(0, 217, 83)), 0.8f), 524, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb3", new BaseColor(0, 217, 83)), 0.6f), 578, 932, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("rgb3", new BaseColor(0, 217, 83)), 0.2f), 634, 932, 36, 36); ColorRectangle(canvas, new DeviceNColor(new PdfDeviceNColor(new PdfSpotColor[] { psc_lab2, psc_lab_blue, psc_lab1 }), new float[] { 0, 0.0f, 1 }), 36, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.1f, 0.1f }), 90, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.2f, 0.2f }), 144, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.3f, 0.3f }), 198, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.4f, 0.4f }), 252, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(new PdfDeviceNColor(new PdfSpotColor[] { psc_lab2, psc_lab_blue, psc_lab1, psc_lab3 }), new float[] { 0.5f, 0.5f, 1, 0.5f }), 306, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor2, new float[] { 0.6f, 0.1f, 0.5f }), 360, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.7f, 0.7f }), 416, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.8f, 0.8f }), 470, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(new PdfDeviceNColor(new PdfSpotColor[] { psc_lab2, psc_lab_blue, psc_lab1 }), new float[] { 0.9f, 0.9f, 1 }), 524, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 1, 1 }), 578, 770, 36, 36); canvas.SaveState(); canvas.Rectangle(418, 412, -329, 189); canvas.Clip(); canvas.NewPath(); canvas.SaveState(); canvas.ConcatCTM(329f, 0f, 0f, -329f, 89f, 506.5f); canvas.PaintShading(PdfShading.SimpleAxial(writer, 0, 0, 1, 0, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 1, 0 }), new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0, 1 }))); canvas.RestoreState(); canvas.RestoreState(); canvas.SetColorStroke(new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 1, 1 })); canvas.Rectangle(418, 412, -329, 189); canvas.Stroke(); // step 5 document.Close(); CompareTool compareTool = new CompareTool(); String error = compareTool.Compare(dest_file, TEST_RESOURCES_PATH + "cmp_lab_spot_based_gradient.pdf", DEST_FOLDER, "diff"); if (error != null) { Assert.Fail(error); } }
void RealizeLinearGradientBrush(LinearGradientBrush brush, XForm xform) { XMatrix matrix = currentTransform; PdfShadingPattern pattern = new PdfShadingPattern(writer.Owner); pattern.Elements[PdfShadingPattern.Keys.PatternType] = new PdfInteger(2); // shading pattern // Setup shading PdfShading shading = new PdfShading(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 = writer.Resources.AddPattern(pattern); writer.WriteLiteral("/Pattern cs\n", name); writer.WriteLiteral("{0} scn\n", name); double alpha = brush.Opacity * brush.GradientStops.GetAverageAlpha(); if (alpha < 1 && writer.renderMode == RenderMode.Default) { #if true PdfExtGState extGState = writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha); string gs = writer.Resources.AddExtGState(extGState); 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 } }
/// <summary> /// Adds /// <see cref="iText.Kernel.Pdf.Colorspace.PdfShading"/> /// object to the resources. /// </summary> /// <param name="shading"> /// the /// <see cref="iText.Kernel.Pdf.Colorspace.PdfShading"/> /// to add. /// </param> /// <returns>added shading resource name.</returns> public virtual PdfName AddShading(PdfShading shading) { return(AddResource(shading, shadingNamesGen)); }
public virtual void DeviceNSpotBasedGradient() { // step 1 Document document = new Document(PageSize.A3); // step 2 String dest_file = DEST_FOLDER + "/device_n_gradient_base.pdf"; PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(dest_file, FileMode.Create)); // step 3 document.Open(); // step 4 PdfContentByte canvas = writer.DirectContent; PdfSpotColor psc_gray = new PdfSpotColor("iTextGray", new GrayColor(0f)); PdfSpotColor psc_cmyk_yell = new PdfSpotColor("iTextYellow", new CMYKColor(0f, 0f, 1f, 0f)); PdfSpotColor psc_cmyk_magen = new PdfSpotColor("iTextMagenta", new CMYKColor(0f, 1f, 0f, 0f)); PdfSpotColor psc_rgb_blue = new PdfSpotColor("iTextBlue", new BaseColor(0, 0, 255)); PdfDeviceNColor pdfDeviceNNChannelColor = new PdfDeviceNColor(new PdfSpotColor[] { psc_cmyk_yell, psc_cmyk_magen, psc_rgb_blue }); PdfDeviceNColor pdfDeviceNNChannelColor2 = new PdfDeviceNColor(new PdfSpotColor[] { psc_cmyk_magen, psc_cmyk_yell, psc_rgb_blue }); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("iTextGray", new GrayColor(0f)), 0.8f), 36, 824, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("iTextYellow", new CMYKColor(0f, 0f, 1f, 0f)), 0.8f), 90, 824, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("iTextMagenta", new CMYKColor(0f, 1f, 0f, 0f)), 0.4f), 144, 824, 36, 36); ColorRectangle(canvas, new SpotColor(new PdfSpotColor("iTextBlue", new BaseColor(0, 0, 255)), 0.7f), 198, 824, 36, 36); ColorRectangle(canvas, new DeviceNColor(new PdfDeviceNColor(new PdfSpotColor[] { psc_cmyk_yell, psc_cmyk_magen, psc_rgb_blue }), new float[] { 0, 0.0f, 1 }), 36, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.1f, 0.1f, 1 }), 90, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.2f, 0.2f, 1 }), 144, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.3f, 0.3f, 1 }), 198, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.4f, 0.4f, 1 }), 252, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(new PdfDeviceNColor(new PdfSpotColor[] { psc_cmyk_yell, psc_cmyk_magen, psc_rgb_blue, psc_gray }), new float[] { 0.5f, 0.5f, 1, 0.5f }), 306, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor2, new float[] { 0.6f, 0.1f, 1 }), 360, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.7f, 0.7f, 1 }), 416, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0.8f, 0.8f, 1 }), 470, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(new PdfDeviceNColor(new PdfSpotColor[] { psc_cmyk_yell, psc_cmyk_magen, psc_rgb_blue }), new float[] { 0.9f, 0.9f, 1 }), 524, 770, 36, 36); ColorRectangle(canvas, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 1, 1, 1 }), 578, 770, 36, 36); PdfDeviceNColor pdfDeviceNColor = new PdfDeviceNColor(new PdfSpotColor[] { psc_cmyk_yell, psc_cmyk_magen, psc_rgb_blue }); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 0, 0, 1 })); canvas.Rectangle(36, 716, 36, 36); canvas.FillStroke(); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 0.1f, 0.1f, 1 })); canvas.Rectangle(90, 716, 36, 36); canvas.FillStroke(); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 0.2f, 0.2f, 1 })); canvas.Rectangle(144, 716, 36, 36); canvas.FillStroke(); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 0.3f, 0.3f, 1 })); canvas.Rectangle(198, 716, 36, 36); canvas.FillStroke(); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 0.4f, 0.4f, 1 })); canvas.Rectangle(252, 716, 36, 36); canvas.FillStroke(); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 0.5f, 0.5f, 1 })); canvas.Rectangle(306, 716, 36, 36); canvas.FillStroke(); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 0.6f, 0.1f, 1 })); canvas.Rectangle(360, 716, 36, 36); canvas.FillStroke(); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 0.7f, 0.7f, 1 })); canvas.Rectangle(416, 716, 36, 36); canvas.FillStroke(); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 0.8f, 0.8f, 1 })); canvas.Rectangle(470, 716, 36, 36); canvas.FillStroke(); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 0.9f, 0.9f, 1 })); canvas.Rectangle(524, 716, 36, 36); canvas.FillStroke(); canvas.SetColorFill(new DeviceNColor(pdfDeviceNColor, new float[] { 1, 1, 1 })); canvas.Rectangle(578, 716, 36, 36); canvas.FillStroke(); canvas.SaveState(); canvas.Rectangle(418, 412, -329, 189); canvas.Clip(); canvas.NewPath(); canvas.SaveState(); canvas.ConcatCTM(329f, 0f, 0f, -329f, 89f, 506.5f); canvas.PaintShading(PdfShading.SimpleAxial(writer, 0, 0, 1, 0, new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 1, 1, 0 }), new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 0, 0, 1 }))); canvas.RestoreState(); canvas.RestoreState(); canvas.SetColorStroke(new DeviceNColor(pdfDeviceNNChannelColor, new float[] { 1, 1, 1 })); canvas.Rectangle(418, 412, -329, 189); canvas.Stroke(); // step 5 document.Close(); CompareTool compareTool = new CompareTool(); String error = compareTool.Compare(dest_file, TEST_RESOURCES_PATH + "cmp_device_n_gradient_base.pdf", DEST_FOLDER, "diff_"); if (error != null) { Assert.Fail(error); } }
void RealizeRadialGradientBrush(RadialGradientBrush brush, XForm xform) { XMatrix matrix = new XMatrix(); // this.renderer.defaultViewMatrix; //matrix.MultiplyPrepend(this.Transform); matrix = currentTransform; //matrix.MultiplyPrepend(XMatrix.CreateScaling(1.3, 1)); PdfShadingPattern pattern = new PdfShadingPattern(writer.Owner); pattern.Elements[PdfShadingPattern.Keys.PatternType] = new PdfInteger(2); // shading pattern // Setup shading PdfShading shading = new PdfShading(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 = writer.Resources.AddPattern(pattern); writer.WriteLiteral("/Pattern cs\n", name); writer.WriteLiteral("{0} scn\n", name); double alpha = brush.Opacity * brush.GradientStops.GetAverageAlpha(); if (alpha < 1) { #if true PdfExtGState extGState = writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha); string gs = writer.Resources.AddExtGState(extGState); 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 } }
public virtual PdfShading GetShading(PdfName name) { PdfObject shading = GetResourceObject(PdfName.Shading, name); return(shading is PdfDictionary?PdfShading.MakeShading((PdfDictionary)shading) : null); }
/** Creates new PdfShadingPattern */ public PdfShadingPattern(PdfShading shading) { writer = shading.Writer; Put(PdfName.PATTERNTYPE, new PdfNumber(2)); this.shading = shading; }