ListFonts.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.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // This sample lists all fonts found in a loaded PDF,
  16. // prints some info for each font, and indicates whether a Font object
  17. // can be created from the font in the PDF.
  18. public class ListFonts
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. const string sourcePDF = "CompleteJavaScriptBook.pdf";
  23.  
  24. var doc = new GcPdfDocument();
  25. var page = doc.NewPage();
  26. var rc = Common.Util.AddNote(
  27. "This sample loads an arbitrary PDF into a temporary GcPdfDocument, " +
  28. "and lists all fonts found in that document, with some of their properties. " +
  29. "It also tries to create a Font object from each of those PDF fonts, " +
  30. "and reports whether this operation succeeded.",
  31. page);
  32.  
  33. // Text layout to render the text:
  34. var tab = 24;
  35. var tl = page.Graphics.CreateTextLayout();
  36. tl.DefaultFormat.Font = StandardFonts.Times;
  37. tl.DefaultFormat.FontSize = 12;
  38. tl.MaxWidth = doc.PageSize.Width;
  39. tl.MaxHeight = doc.PageSize.Height;
  40. tl.MarginAll = rc.Left;
  41. tl.MarginTop = rc.Bottom + 36;
  42. tl.TabStops = [ new (tab) ];
  43. tl.FirstLineIndent = -tab;
  44. tl.MarginRight = 144;
  45.  
  46. // Text split options for widow/orphan control:
  47. var to = new TextSplitOptions(tl)
  48. {
  49. KeepParagraphLinesTogether = true,
  50. MinLinesInFirstParagraph = 2,
  51. MinLinesInLastParagraph = 2,
  52. RestMarginTop = rc.Left,
  53. };
  54.  
  55. // Open an arbitrary PDF, load it into a temp document and get all fonts:
  56. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", sourcePDF));
  57. var doc1 = new GcPdfDocument();
  58. doc1.Load(fs);
  59.  
  60. var fonts = doc1.GetFonts();
  61. tl.AppendLine($"Total of {fonts.Count} fonts found in {sourcePDF}:");
  62. tl.AppendLine();
  63. int i = 0;
  64. foreach (var font in fonts)
  65. {
  66. var nativeFont = font.CreateNativeFont();
  67. tl.Append($"{i}:\tBaseFont: {font.BaseFont}; IsEmbedded: {font.IsEmbedded}.");
  68. tl.AppendParagraphBreak();
  69. if (nativeFont != null)
  70. tl.AppendLine($"\tCreateNativeFont succeeded, family: {nativeFont.FontFamilyName}; bold: {nativeFont.FontBold}; italic: {nativeFont.FontItalic}.");
  71. else
  72. tl.AppendLine("\tCreateNativeFont failed");
  73. tl.AppendLine();
  74. ++i;
  75. }
  76. tl.PerformLayout(true);
  77. while (true)
  78. {
  79. // 'rest' will accept the text that did not fit:
  80. var splitResult = tl.Split(to, out TextLayout rest);
  81. doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);
  82. if (splitResult != SplitResult.Split)
  83. break;
  84. tl = rest;
  85. doc.NewPage();
  86. }
  87. // Done:
  88. doc.Save(stream);
  89. return doc.Pages.Count;
  90. }
  91. }
  92. }
  93.