VintaSoft Imaging .NET SDK 12.4: Documentation for .NET developer
Vintasoft.Imaging.Pdf Namespace / PdfDocument Class / OptionalContentConfiguration Property
Syntax Exceptions Example Requirements SeeAlso
In This Topic
    OptionalContentConfiguration Property (PdfDocument)
    In This Topic
    Gets or sets a configuration of current optional content.
    Syntax
    Exceptions
    ExceptionDescription
    Thrown if OptionalContentProperties is null.
    Thrown if value is null.
    Example

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

    
    ''' <summary>
    ''' Renders a PDF document with optional content.
    ''' </summary>
    ''' <param name="pdfFilename">The PDF filename.</param>
    Public Shared Sub RenderPdfUsingOptonalContent(pdfFilename As String)
        ' get name of output PDF file
        Dim outputFileName As String = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(pdfFilename), System.IO.Path.GetFileNameWithoutExtension(pdfFilename))
        outputFileName = outputFileName & "_{0}_{1}.tif"
    
        ' open document stream
        Using documentStream As System.IO.Stream = System.IO.File.Open(pdfFilename, System.IO.FileMode.Open)
            ' create image collection, which will store pages of PDF document
            Using documentImages As New Vintasoft.Imaging.ImageCollection()
                ' add document pages to the image collection
                documentImages.Add(documentStream)
    
                ' open PDF document
                Dim document As Vintasoft.Imaging.Pdf.PdfDocument = Vintasoft.Imaging.Pdf.PdfDocumentController.OpenDocument(documentStream)
    
                ' if document does not have optional content
                If document.OptionalContentProperties Is Nothing Then
                    System.Console.WriteLine("Document does not have optional content.")
                    Return
                End If
    
                ' if PDF document has the default optional content configuration
                If document.OptionalContentProperties.DefaultConfiguration IsNot Nothing Then
                    ' set the default optional content configuration as current optional content configuration
                    document.OptionalContentConfiguration = document.OptionalContentProperties.DefaultConfiguration
                    ' render PDF pages and save them to a multipage TIFF file
                    SaveImages(documentImages, String.Format(outputFileName, "defaultConfig", document.OptionalContentConfiguration.Name))
                End If
    
                ' if PDF document has a list of optional content configurations
                If document.OptionalContentProperties.Configurations IsNot Nothing Then
                    ' for each optional content configuration
                    For Each config As Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration In document.OptionalContentProperties.Configurations
                        ' set the optional content configuration as current optional content configuration
                        document.OptionalContentConfiguration = config
                        ' render PDF pages and save them to a multipage TIFF file
                        SaveImages(documentImages, String.Format(outputFileName, "config", config.Name))
                    Next
                End If
    
    
                ' create custom optional content configuration
                Dim myConfig As New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration(document, "MyConfig")
                ' create an empty list of optional content groups whose state should
                ' be set to ON when this configuration is applied
                myConfig.OnGroups = New Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentGroupList(document)
                ' specify that optional content groups are "Off" by default
                myConfig.BaseState = Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfigurationBaseState.Off
                ' set custom optional content configuration as
                ' current optional content configuration of PDF document
                document.OptionalContentConfiguration = myConfig
                ' for each optional content group of PDF document
                For Each group As Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentGroup In document.OptionalContentProperties.OptionalContentGroups
                    ' clear list of "On" groups, i.e. specify that groups must be not visible
                    myConfig.OnGroups.Clear()
                    ' add group to the list of "On" groups, i.e. specify that group must be visible
                    myConfig.OnGroups.Add(group)
                    ' render PDF pages and save them to a file
                    SaveImages(documentImages, String.Format(outputFileName, "group", group.Name))
                Next
    
    
                ' close PDF document
                Vintasoft.Imaging.Pdf.PdfDocumentController.CloseDocument(document)
    
                ' free resources
                documentImages.ClearAndDisposeItems()
            End Using
        End Using
    End Sub
    
    ''' <summary>
    ''' Saves images (PDF pages) to the specified file.
    ''' </summary>
    ''' <param name="images">The image collection.</param>
    ''' <param name="filename">The filename where image collection must be saved.</param>
    Private Shared Sub SaveImages(images As Vintasoft.Imaging.ImageCollection, filename As String)
        ' create encoder
        Using encoder As Vintasoft.Imaging.Codecs.Encoders.MultipageEncoderBase = Vintasoft.Imaging.Codecs.Encoders.AvailableEncoders.CreateMultipageEncoder(filename)
            ' set rendering settings
            images.SetRenderingSettings(New Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(New Vintasoft.Imaging.Resolution(300, 300)))
    
            ' specify that image collection should not be switched to new file
            encoder.SaveAndSwitchSource = False
            ' save images to a file
            images.SaveSync(filename, encoder)
        End Using
    End Sub
    
    
    
    /// <summary>
    /// Renders a PDF document with optional content.
    /// </summary>
    /// <param name="pdfFilename">The PDF filename.</param>
    public static void RenderPdfUsingOptonalContent(string pdfFilename)
    {
        // get name of output PDF file
        string outputFileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(pdfFilename), 
            System.IO.Path.GetFileNameWithoutExtension(pdfFilename));
        outputFileName = outputFileName + "_{0}_{1}.tif";
    
        // open document stream
        using (System.IO.Stream documentStream = 
            System.IO.File.Open(pdfFilename, System.IO.FileMode.Open))
        {
            // create image collection, which will store pages of PDF document
            using (Vintasoft.Imaging.ImageCollection documentImages = 
                new Vintasoft.Imaging.ImageCollection())
            {
                // add document pages to the image collection
                documentImages.Add(documentStream);
    
                // open PDF document
                Vintasoft.Imaging.Pdf.PdfDocument document = 
                    Vintasoft.Imaging.Pdf.PdfDocumentController.OpenDocument(documentStream);
    
                // if document does not have optional content
                if (document.OptionalContentProperties == null)
                {
                    System.Console.WriteLine("Document does not have optional content.");
                    return;
                }
    
                // if PDF document has the default optional content configuration
                if (document.OptionalContentProperties.DefaultConfiguration != null)
                {
                    // set the default optional content configuration as current optional content configuration
                    document.OptionalContentConfiguration = document.OptionalContentProperties.DefaultConfiguration;
                    // render PDF pages and save them to a multipage TIFF file
                    SaveImages(documentImages, string.Format(outputFileName, "defaultConfig", document.OptionalContentConfiguration.Name));
                }
    
                // if PDF document has a list of optional content configurations
                if (document.OptionalContentProperties.Configurations != null)
                {
                    // for each optional content configuration
                    foreach (Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration config in
                        document.OptionalContentProperties.Configurations)
                    {
                        // set the optional content configuration as current optional content configuration
                        document.OptionalContentConfiguration = config;
                        // render PDF pages and save them to a multipage TIFF file
                        SaveImages(documentImages, string.Format(outputFileName, "config", config.Name));
                    }
                }
    
    
                // create custom optional content configuration
                Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration myConfig =
                    new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfiguration(document, "MyConfig");
                // create an empty list of optional content groups whose state should
                // be set to ON when this configuration is applied
                myConfig.OnGroups =
                    new Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentGroupList(document);
                // specify that optional content groups are "Off" by default
                myConfig.BaseState =
                    Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentConfigurationBaseState.Off;
                // set custom optional content configuration as
                // current optional content configuration of PDF document
                document.OptionalContentConfiguration = myConfig;
                // for each optional content group of PDF document
                foreach (Vintasoft.Imaging.Pdf.Tree.OptionalContent.PdfOptionalContentGroup 
                    group in document.OptionalContentProperties.OptionalContentGroups)
                {
                    // clear list of "On" groups, i.e. specify that groups must be not visible
                    myConfig.OnGroups.Clear();
                    // add group to the list of "On" groups, i.e. specify that group must be visible
                    myConfig.OnGroups.Add(group);
                    // render PDF pages and save them to a file
                    SaveImages(documentImages, string.Format(outputFileName, "group", group.Name));
                }
    
    
                // close PDF document
                Vintasoft.Imaging.Pdf.PdfDocumentController.CloseDocument(document);
    
                // free resources
                documentImages.ClearAndDisposeItems();
            }
        }
    }
    
    /// <summary>
    /// Saves images (PDF pages) to the specified file.
    /// </summary>
    /// <param name="images">The image collection.</param>
    /// <param name="filename">The filename where image collection must be saved.</param>
    private static void SaveImages(Vintasoft.Imaging.ImageCollection images, string filename)
    {
        // create encoder
        using (Vintasoft.Imaging.Codecs.Encoders.MultipageEncoderBase encoder = 
            Vintasoft.Imaging.Codecs.Encoders.AvailableEncoders.CreateMultipageEncoder(filename))
        {
            // set rendering settings
            images.SetRenderingSettings(new Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(
                new Vintasoft.Imaging.Resolution(300, 300)));
    
            // specify that image collection should not be switched to new file
            encoder.SaveAndSwitchSource = false;
            // save images to a file
            images.SaveSync(filename, encoder);
        }
    }
    
    

    Requirements

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

    See Also