PdfA.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports System.Text
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Common
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Structure
Imports GrapeCity.Documents.Pdf.MarkedContent
Imports GrapeCity.Documents.Pdf.Graphics
Imports GrapeCity.Documents.Pdf.Annotations
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' 此示例演示如何创建符合 PDF/A-3u 标准的文档。
Public Class PdfA
    Sub CreatePDF(ByVal stream As Stream)
        Dim doc = New GcPdfDocument()
        Dim thedate = New DateTime(1961, 4, 12, 6, 7, 0, DateTimeKind.Utc)

        '' 将文档标记为符合 PDF/A-3u 标准:
        doc.ConformanceLevel = PdfAConformanceLevel.PdfA3u

        Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arial.ttf"))
        Dim gap = 36

        '' PDF/A-3a 要求对所有内容进行标记,因此在渲染时创建并填充 StructElement:
        Dim sePart = New StructElement("Part")
        doc.StructTreeRoot.Children.Add(sePart)

        Dim tl As TextLayout = Nothing
        '' 添加 3 个页面,其中包含根据 PDF/A 规则标记的示例内容:
        For pageNo = 1 To 3
            '' 添加页面
            Dim Page = doc.Pages.Add()
            Dim g = Page.Graphics
            Dim y = 72.0F
            If doc.Pages.Count = 1 Then
                '' 创建段落元素:
                Dim seParagraph = New StructElement("P") With {.DefaultPage = Page}
                '' 将其添加到 Part 元素:
                sePart.Children.Add(seParagraph)

                tl = g.CreateTextLayout()
                tl.MarginAll = 72
                tl.MaxWidth = Page.Size.Width

                tl.DefaultFormat.Font = fnt
                tl.DefaultFormat.FontBold = True
                tl.DefaultFormat.FontSize = 20
                tl.Append("PDF/A-3A 文档")

                '' PerformLayout 在新的 TextLayout 中或在 Clear() 之后自动完成:
                '' tl.PerformLayout(true);

                '' 在标记内容中绘制 TextLayout:
                g.BeginMarkedContent(New TagMcid("P", 0))
                g.DrawTextLayout(tl, PointF.Empty)
                g.EndMarkedContent()

                y = tl.ContentRectangle.Bottom + gap

                seParagraph.ContentItems.Add(New McidContentItemLink(0))
            End If

            '' 添加一些根据 PDF/A 规则标记的示例段落:
            For i = 1 To 3
                '' 创建段落元素:
                Dim seParagraph = New StructElement("P") With {.DefaultPage = Page}
                '' 将其添加到 Part 元素:
                sePart.Children.Add(seParagraph)

                Dim sb = New StringBuilder()
                sb.Append(String.Format("第 {1} 页第 {0} 段:", i, pageNo))
                sb.Append(Util.LoremIpsum(1, 2, 4, 5, 10))
                Dim para = sb.ToString()

                tl.Clear()
                tl.DefaultFormat.FontSize = 14
                tl.DefaultFormat.FontBold = False
                tl.MarginTop = y
                tl.Append(para)

                '' 在标记内容中绘制 TextLayout:
                g.BeginMarkedContent(New TagMcid("P", i))
                g.DrawTextLayout(tl, PointF.Empty)
                g.EndMarkedContent()

                y += tl.ContentHeight + gap

                '' 将内容项添加到段落 StructElement:
                seParagraph.ContentItems.Add(New McidContentItemLink(i))

                '' PDF/A-3 允许您将文件嵌入到文档中,但它们应该与某些文档元素相关联
                '' 添加与 seParagraph 关联的嵌入文件:
                Dim ef1 = EmbeddedFileStream.FromBytes(doc, Encoding.UTF8.GetBytes(para))
                '' 如果是 PDF/A,则应指定 ModificationDate 和 MimeType:
                ef1.ModificationDate = thedate
                ef1.MimeType = "text/plain"
                Dim fn = String.Format("页{0}_段落{1}.txt", pageNo, i)
                Dim fs1 = FileSpecification.FromEmbeddedStream(fn, ef1)
                '' 如果是 PDF/A,则应指定关系:
                fs1.Relationship = AFRelationship.Unspecified
                doc.EmbeddedFiles.Add(fn, fs1)
                seParagraph.AssociatedFiles.Add(fs1)
            Next
        Next

        '' PDF/A-3允许在PDF文件中绘制透明度,添加一些:
        Dim gpage = doc.Pages(0).Graphics
        gpage.FillRectangle(New RectangleF(20, 20, 200, 200), Color.FromArgb(40, Color.Red))

        '' PDF/A-3允许您使用FormXObjects,添加一个具有透明度的:
        Dim r = New RectangleF(0, 0, 144, 72)
        Dim fxo = New FormXObject(doc, r)
        Dim gfxo = fxo.Graphics
        gfxo.FillRectangle(r, Color.FromArgb(40, Color.Violet))
        Dim tf = New TextFormat() With
            {
                .Font = fnt,
                .FontSize = 16,
                .ForeColor = Color.FromArgb(100, Color.Black)
            }
        gfxo.DrawString("FormXObject", tf, r, TextAlignment.Center, ParagraphAlignment.Center)
        gfxo.DrawRectangle(r, Color.Blue, 3)
        gpage.DrawForm(fxo, New RectangleF(300, 250, r.Width, r.Height), Nothing, ImageAlign.ScaleImage)

        '' PDF/A-3 允许您使用嵌入文件,但每个嵌入文件必须与文档的元素关联:
        Dim ef = EmbeddedFileStream.FromFile(doc, Path.Combine("Resources", "WordDocs", "ProcurementLetter.docx"))
        '' 应为 PDF/A 中的 EmbeddedFile 指定 ModificationDate 和 MimeType:
        ef.ModificationDate = thedate
        ef.MimeType = "application/msword"
        Dim fs = FileSpecification.FromEmbeddedFile(ef)
        fs.Relationship = AFRelationship.Unspecified
        doc.EmbeddedFiles.Add("ProcurementLetter.docx", fs)
        '' 将嵌入文件与文档关联:
        doc.AssociatedFiles.Add(fs)

        '' 添加与注释关联的附件:
        Dim sa = New StampAnnotation() With
            {
                .UserName = "密涅瓦",
                .Font = fnt,
                .Rect = New RectangleF(300, 36, 220, 72)
            }
        sa.Flags = sa.Flags And AnnotationFlags.Print
        '' 使用 FormXObject 来表示图章注释:
        Dim stampFxo = New FormXObject(doc, New RectangleF(PointF.Empty, sa.Rect.Size))
        Dim gstampFxo = stampFxo.Graphics
        gstampFxo.FillRectangle(stampFxo.Bounds, Color.FromArgb(40, Color.Green))
        gstampFxo.DrawString("与 minerva.jpg 相关的图章注释" + vbLf, tf, stampFxo.Bounds, TextAlignment.Center, ParagraphAlignment.Center)
        gstampFxo.DrawRectangle(stampFxo.Bounds, Color.Green, 3)
        ''
        sa.AppearanceStreams.Normal.Default = stampFxo
        doc.Pages(0).Annotations.Add(sa)
        ef = EmbeddedFileStream.FromFile(doc, Path.Combine("Resources", "Images", "minerva.jpg"))
        ef.ModificationDate = thedate
        ef.MimeType = "image/jpeg"
        fs = FileSpecification.FromEmbeddedFile(ef)
        fs.Relationship = AFRelationship.Unspecified
        doc.EmbeddedFiles.Add("minerva.jpg", fs)
        sa.AssociatedFiles.Add(fs)

        '' 将文档标记为符合标记 PDF 约定(PDF/A 必需):
        doc.MarkInfo.Marked = True

        '' 对于 PDF/A 文档,Metadata.CreatorTool 和 DocumentInfo.Creator 应该相同:
        doc.Metadata.CreatorTool = doc.DocumentInfo.Creator
        '' 应为 PDF/A 文档指定标题:
        doc.Metadata.Title = "GcDocs.PDF 文档"
        doc.ViewerPreferences.DisplayDocTitle = True

        '' 完毕:
        doc.Save(stream)
    End Sub
End Class