TextToOutlines.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.AcroForms;
  10. using GrapeCity.Documents.Pdf.Graphics;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // This sample shows how to convert all text in an existing PDF to glyph outlines.
  17. // The resulting PDF will look exactly like the original, but all glyphs in it
  18. // will be rendered as graphics paths. This can be used to manipulate the paths,
  19. // or to make sure it will be impossible to copy or search the text.
  20. // Note that the resulting documents will have no fonts (see for example
  21. // the Document Properties | Fonts tab in DsPdfViewer).
  22. // The original PDF used by this sample was generated by Wetlands.
  23. public class TextToOutlines
  24. {
  25. public int CreatePDF(Stream stream)
  26. {
  27. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf"));
  28. // Load the source PDF into a temp document:
  29. var srcDoc = new GcPdfDocument();
  30. srcDoc.Load(fs);
  31. // Draw all pages of the source document on pages of the new PDF:
  32. var doc = new GcPdfDocument();
  33. foreach (var srcPage in srcDoc.Pages)
  34. {
  35. var page = doc.Pages.Add(srcPage.Size);
  36. // Setting Graphics.DrawTextAsPath to true makes all glyphs draw as graphics paths
  37. // instead of rendering text using fonts:
  38. page.Graphics.DrawTextAsPath = true;
  39. // Draw the source page on the target:
  40. srcPage.Draw(page.Graphics, srcPage.Bounds);
  41. }
  42. // Done:
  43. doc.Save(stream);
  44. return doc.Pages.Count;
  45. }
  46. }
  47. }
  48.