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