ChangeTextAttrs.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 GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Pdf.TextMap;
  10. using GrapeCity.Documents.Pdf.Layers;
  11. using GrapeCity.Documents.Text;
  12.  
  13. namespace DsPdfWeb.Demos
  14. {
  15. // Finds all occurrences of a regular expression (specifically,
  16. // the regex searches for strings that look like dates or times)
  17. // and changes the text rendering attributes of all those occurrences.
  18. // The following attributes can be changed:
  19. // - Text rendering mode (fill/stroke/clip);
  20. // - Text fill color and alpha;
  21. // - Text stroke color and alpha.
  22. // For reference, the original PDF without changes is appended to the result.
  23. // The original PDF and the search regex are the same as used in DeleteText.
  24. public class ChangeTextAttrs
  25. {
  26. public int CreatePDF(Stream stream)
  27. {
  28. var doc = new GcPdfDocument();
  29. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "TimeSheet.pdf"));
  30. doc.Load(fs);
  31. var pageCount = doc.Pages.Count;
  32. // Duplicate the PDF:
  33. doc.MergeWithDocument(doc);
  34. // Modify text on the first copy:
  35. for (int i = 0; i < pageCount; ++i)
  36. {
  37. var ftps = new FindTextParams(@"\d+/\d+/\d+|\d+:\d+| am| pm", false, false, 72, 72, false, true);
  38. // TextRenderingAttrs allows you to specify the following text rendering attributes:
  39. // - RenderingMode
  40. // - FillAlpha
  41. // - FillColor
  42. // - StrokeAlpha
  43. // - StrokePen
  44. doc.Pages[i].SetTextRenderingAttrs(ftps, new TextRenderingAttrs() {
  45. RenderingMode = TextRenderingMode.FillStroke,
  46. FillColor = Color.Red,
  47. FillAlpha = 0.5f,
  48. StrokeAlpha = 1.0f,
  49. StrokePen = new GrapeCity.Documents.Drawing.Pen(Color.Blue, 1)});
  50. }
  51. // Done:
  52. doc.Save(stream);
  53. return doc.Pages.Count;
  54. }
  55. }
  56. }
  57.