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