StrikeoutParagraph.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.Linq;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Pdf.TextMap;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Pdf.Annotations;
  13. using System.Threading;
  14. using System.Collections.Generic;
  15. using GrapeCity.Documents.Common;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // This example shows how to strikeout a paragraph starting with a certain phrase
  20. // ("Several species") using the text markup strikeout annotation.
  21. public class StrikeoutParagraph
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. // Load the PDF:
  26. var doc = new GcPdfDocument();
  27. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf"));
  28. doc.Load(fs);
  29.  
  30. // Strikeout the first paragraph that starts with the words "Several species":
  31. ITextParagraph para = null;
  32. foreach (var p in doc.Pages)
  33. {
  34. var paras = p.GetTextMap().Paragraphs;
  35. para = paras.FirstOrDefault(p_ => p_.GetText().StartsWith("Several species"));
  36. if (para != null)
  37. break;
  38. }
  39. if (para == null)
  40. return doc.Pages.Count; // Should not happen.
  41.  
  42. // Text markup strikeout annotation:
  43. var markup = new TextMarkupAnnotation()
  44. {
  45. Page = para.Page,
  46. MarkupType = TextMarkupType.StrikeOut,
  47. Color = Color.Red
  48. };
  49.  
  50. // Get the coordinates of all text run fragments in the paragraph,
  51. // and add them to the annotation's area:
  52. List<Quadrilateral> area = new List<Quadrilateral>();
  53. foreach (var run in para.Runs)
  54. {
  55. foreach (var frag in run)
  56. {
  57. // Note: in v6.0.3 a method ITextRunFragment.GetBounds() was added,
  58. // so this code can be replaced with a single 'frag.GetBounds()' call.
  59. // Find the text run fragment's index in the containing line:
  60. int index = 0;
  61. var line = frag.Line;
  62. var frags = line.RunFragments;
  63. for (int i = 0; i < frags.Count && frags[i] != frag; ++i)
  64. index += frags[i].Count;
  65. // Add the fragment's bounds (quadrilateral) to the annotation area:
  66. area.Add(frag.Line.GetCoords(index, frag.Count));
  67. }
  68. }
  69. markup.Area = area;
  70.  
  71. // Done:
  72. doc.Save(stream);
  73. return doc.Pages.Count;
  74. }
  75. }
  76. }
  77.