VintaSoft Imaging .NET SDK v8.6
Vintasoft.Imaging.Pdf Namespace / PdfDocument Class / OptionalContentConfiguration Property
Example



In This Topic
    OptionalContentConfiguration Property
    In This Topic
    Gets or sets a configuration of current optional content.
    Syntax
    'Declaration
     
    Public Property OptionalContentConfiguration As PdfOptionalContentConfiguration
    public PdfOptionalContentConfiguration OptionalContentConfiguration {get; set;}
    public: __property PdfOptionalContentConfiguration* get_OptionalContentConfiguration();
    public: __property void set_OptionalContentConfiguration( 
       PdfOptionalContentConfiguration* value
    );
    public:
    property PdfOptionalContentConfiguration^ OptionalContentConfiguration {
       PdfOptionalContentConfiguration^ get();
       void set (    PdfOptionalContentConfiguration^ value);
    }
    Exceptions
    ExceptionDescription
    Thrown if OptionalContentProperties is null.
    Thrown if value is null.
    Example
    This example 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.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.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.PdfOptionalContentGroupList(document)
                ' specify that optional content groups are "Off" by default
                myConfig.BaseState = Vintasoft.Imaging.Pdf.Tree.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.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.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.PdfOptionalContentConfiguration myConfig = 
                    new Vintasoft.Imaging.Pdf.Tree.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.PdfOptionalContentGroupList(document);
                // specify that optional content groups are "Off" by default
                myConfig.BaseState = 
                    Vintasoft.Imaging.Pdf.Tree.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.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: Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also