PdfA4.vb
''
'' このコードは、DioDocs for PDF のサンプルの一部として提供されています。
'' © MESCIUS inc. All rights reserved.
''
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-4f準拠のドキュメントを作成する方法を示しています。
Public Class PdfA4
Function CreatePDF(ByVal stream As Stream) As Integer
Dim doc = New GcPdfDocument()
Dim thedate = New DateTime(1961, 4, 12, 6, 7, 0, DateTimeKind.Utc)
'' PDF/A-4 はPDF 2.0に基づいています。
doc.PdfVersion = "2.0"
'' ドキュメントを PDF/A-4f 準拠としてマークします。
doc.Metadata.PdfA = PdfAConformanceLevel.PdfA4f
'' PDF/A-4 では、(文書の日付ではなく)ISO 19005-4:2020規格自体の「改訂」年が求められます。
doc.Metadata.PdfRev = "2020"
Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "ipag.ttc"))
Dim gap = 36
'' PDF/A-4 では、すべてのコンテンツにタグを付ける必要があるため、描画時に StructElement を作成してデータを入力します。
Dim sePart = New StructElement("Part")
doc.StructTreeRoot.Children.Add(sePart)
Dim tl As TextLayout = Nothing
'' PDF/A 規格に従ってタグ付けされたサンプルコンテンツを持つ3つのページを追加します。
For pageNo = 1 To 3
Dim page = doc.Pages.Add()
Dim g = page.Graphics
Dim y = 72.0F
If doc.Pages.Count = 1 Then
'' 段落要素を作成し、それをPart要素に追加します。
Dim seParagraph = New StructElement("P") With {.DefaultPage = page}
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-4 ドキュメント")
'' タグ付きコンテンツ内で 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}
sePart.Children.Add(seParagraph)
Dim sb = New StringBuilder()
sb.Append($"ページ {pageNo} 上の段落 {i}: ")
sb.Append(Environment.NewLine)
sb.Append(Util.getString_ja(0, 1, 2, 3, 6))
Dim para = sb.ToString()
tl.Clear()
tl.DefaultFormat.FontSize = 14
tl.DefaultFormat.FontBold = False
tl.MarginTop = y
tl.Append(para)
g.BeginMarkedContent(New TagMcid("P", i))
g.DrawTextLayout(tl, PointF.Empty)
g.EndMarkedContent()
y += tl.ContentHeight + gap
seParagraph.ContentItems.Add(New McidContentItemLink(i))
Next
Next
'' PDF/A-4 では、PDF ファイルに透過描画を行うことができます。いくつか追加します。
Dim gpage = doc.Pages(0).Graphics
gpage.FillRectangle(New RectangleF(20, 20, 200, 200), Color.FromArgb(40, Color.Red))
'' PDF/A-4 では、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-4f ではファイルをドキュメントに埋め込むことができますが、いずれかのドキュメント要素に関連付ける必要があります。
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.UnicodeFile.FileName = fs.File.FileName
fs.Relationship = AFRelationship.Unspecified
doc.EmbeddedFiles.Add("ProcurementLetter.docx", fs)
'' 埋め込みファイルをドキュメントに関連付けます。
doc.AssociatedFiles.Add(fs)
'' 注釈に関連付けられた添付ファイルを追加します。
'' (リッチメディア注釈はPDF/A-4eでのみ使用可能なため、ここでは代わりにスタンプ注釈を使用します。)
Dim sa = New StampAnnotation() With
{
.UserName = "User",
.Font = fnt,
.Rect = New RectangleF(300, 36, 220, 72)
}
'' PDF/A仕様の規定に従い、Printフラグを設定する必要があります。
sa.Flags = sa.Flags Or 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("Stamp", 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.UnicodeFile.FileName = fs.File.FileName
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 = "PDF/A-4 準拠ドキュメント"
doc.ViewerPreferences.DisplayDocTitle = True
'' PDF/A-4では従来のDocumentInfo辞書が許可されないため、これを削除する必要があります。
'' (DocumentInfo.Creatorは上記で使用されているため、この処理は最後に行ってください。)
doc.DocumentInfo = Nothing
doc.Save(stream)
Return doc.Pages.Count
End Function
End Class