public static void Main(string[] args) { // Extract the page content from the source file. FileStream stream = File.OpenRead("input.pdf"); PDFFile source = new PDFFile(stream); PDFPageContent pageContent = source.ExtractPageContent(0); stream.Close(); PDFFixedDocument document = new PDFFixedDocument(); document.OptionalContentProperties = new PDFOptionalContentProperties(); PDFPage page = document.Pages.Add(); // Create an optional content group (layer) for the extracted page content. PDFOptionalContentGroup ocg = new PDFOptionalContentGroup(); ocg.Name = "Embedded page"; ocg.VisibilityState = PDFOptionalContentGroupVisibilityState.AlwaysVisible; ocg.PrintState = PDFOptionalContentGroupPrintState.NeverPrint; // Draw the extracted page content in the layer page.Canvas.BeginOptionalContentGroup(ocg); page.Canvas.DrawFormXObject(pageContent, 0, 0, page.Width, page.Height); page.Canvas.EndOptionalContentGroup(); // Build the display tree for the optional content PDFOptionalContentDisplayTreeNode ocgNode = new PDFOptionalContentDisplayTreeNode(ocg); document.OptionalContentProperties.DisplayTree.Nodes.Add(ocgNode); using (FileStream output = File.Create("EmbedPageAsLayer.pdf")) { document.Save(output); } }
/// <summary> /// Main method for running the sample. /// </summary> public static SampleOutputInfo[] Run() { PDFFixedDocument document = new PDFFixedDocument(); document.OptionalContentProperties = new PDFOptionalContentProperties(); PDFStandardFont helveticaBold = new PDFStandardFont(PDFStandardFontFace.HelveticaBold, 18); PDFBrush blackBrush = new PDFBrush(); PDFBrush greenBrush = new PDFBrush(PDFRgbColor.DarkGreen); PDFBrush yellowBrush = new PDFBrush(PDFRgbColor.Yellow); PDFPen bluePen = new PDFPen(PDFRgbColor.DarkBlue, 5); PDFPen redPen = new PDFPen(PDFRgbColor.DarkRed, 5); PDFPage page = document.Pages.Add(); page.Canvas.DrawString("Simple optional content: the green rectangle", helveticaBold, blackBrush, 20, 50); PDFOptionalContentGroup ocgPage1 = new PDFOptionalContentGroup(); ocgPage1.Name = "Page 1 - Green Rectangle"; page.Canvas.BeginOptionalContentGroup(ocgPage1); page.Canvas.DrawRectangle(bluePen, greenBrush, 20, 100, 570, 400); page.Canvas.EndOptionalContentGroup(); page = document.Pages.Add(); page.Canvas.DrawString("Multipart optional content: the green rectangles", helveticaBold, blackBrush, 20, 50); PDFOptionalContentGroup ocgPage2 = new PDFOptionalContentGroup(); ocgPage2.Name = "Page 2 - Green Rectangles"; page.Canvas.BeginOptionalContentGroup(ocgPage2); page.Canvas.DrawRectangle(bluePen, greenBrush, 20, 200, 570, 200); page.Canvas.EndOptionalContentGroup(); page.Canvas.DrawRectangle(redPen, yellowBrush, 250, 90, 110, 680); page.Canvas.BeginOptionalContentGroup(ocgPage2); page.Canvas.DrawRectangle(bluePen, greenBrush, 20, 500, 570, 200); page.Canvas.EndOptionalContentGroup(); page = document.Pages.Add(); page.Canvas.DrawString("Imbricated optional content: the green and yellow rectangles", helveticaBold, blackBrush, 20, 50); PDFOptionalContentGroup ocgPage31 = new PDFOptionalContentGroup(); ocgPage31.Name = "Page 3 - Green Rectangle"; page.Canvas.BeginOptionalContentGroup(ocgPage31); page.Canvas.DrawRectangle(bluePen, greenBrush, 20, 100, 570, 600); PDFOptionalContentGroup ocgPage32 = new PDFOptionalContentGroup(); ocgPage32.Name = "Page 3 - Yellow Rectangle"; page.Canvas.BeginOptionalContentGroup(ocgPage32); page.Canvas.DrawRectangle(redPen, yellowBrush, 100, 200, 400, 300); page.Canvas.EndOptionalContentGroup(); // ocgPage32 page.Canvas.EndOptionalContentGroup(); // ocgPage31 page = document.Pages.Add(); page.Canvas.DrawString("Multipage optional content: the green rectangles on page 4 & 5", helveticaBold, blackBrush, 20, 50); PDFOptionalContentGroup ocgPage45 = new PDFOptionalContentGroup(); ocgPage45.Name = "Page 4 & 5 - Green Rectangles"; page.Canvas.BeginOptionalContentGroup(ocgPage45); page.Canvas.DrawRectangle(bluePen, greenBrush, 20, 200, 570, 200); page.Canvas.EndOptionalContentGroup(); page.Canvas.DrawRectangle(redPen, yellowBrush, 250, 90, 110, 680); page.Canvas.BeginOptionalContentGroup(ocgPage45); page.Canvas.DrawRectangle(bluePen, greenBrush, 20, 500, 570, 200); page.Canvas.EndOptionalContentGroup(); page = document.Pages.Add(); page.Canvas.DrawString("Multipage optional content: continued", helveticaBold, blackBrush, 20, 50); page.Canvas.BeginOptionalContentGroup(ocgPage45); page.Canvas.DrawRectangle(bluePen, greenBrush, 20, 200, 570, 200); page.Canvas.EndOptionalContentGroup(); page.Canvas.DrawRectangle(redPen, yellowBrush, 250, 90, 110, 680); page.Canvas.BeginOptionalContentGroup(ocgPage45); page.Canvas.DrawRectangle(bluePen, greenBrush, 20, 500, 570, 200); page.Canvas.EndOptionalContentGroup(); // Build the display tree for the optional content, // how its structure and relationships between optional content groups are presented to the user. PDFOptionalContentDisplayTreeNode ocgPage1Node = new PDFOptionalContentDisplayTreeNode(ocgPage1); document.OptionalContentProperties.DisplayTree.Nodes.Add(ocgPage1Node); PDFOptionalContentDisplayTreeNode ocgPage2Node = new PDFOptionalContentDisplayTreeNode(ocgPage2); document.OptionalContentProperties.DisplayTree.Nodes.Add(ocgPage2Node); PDFOptionalContentDisplayTreeNode ocgPage31Node = new PDFOptionalContentDisplayTreeNode(ocgPage31); document.OptionalContentProperties.DisplayTree.Nodes.Add(ocgPage31Node); PDFOptionalContentDisplayTreeNode ocgPage32Node = new PDFOptionalContentDisplayTreeNode(ocgPage32); ocgPage31Node.Nodes.Add(ocgPage32Node); PDFOptionalContentDisplayTreeNode ocgPage45Node = new PDFOptionalContentDisplayTreeNode(ocgPage45); document.OptionalContentProperties.DisplayTree.Nodes.Add(ocgPage45Node); SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "optionalcontent.pdf") }; return(output); }
private static PDFFixedDocument CreatePDFA2uDocument(Stream iccInput, Stream ttfInput) { iccInput.Position = 0; ttfInput.Position = 0; PDFFixedDocument document = new PDFFixedDocument(); document.OptionalContentProperties = new PDFOptionalContentProperties(); // Setup the document by creating a PDF/A output intent, based on a RGB ICC profile, // and document information and metadata PDFIccColorSpace icc = new PDFIccColorSpace(); byte[] profilePayload = new byte[iccInput.Length]; iccInput.Read(profilePayload, 0, profilePayload.Length); icc.IccProfile = profilePayload; PDFOutputIntent oi = new PDFOutputIntent(); oi.Type = PDFOutputIntentType.PDFA1; oi.Info = "sRGB IEC61966-2.1"; oi.OutputConditionIdentifier = "Custom"; oi.DestinationOutputProfile = icc; document.OutputIntents = new PDFOutputIntentCollection(); document.OutputIntents.Add(oi); document.DocumentInformation = new PDFDocumentInformation(); document.DocumentInformation.Author = "O2 Solutions"; document.DocumentInformation.Title = "PDF4NET PDF/A-2B/U Demo"; document.DocumentInformation.Creator = "PDF4NET PDF/A-2B/U Demo Application"; document.DocumentInformation.Producer = "PDF4NET"; document.DocumentInformation.Keywords = "pdf/a"; document.DocumentInformation.Subject = "PDF/A-2B/U Sample produced by PDF4NET"; document.XmpMetadata = new PDFXmpMetadata(); PDFPage page = document.Pages.Add(); page.Rotation = 90; // All fonts must be embedded in a PDF/A document. PDFStringAppearanceOptions sao = new PDFStringAppearanceOptions(); sao.Font = new PDFAnsiTrueTypeFont(ttfInput, 24, true); sao.Brush = new PDFBrush(new PDFRgbColor(0, 0, 128)); PDFStringLayoutOptions slo = new PDFStringLayoutOptions(); slo.HorizontalAlign = PDFStringHorizontalAlign.Center; slo.VerticalAlign = PDFStringVerticalAlign.Bottom; slo.X = page.Width / 2; slo.Y = page.Height / 2 - 10; page.Canvas.DrawString("PDF4NET", sao, slo); slo.Y = page.Height / 2 + 10; slo.VerticalAlign = PDFStringVerticalAlign.Top; sao.Font.Size = 14; page.Canvas.DrawString("This is a sample PDF/A-2B/U document that shows the PDF/A-2B/U capabilities in PDF4NET library", sao, slo); // PDF/A-2 documents support optional content and transparency. PDFOptionalContentGroup ocgPage1 = new PDFOptionalContentGroup(); ocgPage1.Name = "Green Rectangle"; page.Canvas.BeginOptionalContentGroup(ocgPage1); page.Canvas.SaveGraphicsState(); PDFExtendedGraphicState gs = new PDFExtendedGraphicState(); gs.FillAlpha = 0.8; gs.StrokeAlpha = 0.2; gs.BlendMode = PDFBlendMode.Luminosity; page.Canvas.SetExtendedGraphicState(gs); PDFBrush greenBrush = new PDFBrush(PDFRgbColor.DarkGreen); PDFPen bluePen = new PDFPen(PDFRgbColor.DarkBlue, 5); page.Canvas.DrawRectangle(bluePen, greenBrush, 20, 20, page.Width - 40, page.Height - 40); page.Canvas.RestoreGraphicsState(); page.Canvas.EndOptionalContentGroup(); // Build the display tree for the optional content, // how its structure and relationships between optional content groups are presented to the user. PDFOptionalContentDisplayTreeNode ocgPage1Node = new PDFOptionalContentDisplayTreeNode(ocgPage1); document.OptionalContentProperties.DisplayTree.Nodes.Add(ocgPage1Node); return(document); }