''' <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