TransparencyLayer.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Numerics;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GCTEXT = GrapeCity.Documents.Text;
  12. using GCDRAW = GrapeCity.Documents.Drawing;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // This example shows how to use GcGraphics.PushTransparencyLayer()/PopTransparencyLayer() methods
  17. // to specify a common opacity for a group of drawing operations.
  18. // The code is similar to AddWatermark sample, but instead of using a semi-transparent
  19. // text color, it performs all related drawing on a transparency layer.
  20. // This provides a more flexible and convenient approach to control transparency.
  21. public class TransparencyLayer
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. var doc = new GcPdfDocument();
  26. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "SlidePages.pdf"));
  27. doc.Load(fs);
  28. foreach (var page in doc.Pages)
  29. {
  30. var g = page.Graphics;
  31.  
  32. // Text layout used to draw the 'watermark':
  33. var tl = g.CreateTextLayout();
  34. tl.Append("DsPdf Demo");
  35. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibrib.ttf"));
  36. tl.DefaultFormat.FontSize = g.Resolution;
  37. tl.DefaultFormat.ForeColor = Color.Blue;
  38. tl.DefaultFormat.GlyphAdvanceFactor = 1.5f;
  39. tl.PerformLayout();
  40.  
  41. // Rotation angle (radians) - from left/bottom to right/top corners of the page:
  42. var angle = -Math.Asin(g.CanvasSize.Width / g.CanvasSize.Height);
  43. // Page center:
  44. var center = new PointF(g.CanvasSize.Width / 2, g.CanvasSize.Height / 2);
  45. // Additional offset from text size:
  46. var delta = new PointF(
  47. (float)((tl.ContentWidth * Math.Cos(angle) - tl.ContentHeight * Math.Sin(angle)) / 2),
  48. (float)((tl.ContentWidth * Math.Sin(angle) + tl.ContentHeight * Math.Cos(angle)) / 2));
  49.  
  50. // Create semi-opaque transparency layer. All subsequent drawing operations
  51. // will be drawn using the specified opacity till the PopTransparencyLayer() call:
  52. g.PushTransparencyLayer(null, 0.5f);
  53.  
  54. // Draw watermark text diagonally in the center of the page
  55. // (matrix transforms are applied from last to first):
  56. g.Transform =
  57. Matrix3x2.CreateRotation((float)angle) *
  58. Matrix3x2.CreateTranslation(center.X - delta.X, center.Y - delta.Y);
  59. g.DrawTextLayout(tl, PointF.Empty);
  60. // Add a border around the text, it will also be drawn semi-transparently:
  61. var rc = new RectangleF(-30, -20, tl.ContentWidth + 60, tl.ContentHeight + 40);
  62. g.DrawRoundRect(rc, 30, new GCDRAW.Pen(Color.DarkOrange, 8));
  63. g.Transform = Matrix3x2.Identity;
  64.  
  65. // Pop transparency layer:
  66. g.PopTransparencyLayer();
  67. }
  68. doc.Save(stream);
  69. return doc.Pages.Count;
  70. }
  71. }
  72. }
  73.