VintaSoft Imaging .NET SDK v8.6
Vintasoft.Imaging.Pdf.Tree Namespace / PdfTreeNodeBase Class / BasicObject Property
Example



In This Topic
    BasicObject Property
    In This Topic
    Gets the basic object of this tree node.
    Syntax
    'Declaration
     
    <BrowsableAttribute(False)>
    Public ReadOnly Property BasicObject As PdfBasicObject
    [Browsable(false)]
    public PdfBasicObject BasicObject {get;}
    [Browsable(false)]
    public: __property PdfBasicObject* get_BasicObject();
    [Browsable(false)]
    public:
    property PdfBasicObject^ BasicObject {
       PdfBasicObject^ get();
    }
    Example
    This example shows how to add the custom data to the PDF tree nodes.
    ''' <summary>
    ''' Tests add/get custom data.
    ''' </summary>
    Public Shared Sub Test()
        ' create PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument("textCustomData.pdf", Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14)
            ' add empty page to the PDF document
            Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages.Add(Vintasoft.Imaging.PaperSizeKind.A4)
                
            ' add custom data to the PDF document catalog
            AddCustomStringData(document.Catalog, "MyStringData", "Test String Value 1")
            AddCustomStreamData(document.Catalog, "MyStreamData", System.Text.Encoding.Unicode.GetBytes("Test Stream Data 1"))
                
            ' add custom data to the PDF page
            AddCustomStringData(page, "MyStringData", "Test String Value 2")
            AddCustomStreamData(page, "MyStreamData", System.Text.Encoding.Unicode.GetBytes("Test Stream Data 2"))
                
            ' save changes in PDF document
            document.SaveChanges()
        End Using
                
        ' open PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument("textCustomData.pdf")
            ' read "/MyStringData" entry from the PDF document catalog
            System.Console.WriteLine(GetCustomStringData(document.Catalog, "MyStringData"))
                
            ' read "/MyStreamData" entry from the PDF document catalog
            System.Console.WriteLine(System.Text.Encoding.Unicode.GetString(GetCustomStreamData(document.Catalog, "MyStreamData")))
                
            ' read "/MyStringData" entry from PDF page
            System.Console.WriteLine(GetCustomStringData(document.Pages(0), "MyStringData"))
                
            ' read "/MyStreamData" entry from PDF page
            System.Console.WriteLine(System.Text.Encoding.Unicode.GetString(GetCustomStreamData(document.Pages(0), "MyStreamData")))
        End Using
    End Sub
                
    ''' <summary>
    ''' Adds the custom string data with the specified name to the specified PDF tree node.
    ''' </summary>
    ''' <param name="pdfTreeNode">The PDF tree node.</param>
    ''' <param name="dataName">The data name.</param>
    ''' <param name="dataValue">The data value.</param>
    Public Shared Sub AddCustomStringData(pdfTreeNode As Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase, dataName As String, dataValue As String)
        Dim dictionary As Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary = GetDictionary(pdfTreeNode)
        If dictionary.ContainsKey(dataName) Then
            Throw New System.ArgumentException(String.Format("Key '{0}' already exists.", dataName))
        End If
        dictionary(dataName) = New Vintasoft.Imaging.Pdf.BasicTypes.PdfString(dataValue)
    End Sub
                
    ''' <summary>
    ''' Returns the custom string data with the specified name from the specified PDF tree node.
    ''' </summary>
    ''' <param name="pdfTreeNode">The PDF tree node.</param>
    ''' <param name="dataName">The data name.</param>
    ''' <returns>The custom string data with the specified name from the specified PDF tree node.</returns>
    Public Shared Function GetCustomStringData(pdfTreeNode As Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase, dataName As String) As String
        Dim dictionary As Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary = GetDictionary(pdfTreeNode)
        If dictionary.ContainsKey(dataName) Then
            Return DirectCast(dictionary(dataName), Vintasoft.Imaging.Pdf.BasicTypes.PdfString).ValueAsTextString
        End If
        Throw New System.ArgumentException(String.Format("Key '{0}' is not found.", dataName))
    End Function
                
    ''' <summary>
    ''' Adds the custom stream data with the specified name to the specified PDF tree node.
    ''' </summary>
    ''' <param name="pdfTreeNode">The PDF tree node.</param>
    ''' <param name="dataName">The data name.</param>
    ''' <param name="dataValue">The data value.</param>
    Public Shared Sub AddCustomStreamData(pdfTreeNode As Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase, dataName As String, dataValue As Byte())
        Dim dictionary As Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary = GetDictionary(pdfTreeNode)
        If dictionary.ContainsKey(dataName) Then
            Throw New System.ArgumentException(String.Format("Key '{0}' already exists.", dataName))
        End If
        ' create a stream
        Dim stream As New Vintasoft.Imaging.Pdf.BasicTypes.PdfStream(pdfTreeNode.Document)
        ' write data, compressed with ZIP compression, to the stream
        stream.SetBytes(dataValue, Vintasoft.Imaging.Pdf.PdfCompression.Zip, Vintasoft.Imaging.Pdf.PdfCompressionSettings.DefaultSettings)
        ' create an indirect object from the stream
        Dim indirectObject As Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectObject = Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectObject.Create(pdfTreeNode.Document, stream)
        ' set the reference for the specified entry
        dictionary(dataName) = indirectObject.GetReference()
    End Sub
                
    ''' <summary>
    ''' Returns the custom stream data with specified name from specified tree node.
    ''' </summary>
    ''' <param name="pdfTreeNode">The PDF tree node.</param>
    ''' <param name="dataName">The data name.</param>
    Public Shared Function GetCustomStreamData(pdfTreeNode As Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase, dataName As String) As Byte()
        Dim dictionary As Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary = GetDictionary(pdfTreeNode)
        If dictionary.ContainsKey(dataName) Then
            Dim reference As Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectReference = DirectCast(dictionary(dataName), Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectReference)
            Dim indirectObject As Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectObject = Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectObject.GetByReference(reference)
            Dim stream As Vintasoft.Imaging.Pdf.BasicTypes.PdfStream = DirectCast(indirectObject.Value, Vintasoft.Imaging.Pdf.BasicTypes.PdfStream)
            Return stream.GetBytes()
        End If
        Throw New System.ArgumentException(String.Format("Key '{0}' is not found.", dataName))
    End Function
                
    ''' <summary>
    ''' Returns the dictionary from PDF tree node.
    ''' </summary>
    ''' <param name="node">The node.</param>
    Private Shared Function GetDictionary(node As Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase) As Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary
        Dim dictionary As Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary = TryCast(node.BasicObject, Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary)
        If dictionary Is Nothing Then
            Throw New System.ArgumentException(String.Format("Node {0} is not a dictionary!", node.[GetType]().Name))
        End If
        Return dictionary
    End Function
    /// <summary>
    /// Tests add/get custom data.
    /// </summary>
    public static void Test()
    {
        // create PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = 
            new Vintasoft.Imaging.Pdf.PdfDocument("textCustomData.pdf", Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14))
        {
            // add empty page to the PDF document
            Vintasoft.Imaging.Pdf.Tree.PdfPage page = document.Pages.Add(Vintasoft.Imaging.PaperSizeKind.A4);
            
            // add custom data to the PDF document catalog
            AddCustomStringData(document.Catalog, 
                "MyStringData", "Test String Value 1");                
            AddCustomStreamData(document.Catalog, 
                "MyStreamData", System.Text.Encoding.Unicode.GetBytes("Test Stream Data 1"));
            
            // add custom data to the PDF page
            AddCustomStringData(page, 
                "MyStringData", "Test String Value 2");
            AddCustomStreamData(page, 
                "MyStreamData", System.Text.Encoding.Unicode.GetBytes("Test Stream Data 2"));
                
            // save changes in PDF document
            document.SaveChanges();
        }
                
        // open PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = 
            new Vintasoft.Imaging.Pdf.PdfDocument("textCustomData.pdf"))
        {
            // read "/MyStringData" entry from the PDF document catalog
            System.Console.WriteLine(
                GetCustomStringData(document.Catalog, "MyStringData"));
                
            // read "/MyStreamData" entry from the PDF document catalog
            System.Console.WriteLine(
                System.Text.Encoding.Unicode.GetString(GetCustomStreamData(document.Catalog, "MyStreamData")));
            
            // read "/MyStringData" entry from PDF page
            System.Console.WriteLine(
                GetCustomStringData(document.Pages[0], "MyStringData"));
            
            // read "/MyStreamData" entry from PDF page
            System.Console.WriteLine(
                System.Text.Encoding.Unicode.GetString(GetCustomStreamData(document.Pages[0], "MyStreamData")));
        }
    }       
                
    /// <summary>
    /// Adds the custom string data with the specified name to the specified PDF tree node.
    /// </summary>
    /// <param name="pdfTreeNode">The PDF tree node.</param>
    /// <param name="dataName">The data name.</param>
    /// <param name="dataValue">The data value.</param>
    public static void AddCustomStringData(
        Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase pdfTreeNode,
        string dataName,
        string dataValue)
    {
        Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary dictionary = GetDictionary(pdfTreeNode);
        if (dictionary.ContainsKey(dataName))
            throw new System.ArgumentException(string.Format("Key '{0}' already exists.", dataName));
        dictionary[dataName] = new Vintasoft.Imaging.Pdf.BasicTypes.PdfString(dataValue);
    }
                
    /// <summary>
    /// Returns the custom string data with the specified name from the specified PDF tree node.
    /// </summary>
    /// <param name="pdfTreeNode">The PDF tree node.</param>
    /// <param name="dataName">The data name.</param>
    /// <returns>The custom string data with the specified name from the specified PDF tree node.</returns>
    public static string GetCustomStringData(Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase pdfTreeNode, string dataName)
    {
        Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary dictionary = GetDictionary(pdfTreeNode);
        if (dictionary.ContainsKey(dataName))
            return ((Vintasoft.Imaging.Pdf.BasicTypes.PdfString)dictionary[dataName]).ValueAsTextString;
        throw new System.ArgumentException(string.Format("Key '{0}' is not found.", dataName));
    }
                
    /// <summary>
    /// Adds the custom stream data with the specified name to the specified PDF tree node.
    /// </summary>
    /// <param name="pdfTreeNode">The PDF tree node.</param>
    /// <param name="dataName">The data name.</param>
    /// <param name="dataValue">The data value.</param>
    public static void AddCustomStreamData(
        Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase pdfTreeNode,
        string dataName,
        byte[] dataValue)
    {
        Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary dictionary = GetDictionary(pdfTreeNode);
        if (dictionary.ContainsKey(dataName))
            throw new System.ArgumentException(string.Format("Key '{0}' already exists.", dataName));
        // create a stream
        Vintasoft.Imaging.Pdf.BasicTypes.PdfStream stream = 
            new Vintasoft.Imaging.Pdf.BasicTypes.PdfStream(pdfTreeNode.Document);
        // write data, compressed with ZIP compression, to the stream
        stream.SetBytes(dataValue, Vintasoft.Imaging.Pdf.PdfCompression.Zip, 
            Vintasoft.Imaging.Pdf.PdfCompressionSettings.DefaultSettings);
        // create an indirect object from the stream
        Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectObject indirectObject = 
            Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectObject.Create(pdfTreeNode.Document, stream);
        // set the reference for the specified entry
        dictionary[dataName] = indirectObject.GetReference();
    }
                
    /// <summary>
    /// Returns the custom stream data with specified name from specified tree node.
    /// </summary>
    /// <param name="pdfTreeNode">The PDF tree node.</param>
    /// <param name="dataName">The data name.</param>
    public static byte[] GetCustomStreamData(Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase pdfTreeNode, string dataName)
    {
        Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary dictionary = GetDictionary(pdfTreeNode);
        if (dictionary.ContainsKey(dataName))
        {
            Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectReference reference = 
                (Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectReference)dictionary[dataName];
            Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectObject indirectObject = 
                Vintasoft.Imaging.Pdf.BasicTypes.PdfIndirectObject.GetByReference(reference);
            Vintasoft.Imaging.Pdf.BasicTypes.PdfStream stream = 
                (Vintasoft.Imaging.Pdf.BasicTypes.PdfStream)indirectObject.Value;
            return stream.GetBytes();
        }
        throw new System.ArgumentException(string.Format("Key '{0}' is not found.", dataName));
    }
                
    /// <summary>
    /// Returns the dictionary from PDF tree node.
    /// </summary>
    /// <param name="node">The node.</param>
    private static Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary GetDictionary(
        Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase node)
    {
        Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary dictionary = 
            node.BasicObject as Vintasoft.Imaging.Pdf.BasicTypes.PdfDictionary;
        if (dictionary == null)
            throw new System.ArgumentException(
                string.Format("Node {0} is not a dictionary!", node.GetType().Name));
        return dictionary;
    }
    Requirements

    Target Platforms: Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also