VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
Vintasoft.Imaging.Pdf Namespace / PdfDocument Class / OptionalContentProperties Property
Syntax Example Requirements SeeAlso
In This Topic
OptionalContentProperties Property (PdfDocument)
In This Topic
Gets or sets the properties of document optional content.
Syntax
Example

Here is an example that shows how to create a PDF document with optional content:


''' <summary>
''' Creates PDF document with the optional content.
''' </summary>
''' <param name="pdfFilename">The PDF filename.</param>
Public Shared Sub CreateDocumentWithOptionalContent(pdfFilename As String)
    ' create new PDF document
    Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_16)
        ' add empty page (A4 size)
        Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages.Add(Vintasoft.Imaging.PaperSizeKind.A4)

        ' create two optional content groups
        Dim layer1 As New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentGroup(document, "Layer1")
        Dim layer2 As New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentGroup(document, "Layer2")

        ' add optional content groups to OptionalContentProperties
        document.OptionalContentProperties = New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentProperties(document)
        document.OptionalContentProperties.OptionalContentGroups.Add(layer1)
        document.OptionalContentProperties.OptionalContentGroups.Add(layer2)

        ' get PdfGraphics for PDF page
        Using g As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = page.GetGraphics()
            Dim font As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = document.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman)
            Dim brush As New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black)
            Dim brush1 As New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Green)
            Dim brush2 As New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Red)

            ' draw not optional content
            g.DrawString("Not optional content", font, 20, brush, New System.Drawing.PointF(50, 650))

            ' draw optional content "Layer1"
            g.BeginOptionalContent(layer1)
            g.DrawString(String.Format("Optional content '{0}'", layer1.Name), font, 25, brush1, New System.Drawing.PointF(50, 550))
            g.EndOptionalContent()

            ' draw not optional content
            g.DrawString("Not optional content", font, 30, brush, New System.Drawing.PointF(50, 450))

            ' draw optional content "Layer2"
            g.BeginOptionalContent(layer2)
            g.DrawString(String.Format("Optional content '{0}'", layer2.Name), font, 35, brush2, New System.Drawing.PointF(50, 350))
            g.EndOptionalContent()

            ' draw not optional content
            g.DrawString("Not optional content", font, 40, brush, New System.Drawing.PointF(50, 250))
        End Using

        ' create optional content configurations
        Dim configuration1 As New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration(document, "Layer1 and Layer2")
        configuration1.SetGroupVisibility(layer1, True)
        configuration1.SetGroupVisibility(layer2, True)
        Dim configuration2 As New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration(document, "Layer1")
        configuration2.SetGroupVisibility(layer1, True)
        configuration2.SetGroupVisibility(layer2, False)
        Dim configuration3 As New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration(document, "Layer2")
        configuration3.SetGroupVisibility(layer1, False)
        configuration3.SetGroupVisibility(layer2, True)
        Dim configuration4 As New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration(document, "No Layers")
        configuration4.SetGroupVisibility(layer1, False)
        configuration4.SetGroupVisibility(layer2, False)

        ' create list of optional content configuration
        document.OptionalContentProperties.Configurations = New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfigurationList(document)

        ' add configurations to list
        document.OptionalContentProperties.Configurations.Add(configuration1)
        document.OptionalContentProperties.Configurations.Add(configuration2)
        document.OptionalContentProperties.Configurations.Add(configuration3)
        document.OptionalContentProperties.Configurations.Add(configuration4)

        ' set default configuration
        document.OptionalContentProperties.DefaultConfiguration = configuration1

        ' set presentation order
        configuration1.PresentationOrder = New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentPresentationOrder(document, layer1, layer2)

        ' optional content panel is visible
        document.DocumentViewMode = Vintasoft.Imaging.Pdf.PdfDocumentViewMode.UseOC

        ' save changes in PDF document
        document.SaveChanges()
    End Using
End Sub


/// <summary>
/// Creates PDF document with the optional content.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
public static void CreateDocumentWithOptionalContent(string pdfFilename)
{
    // create new PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = 
        new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_16))
    {
        // add empty page (A4 size)
        Vintasoft.Imaging.Pdf.Tree.PdfPage page = document.Pages.Add(
            Vintasoft.Imaging.PaperSizeKind.A4);

        // create two optional content groups
        Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentGroup layer1 =
            new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentGroup(document, "Layer1");
        Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentGroup layer2 =
            new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentGroup(document, "Layer2");

        // add optional content groups to OptionalContentProperties
        document.OptionalContentProperties =
            new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentProperties(document);
        document.OptionalContentProperties.OptionalContentGroups.Add(layer1);
        document.OptionalContentProperties.OptionalContentGroups.Add(layer2);

        // get PdfGraphics for PDF page
        using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics g = page.GetGraphics())
        {
            Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font = document.FontManager.GetStandardFont(
                Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman);
            Vintasoft.Imaging.Pdf.Drawing.PdfBrush brush = new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(
                System.Drawing.Color.Black);
            Vintasoft.Imaging.Pdf.Drawing.PdfBrush brush1 = new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(
                System.Drawing.Color.Green);
            Vintasoft.Imaging.Pdf.Drawing.PdfBrush brush2 = new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(
                System.Drawing.Color.Red);

            // draw not optional content
            g.DrawString("Not optional content", font, 20, brush, new System.Drawing.PointF(50, 650));

            // draw optional content "Layer1"
            g.BeginOptionalContent(layer1);
            g.DrawString(string.Format("Optional content '{0}'", layer1.Name),
                font, 25, brush1, new System.Drawing.PointF(50, 550));
            g.EndOptionalContent();

            // draw not optional content
            g.DrawString("Not optional content", font, 30, brush, new System.Drawing.PointF(50, 450));

            // draw optional content "Layer2"
            g.BeginOptionalContent(layer2);
            g.DrawString(string.Format("Optional content '{0}'", layer2.Name),
                font, 35, brush2, new System.Drawing.PointF(50, 350));
            g.EndOptionalContent();

            // draw not optional content
            g.DrawString("Not optional content", font, 40, brush, new System.Drawing.PointF(50, 250));
        }

        // create optional content configurations
        Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration configuration1 =
            new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration(document, "Layer1 and Layer2");
        configuration1.SetGroupVisibility(layer1, true);
        configuration1.SetGroupVisibility(layer2, true);
        Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration configuration2 =
            new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration(document, "Layer1");
        configuration2.SetGroupVisibility(layer1, true);
        configuration2.SetGroupVisibility(layer2, false);
        Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration configuration3 =
            new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration(document, "Layer2");
        configuration3.SetGroupVisibility(layer1, false);
        configuration3.SetGroupVisibility(layer2, true);
        Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration configuration4 =
            new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration(document, "No Layers");
        configuration4.SetGroupVisibility(layer1, false);
        configuration4.SetGroupVisibility(layer2, false);

        // create list of optional content configuration
        document.OptionalContentProperties.Configurations =
            new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfigurationList(document);

        // add configurations to list
        document.OptionalContentProperties.Configurations.Add(configuration1);
        document.OptionalContentProperties.Configurations.Add(configuration2);
        document.OptionalContentProperties.Configurations.Add(configuration3);
        document.OptionalContentProperties.Configurations.Add(configuration4);

        // set default configuration
        document.OptionalContentProperties.DefaultConfiguration = configuration1;

        // set presentation order
        configuration1.PresentationOrder =
            new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentPresentationOrder(document, layer1, layer2);

        // optional content panel is visible
        document.DocumentViewMode = Vintasoft.Imaging.Pdf.PdfDocumentViewMode.UseOC;

        // save changes in PDF document
        document.SaveChanges();
    }
}

Requirements

Target Platforms: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

See Also