ListFonts.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Collections.Generic
  8. Imports System.Linq
  9. Imports GrapeCity.Documents.Pdf
  10. Imports GrapeCity.Documents.Text
  11.  
  12. '' This sample lists all fonts found in a loaded PDF,
  13. '' prints some info for each font, And indicates whether a Font object
  14. '' can be created from the font in the PDF.
  15. Public Class ListFonts
  16. Function CreatePDF(ByVal stream As Stream) As Integer
  17. Dim sourcePDF = "CompleteJavaScriptBook.pdf"
  18.  
  19. Dim doc = New GcPdfDocument()
  20. Dim page = doc.NewPage()
  21.  
  22. Dim rc = Util.AddNote(
  23. "This sample loads an arbitrary PDF into a temporary GcPdfDocument, " +
  24. "and lists all fonts found in that document, with some of their properties. " +
  25. "It also tries to create a Font object from each of those PDF fonts, " +
  26. "and reports whether this operation succeeded.",
  27. page)
  28.  
  29. '' Text layout to render the text:
  30. Dim tab = 24
  31. Dim tl = page.Graphics.CreateTextLayout()
  32. tl.DefaultFormat.Font = StandardFonts.Times
  33. tl.DefaultFormat.FontSize = 12
  34. tl.MaxWidth = doc.PageSize.Width
  35. tl.MaxHeight = doc.PageSize.Height
  36. tl.MarginAll = rc.Left
  37. tl.MarginTop = rc.Bottom + 36
  38. tl.TabStops = New List(Of TabStop)() From {New TabStop(tab)}
  39. tl.FirstLineIndent = -tab
  40. tl.MarginRight = 144
  41.  
  42. '' Text split options for widow/orphan control:
  43. Dim tso = New TextSplitOptions(tl) With
  44. {
  45. .KeepParagraphLinesTogether = True,
  46. .MinLinesInFirstParagraph = 2,
  47. .MinLinesInLastParagraph = 2,
  48. .RestMarginTop = rc.Left
  49. }
  50.  
  51. '' Open an arbitrary PDF, load it into a temp document and get all fonts:
  52. Using fs = New FileStream(Path.Combine("Resources", "PDFs", sourcePDF), FileMode.Open, FileAccess.Read)
  53. Dim doc1 = New GcPdfDocument()
  54. doc1.Load(fs)
  55. Dim fonts = doc1.GetFonts()
  56. tl.AppendLine($"Total of {fonts.Count} fonts found in {sourcePDF}:")
  57. tl.AppendLine()
  58. Dim i As Integer = 0
  59. For Each fnt In fonts
  60. Dim nativeFont = fnt.CreateNativeFont()
  61. tl.Append($"{i}:{vbTab}BaseFont: {fnt.BaseFont} IsEmbedded: {fnt.IsEmbedded}.")
  62. tl.AppendParagraphBreak()
  63. If nativeFont IsNot Nothing Then
  64. tl.AppendLine($"{vbTab}CreateNativeFont succeeded, family: {nativeFont.FontFamilyName} bold: {nativeFont.FontBold} italic: {nativeFont.FontItalic}.")
  65. Else
  66. tl.AppendLine($"{vbTab}CreateNativeFont failed")
  67. End If
  68. tl.AppendLine()
  69. i += 1
  70. Next
  71. tl.PerformLayout(True)
  72. While (True)
  73. '' 'rest' will accept the text that did not fit:
  74. Dim rest As TextLayout = Nothing
  75. Dim splitResult = tl.Split(tso, rest)
  76. doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty)
  77. If splitResult <> SplitResult.Split Then
  78. Exit While
  79. End If
  80. tl = rest
  81. doc.NewPage()
  82. End While
  83. End Using
  84. '' Done:
  85. doc.Save(stream)
  86. Return doc.Pages.Count
  87. End Function
  88. End Class
  89.