TagParagraphs.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 GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Drawing
  10. Imports GrapeCity.Documents.Pdf.Structure
  11. Imports GrapeCity.Documents.Pdf.MarkedContent
  12.  
  13. '' This sample shows how to create tagged (structured) PDF.
  14. '' To see/explore the tags, open the document in Adobe Acrobat Pro and go to
  15. '' View | Navigation Panels | Tags.
  16. Public Class TagParagraphs
  17. Function CreatePDF(ByVal stream As Stream) As Integer
  18. Dim doc = New GcPdfDocument()
  19. Dim rnd = Util.NewRandom()
  20. Dim pageCount = rnd.Next(3, 7)
  21.  
  22. '' Create Part element, it will contain P (paragraph) elements
  23. Dim sePart = New StructElement("Part")
  24. doc.StructTreeRoot.Children.Add(sePart)
  25.  
  26. '' Add some pages, on each page add some paragraphs and tag them:
  27. For pageIndex = 0 To pageCount - 1
  28. '' Add page:
  29. Dim page = doc.Pages.Add()
  30. Dim g = page.Graphics
  31. Const margin = 36.0F
  32. Const dy = 18.0F
  33. '' Add some paragraphs:
  34. Dim paraCount = rnd.Next(1, 5)
  35. Dim y = margin
  36. For i = 0 To paraCount - 1
  37. '' Create paragraph element:
  38. Dim seParagraph = New StructElement("P") With {.DefaultPage = page}
  39. '' Add it to Part element:
  40. sePart.Children.Add(seParagraph)
  41. '' Create paragraph:
  42. Dim tl = g.CreateTextLayout()
  43. tl.DefaultFormat.Font = StandardFonts.Helvetica
  44. tl.DefaultFormat.FontSize = 12
  45. tl.Append(Util.LoremIpsum(1, 1, 5, 5, 10))
  46. tl.MaxWidth = page.Size.Width
  47. tl.MarginLeft = margin
  48. tl.MarginRight = margin
  49. tl.PerformLayout(True)
  50. '' Draw TextLayout within tagged content:
  51. g.BeginMarkedContent(New TagMcid("P", i))
  52. g.DrawTextLayout(tl, New PointF(0, y))
  53. g.EndMarkedContent()
  54. y += tl.ContentHeight + dy
  55. '' Add content item to paragraph StructElement:
  56. seParagraph.ContentItems.Add(New McidContentItemLink(i))
  57. Next
  58. Next
  59. '' Mark document as tagged:
  60. doc.MarkInfo.Marked = True
  61. ''
  62. '' Done:
  63. doc.Save(stream)
  64. Return doc.Pages.Count
  65. End Function
  66. End Class
  67.