PdfA.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.Text
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Common
  10. Imports GrapeCity.Documents.Drawing
  11. Imports GrapeCity.Documents.Pdf
  12. Imports GrapeCity.Documents.Pdf.Structure
  13. Imports GrapeCity.Documents.Pdf.MarkedContent
  14. Imports GrapeCity.Documents.Pdf.Graphics
  15. Imports GrapeCity.Documents.Pdf.Annotations
  16. Imports GCTEXT = GrapeCity.Documents.Text
  17. Imports GCDRAW = GrapeCity.Documents.Drawing
  18.  
  19. '' This sample shows how to create a PDF/A-3u conformant document.
  20. Public Class PdfA
  21. Sub CreatePDF(ByVal stream As Stream)
  22. Dim doc = New GcPdfDocument()
  23. Dim thedate = New DateTime(1961, 4, 12, 6, 7, 0, DateTimeKind.Utc)
  24.  
  25. '' Mark the document as PDF/A-3u conformant:
  26. doc.ConformanceLevel = PdfAConformanceLevel.PdfA3u
  27.  
  28. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arial.ttf"))
  29. Dim gap = 36
  30.  
  31. '' PDF/A-3a requires all content to be tagged so create and populate StructElement when rendering:
  32. Dim sePart = New StructElement("Part")
  33. doc.StructTreeRoot.Children.Add(sePart)
  34.  
  35. Dim tl As TextLayout = Nothing
  36. '' Add 3 pages with sample content tagged according to PDF/A rules:
  37. For pageNo = 1 To 3
  38. '' add page
  39. Dim Page = doc.Pages.Add()
  40. Dim g = Page.Graphics
  41. Dim y = 72.0F
  42. If doc.Pages.Count = 1 Then
  43. '' Create paragraph element:
  44. Dim seParagraph = New StructElement("P") With {.DefaultPage = Page}
  45. '' Add it to Part element:
  46. sePart.Children.Add(seParagraph)
  47.  
  48. tl = g.CreateTextLayout()
  49. tl.MarginAll = 72
  50. tl.MaxWidth = Page.Size.Width
  51.  
  52. tl.DefaultFormat.Font = fnt
  53. tl.DefaultFormat.FontBold = True
  54. tl.DefaultFormat.FontSize = 20
  55. tl.Append("PDF/A-3A Document")
  56.  
  57. '' PerformLayout is done automatically in a new TextLayout or after a Clear():
  58. ''tl.PerformLayout(True)
  59.  
  60. '' Draw TextLayout within tagged content:
  61. g.BeginMarkedContent(New TagMcid("P", 0))
  62. g.DrawTextLayout(tl, PointF.Empty)
  63. g.EndMarkedContent()
  64.  
  65. y = tl.ContentRectangle.Bottom + gap
  66.  
  67. seParagraph.ContentItems.Add(New McidContentItemLink(0))
  68. End If
  69.  
  70. '' Add some sample paragraphs tagged according to PDF/A rules:
  71. For i = 1 To 3
  72. '' Create paragraph element:
  73. Dim seParagraph = New StructElement("P") With {.DefaultPage = Page}
  74. '' Add it to Part element:
  75. sePart.Children.Add(seParagraph)
  76.  
  77. Dim sb = New StringBuilder()
  78. sb.Append(String.Format("Paragraph {0} on page {1}: ", i, pageNo))
  79. sb.Append(Util.LoremIpsum(1, 2, 4, 5, 10))
  80. Dim para = sb.ToString()
  81.  
  82. tl.Clear()
  83. tl.DefaultFormat.FontSize = 14
  84. tl.DefaultFormat.FontBold = False
  85. tl.MarginTop = y
  86. tl.Append(para)
  87.  
  88. '' Draw TextLayout within tagged content:
  89. g.BeginMarkedContent(New TagMcid("P", i))
  90. g.DrawTextLayout(tl, PointF.Empty)
  91. g.EndMarkedContent()
  92.  
  93. y += tl.ContentHeight + gap
  94.  
  95. '' Add content item to paragraph StructElement:
  96. seParagraph.ContentItems.Add(New McidContentItemLink(i))
  97.  
  98. '' PDF/A-3 allows embedding files into document, but they should be associated with some document element
  99. '' add embedded file associated with seParagraph:
  100. Dim ef1 = EmbeddedFileStream.FromBytes(doc, Encoding.UTF8.GetBytes(para))
  101. '' ModificationDate and MimeType should be specified in case of PDF/A:
  102. ef1.ModificationDate = thedate
  103. ef1.MimeType = "text/plain"
  104. Dim fn = String.Format("Page{0}_Paragraph{1}.txt", pageNo, i)
  105. Dim fs1 = FileSpecification.FromEmbeddedStream(fn, ef1)
  106. '' Relationship should be specified in case of PDF/A:
  107. fs1.Relationship = AFRelationship.Unspecified
  108. doc.EmbeddedFiles.Add(fn, fs1)
  109. seParagraph.AssociatedFiles.Add(fs1)
  110. Next
  111. Next
  112.  
  113. '' PDF/A-3 allows transparency drawing in PDF file, add some:
  114. Dim gpage = doc.Pages(0).Graphics
  115. gpage.FillRectangle(New RectangleF(20, 20, 200, 200), Color.FromArgb(40, Color.Red))
  116.  
  117. '' PDF/A-3 allows using FormXObjects, add one with transparency:
  118. Dim r = New RectangleF(0, 0, 144, 72)
  119. Dim fxo = New FormXObject(doc, r)
  120. Dim gfxo = fxo.Graphics
  121. gfxo.FillRectangle(r, Color.FromArgb(40, Color.Violet))
  122. Dim tf = New TextFormat() With
  123. {
  124. .Font = fnt,
  125. .FontSize = 16,
  126. .ForeColor = Color.FromArgb(100, Color.Black)
  127. }
  128. gfxo.DrawString("FormXObject", tf, r, TextAlignment.Center, ParagraphAlignment.Center)
  129. gfxo.DrawRectangle(r, Color.Blue, 3)
  130. gpage.DrawForm(fxo, New RectangleF(300, 250, r.Width, r.Height), Nothing, ImageAlign.ScaleImage)
  131.  
  132. '' PDF/A-3 allows using embedded files, but each embedded file must be associated with a document's element:
  133. Dim ef = EmbeddedFileStream.FromFile(doc, Path.Combine("Resources", "WordDocs", "ProcurementLetter.docx"))
  134. '' ModificationDate and MimeType should be specified for EmbeddedFile in PDF/A:
  135. ef.ModificationDate = thedate
  136. ef.MimeType = "application/msword"
  137. Dim fs = FileSpecification.FromEmbeddedFile(ef)
  138. fs.Relationship = AFRelationship.Unspecified
  139. doc.EmbeddedFiles.Add("ProcurementLetter.docx", fs)
  140. '' Associate embedded file with the document:
  141. doc.AssociatedFiles.Add(fs)
  142.  
  143. '' Add an attachment associated with an annotation:
  144. Dim sa = New StampAnnotation() With
  145. {
  146. .UserName = "Minerva",
  147. .Font = fnt,
  148. .Rect = New RectangleF(300, 36, 220, 72)
  149. }
  150. sa.Flags = sa.Flags And AnnotationFlags.Print
  151. '' Use a FormXObject to represent the stamp annotation:
  152. Dim stampFxo = New FormXObject(doc, New RectangleF(PointF.Empty, sa.Rect.Size))
  153. Dim gstampFxo = stampFxo.Graphics
  154. gstampFxo.FillRectangle(stampFxo.Bounds, Color.FromArgb(40, Color.Green))
  155. gstampFxo.DrawString("Stamp Annotation" + vbLf + "associated with minerva.jpg", tf, stampFxo.Bounds, TextAlignment.Center, ParagraphAlignment.Center)
  156. gstampFxo.DrawRectangle(stampFxo.Bounds, Color.Green, 3)
  157. ''
  158. sa.AppearanceStreams.Normal.Default = stampFxo
  159. doc.Pages(0).Annotations.Add(sa)
  160. ef = EmbeddedFileStream.FromFile(doc, Path.Combine("Resources", "Images", "minerva.jpg"))
  161. ef.ModificationDate = thedate
  162. ef.MimeType = "image/jpeg"
  163. fs = FileSpecification.FromEmbeddedFile(ef)
  164. fs.Relationship = AFRelationship.Unspecified
  165. doc.EmbeddedFiles.Add("minerva.jpg", fs)
  166. sa.AssociatedFiles.Add(fs)
  167.  
  168. '' Mark the document as conforming to Tagged PDF conventions (required for PDF/A):
  169. doc.MarkInfo.Marked = True
  170.  
  171. '' Metadata.CreatorTool and DocumentInfo.Creator should be the same for a PDF/A document:
  172. doc.Metadata.CreatorTool = doc.DocumentInfo.Creator
  173. '' A title should be specified for PDF/A document:
  174. doc.Metadata.Title = "DsPdf Document"
  175. doc.ViewerPreferences.DisplayDocTitle = True
  176.  
  177. '' Done:
  178. doc.Save(stream)
  179. End Sub
  180. End Class
  181.