private FormXObject CreateWatermark(Document document) { SKSize size = document.GetSize(); // 1. Create an external form object to represent the watermark! FormXObject watermark = new FormXObject(document, size); // 2. Inserting the contents of the watermark... // 2.1. Create a content composer! PrimitiveComposer composer = new PrimitiveComposer(watermark); // 2.2. Inserting the contents... // Set the font to use! composer.SetFont(new fonts::StandardType1Font(document, fonts::StandardType1Font.FamilyEnum.Times, true, false), 120); // Set the color to fill the text characters! composer.SetFillColor(new DeviceRGBColor(115 / 255d, 164 / 255d, 232 / 255d)); // Apply transparency! { ExtGState state = new ExtGState(document); state.FillAlpha = .3; composer.ApplyState(state); } // Show the text! composer.ShowText( "PdfClown", // Text to show. new SKPoint(size.Width / 2f, size.Height / 2f), // Anchor location: page center. XAlignmentEnum.Center, // Horizontal placement (relative to the anchor): center. YAlignmentEnum.Middle, // Vertical placement (relative to the anchor): middle. 50 // Rotation: 50-degree-counterclockwise. ); // 2.3. Flush the contents into the watermark! composer.Flush(); return(watermark); }
private void PrintMarcaDAgua(String text) { PointF p = PointF.Empty; var blocoProdutos = BlocosSuperiores.FirstOrDefault(x => x is BlocoProdutos); if (blocoProdutos == null) { p = new PointF(_Danfe.Size.Width / 2f, _Danfe.Size.Height / 2f); } else { p.X = blocoProdutos.Posicao.X + blocoProdutos.Size.Width / 2F; p.Y = blocoProdutos.Posicao.Y + blocoProdutos.Size.Height / 2F; } _Composer.BeginLocalState(); _Composer.SetFont(_Danfe.FontBold, 50); org.pdfclown.documents.contents.ExtGState state = new org.pdfclown.documents.contents.ExtGState(_Danfe.Document); state.FillAlpha = 0.3F; _Composer.ApplyState(state); _Composer.ShowText(text, p, XAlignmentEnum.Center, YAlignmentEnum.Middle, 0); _Composer.End(); }
private FormXObject CreateWatermark( Document document ) { SizeF size = document.GetSize(); // 1. Create an external form object to represent the watermark! FormXObject watermark = new FormXObject(document, size); // 2. Inserting the contents of the watermark... // 2.1. Create a content composer! PrimitiveComposer composer = new PrimitiveComposer(watermark); // 2.2. Inserting the contents... // Set the font to use! composer.SetFont(new fonts::StandardType1Font(document, fonts::StandardType1Font.FamilyEnum.Times, true, false), 120); // Set the color to fill the text characters! composer.SetFillColor(new DeviceRGBColor(115 / 255d, 164 / 255d, 232 / 255d)); // Apply transparency! { ExtGState state = new ExtGState(document); state.FillAlpha = .3; composer.ApplyState(state); } // Show the text! composer.ShowText( "PDFClown", // Text to show. new PointF(size.Width / 2f, size.Height / 2f), // Anchor location: page center. XAlignmentEnum.Center, // Horizontal placement (relative to the anchor): center. YAlignmentEnum.Middle, // Vertical placement (relative to the anchor): middle. 50 // Rotation: 50-degree-counterclockwise. ); // 2.3. Flush the contents into the watermark! composer.Flush(); return watermark; }
/* * TODO: refresh should happen just before serialization, on document event (e.g. OnWrite()) */ private void RefreshAppearance( ) { FormXObject normalAppearance; RectangleF box = org.pdfclown.objects.Rectangle.Wrap(BaseDataObject[PdfName.Rect]).ToRectangleF(); { AppearanceStates normalAppearances = Appearance.Normal; normalAppearance = normalAppearances[null]; if (normalAppearance != null) { normalAppearance.Box = box; normalAppearance.BaseDataObject.Body.SetLength(0); } else { normalAppearances[null] = normalAppearance = new FormXObject(Document, box); } } PrimitiveComposer composer = new PrimitiveComposer(normalAppearance); { float yOffset = box.Height - Page.Box.Height; MarkupTypeEnum markupType = MarkupType; switch (markupType) { case MarkupTypeEnum.Highlight: { ExtGState defaultExtGState; { ExtGStateResources extGStates = normalAppearance.Resources.ExtGStates; defaultExtGState = extGStates[HighlightExtGStateName]; if (defaultExtGState == null) { if (extGStates.Count > 0) { extGStates.Clear(); } extGStates[HighlightExtGStateName] = defaultExtGState = new ExtGState(Document); defaultExtGState.AlphaShape = false; defaultExtGState.BlendMode = new List <BlendModeEnum>(new BlendModeEnum[] { BlendModeEnum.Multiply }); } } composer.ApplyState(defaultExtGState); composer.SetFillColor(Color); { foreach (Quad markupBox in MarkupBoxes) { PointF[] points = markupBox.Points; float markupBoxHeight = points[3].Y - points[0].Y; float markupBoxMargin = GetMarkupBoxMargin(markupBoxHeight); composer.DrawCurve( new PointF(points[3].X, points[3].Y + yOffset), new PointF(points[0].X, points[0].Y + yOffset), new PointF(points[3].X - markupBoxMargin, points[3].Y - markupBoxMargin + yOffset), new PointF(points[0].X - markupBoxMargin, points[0].Y + markupBoxMargin + yOffset) ); composer.DrawLine( new PointF(points[1].X, points[1].Y + yOffset) ); composer.DrawCurve( new PointF(points[2].X, points[2].Y + yOffset), new PointF(points[1].X + markupBoxMargin, points[1].Y + markupBoxMargin + yOffset), new PointF(points[2].X + markupBoxMargin, points[2].Y - markupBoxMargin + yOffset) ); composer.Fill(); } } } break; case MarkupTypeEnum.Squiggly: { composer.SetStrokeColor(Color); composer.SetLineCap(LineCapEnum.Round); composer.SetLineJoin(LineJoinEnum.Round); { foreach (Quad markupBox in MarkupBoxes) { PointF[] points = markupBox.Points; float markupBoxHeight = points[3].Y - points[0].Y; float lineWidth = markupBoxHeight * .05f; float step = markupBoxHeight * .125f; float boxXOffset = points[3].X; float boxYOffset = points[3].Y + yOffset - lineWidth; bool phase = false; composer.SetLineWidth(lineWidth); for (float x = 0, xEnd = points[2].X - boxXOffset; x < xEnd || !phase; x += step) { PointF point = new PointF(x + boxXOffset, (phase ? -step : 0) + boxYOffset); if (x == 0) { composer.StartPath(point); } else { composer.DrawLine(point); } phase = !phase; } } composer.Stroke(); } } break; case MarkupTypeEnum.StrikeOut: case MarkupTypeEnum.Underline: { composer.SetStrokeColor(Color); { float lineYRatio = 0; switch (markupType) { case MarkupTypeEnum.StrikeOut: lineYRatio = .5f; break; case MarkupTypeEnum.Underline: lineYRatio = .9f; break; default: throw new NotImplementedException(); } foreach (Quad markupBox in MarkupBoxes) { PointF[] points = markupBox.Points; float markupBoxHeight = points[3].Y - points[0].Y; float boxYOffset = markupBoxHeight * lineYRatio + yOffset; composer.SetLineWidth(markupBoxHeight * .065); composer.DrawLine( new PointF(points[3].X, points[0].Y + boxYOffset), new PointF(points[2].X, points[1].Y + boxYOffset) ); } composer.Stroke(); } } break; default: throw new NotImplementedException(); } } composer.Flush(); }
/* TODO: refresh should happen just before serialization, on document event (e.g. OnWrite()) */ private void RefreshAppearance( ) { FormXObject normalAppearance; RectangleF box = org.pdfclown.objects.Rectangle.Wrap(BaseDataObject[PdfName.Rect]).ToRectangleF(); { AppearanceStates normalAppearances = Appearance.Normal; normalAppearance = normalAppearances[null]; if(normalAppearance != null) { normalAppearance.Box = box; normalAppearance.BaseDataObject.Body.SetLength(0); } else {normalAppearances[null] = normalAppearance = new FormXObject(Document, box);} } PrimitiveComposer composer = new PrimitiveComposer(normalAppearance); { float yOffset = box.Height - Page.Box.Height; MarkupTypeEnum markupType = MarkupType; switch(markupType) { case MarkupTypeEnum.Highlight: { ExtGState defaultExtGState; { ExtGStateResources extGStates = normalAppearance.Resources.ExtGStates; defaultExtGState = extGStates[HighlightExtGStateName]; if(defaultExtGState == null) { if(extGStates.Count > 0) {extGStates.Clear();} extGStates[HighlightExtGStateName] = defaultExtGState = new ExtGState(Document); defaultExtGState.AlphaShape = false; defaultExtGState.BlendMode = new List<BlendModeEnum>(new BlendModeEnum[]{BlendModeEnum.Multiply}); } } composer.ApplyState(defaultExtGState); composer.SetFillColor(Color); { foreach(Quad markupBox in MarkupBoxes) { PointF[] points = markupBox.Points; float markupBoxHeight = points[3].Y - points[0].Y; float markupBoxMargin = GetMarkupBoxMargin(markupBoxHeight); composer.DrawCurve( new PointF(points[3].X, points[3].Y + yOffset), new PointF(points[0].X, points[0].Y + yOffset), new PointF(points[3].X - markupBoxMargin, points[3].Y - markupBoxMargin + yOffset), new PointF(points[0].X - markupBoxMargin, points[0].Y + markupBoxMargin + yOffset) ); composer.DrawLine( new PointF(points[1].X, points[1].Y + yOffset) ); composer.DrawCurve( new PointF(points[2].X, points[2].Y + yOffset), new PointF(points[1].X + markupBoxMargin, points[1].Y + markupBoxMargin + yOffset), new PointF(points[2].X + markupBoxMargin, points[2].Y - markupBoxMargin + yOffset) ); composer.Fill(); } } } break; case MarkupTypeEnum.Squiggly: { composer.SetStrokeColor(Color); composer.SetLineCap(LineCapEnum.Round); composer.SetLineJoin(LineJoinEnum.Round); { foreach(Quad markupBox in MarkupBoxes) { PointF[] points = markupBox.Points; float markupBoxHeight = points[3].Y - points[0].Y; float lineWidth = markupBoxHeight * .02f; float step = markupBoxHeight * .125f; float boxXOffset = points[3].X; float boxYOffset = points[3].Y + yOffset - lineWidth; bool phase = false; composer.SetLineWidth(lineWidth); for(float x = 0, xEnd = points[2].X - boxXOffset; x < xEnd || !phase; x += step) { PointF point = new PointF(x + boxXOffset, (phase ? -step : 0) + boxYOffset); if(x == 0) {composer.StartPath(point);} else {composer.DrawLine(point);} phase = !phase; } } composer.Stroke(); } } break; case MarkupTypeEnum.StrikeOut: case MarkupTypeEnum.Underline: { composer.SetStrokeColor(Color); { float lineYRatio = 0; switch(markupType) { case MarkupTypeEnum.StrikeOut: lineYRatio = .575f; break; case MarkupTypeEnum.Underline: lineYRatio = .85f; break; default: throw new NotImplementedException(); } foreach(Quad markupBox in MarkupBoxes) { PointF[] points = markupBox.Points; float markupBoxHeight = points[3].Y - points[0].Y; float boxYOffset = markupBoxHeight * lineYRatio + yOffset; composer.SetLineWidth(markupBoxHeight * .065); composer.DrawLine( new PointF(points[3].X, points[0].Y + boxYOffset), new PointF(points[2].X, points[1].Y + boxYOffset) ); } composer.Stroke(); } } break; default: throw new NotImplementedException(); } } composer.Flush(); }