PdfA.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using System.Text;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Common;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.Structure;
using GrapeCity.Documents.Pdf.MarkedContent;
using GrapeCity.Documents.Pdf.Graphics;
using GrapeCity.Documents.Pdf.Annotations;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsPdfWeb.Demos.Basics
{
    // 此示例演示如何创建符合 PDF/A-3u 标准的文档。
    public class PdfA
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var date = new DateTime(1961, 4, 12, 6, 7, 0, DateTimeKind.Utc);

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

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

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

            TextLayout tl = null;
            // 添加 3 个页面,其中包含根据 PDF/A 规则标记的示例内容:
            for (int pageNo = 1; pageNo <= 3; ++pageNo)
            {
                // 添加页面
                var page = doc.Pages.Add();
                var g = page.Graphics;
                float y = 72;
                if (doc.Pages.Count == 1)
                {
                    // 创建段落元素:
                    var seParagraph = new StructElement("P") { 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));
                }

                // 添加一些根据 PDF/A 规则标记的示例段落:
                for (int i = 1; i <= 3; ++i)
                {
                    // 创建段落元素:
                    var seParagraph = new StructElement("P") { DefaultPage = page };
                    // 将其添加到 Part 元素:
                    sePart.Children.Add(seParagraph);

                    var sb = new StringBuilder();
                    sb.Append(string.Format("第 {1} 页第 {0} 段:", i, pageNo));
                    sb.Append(Common.Util.LoremIpsum(1, 2, 4, 5, 10));
                    var 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 关联的嵌入文件:
                    var ef1 = EmbeddedFileStream.FromBytes(doc, Encoding.UTF8.GetBytes(para));
                    // 如果是 PDF/A,则应指定 ModificationDate 和 MimeType:
                    ef1.ModificationDate = date;
                    ef1.MimeType = "text/plain";
                    var fn = string.Format("页{0}_段落{1}.txt", pageNo, i);
                    var fs1 = FileSpecification.FromEmbeddedStream(fn, ef1);
                    // 应指定 UnicodeFile.FileName 以实现 PDF/A 合规性:
                    fs1.UnicodeFile.FileName = fs1.File.FileName;
                    // 如果是 PDF/A,则应指定关系:
                    fs1.Relationship = AFRelationship.Unspecified;
                    doc.EmbeddedFiles.Add(fn, fs1);
                    seParagraph.AssociatedFiles.Add(fs1);
                }
            }

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

            // PDF/A-3允许您使用FormXObjects,添加一个具有透明度的:
            var r = new RectangleF(0, 0, 144, 72);
            var fxo = new FormXObject(doc, r);
            var gfxo = fxo.Graphics;
            gfxo.FillRectangle(r, Color.FromArgb(40, Color.Violet));
            var tf = new TextFormat()
            {
                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), null, ImageAlign.ScaleImage);

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

            // 添加与注释关联的附件:
            var sa = new StampAnnotation()
            {
                UserName = "密涅瓦",
                Font = fnt,
                Rect = new RectangleF(300, 36, 220, 72),
            };
            sa.Flags |= AnnotationFlags.Print;
            // 使用 FormXObject 来表示图章注释:
            var stampFxo = new FormXObject(doc, new RectangleF(PointF.Empty, sa.Rect.Size));
            var gstampFxo = stampFxo.Graphics;
            gstampFxo.FillRectangle(stampFxo.Bounds, Color.FromArgb(40, Color.Green));
            gstampFxo.DrawString("与 minerva.jpg 相关的图章注释\n", 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 = date;
            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 = "GcDocs.PDF 文档";
            doc.ViewerPreferences.DisplayDocTitle = true;

            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}