I'm having a problem converting a particular code from C# to VB.
C# Code
const string filename = "C:\Sample.pdf";
PdfDocument document = PdfReader.Open(filename);
int imageCount = 0;
// Iterate pages
foreach (PdfPage page in document.Pages)
{
// Get resources dictionary
PdfDictionary resources = page.Elements.GetDictionary("/Resources");
if (resources != null)
{
// Get external objects dictionary
PdfDictionary xObjects = resources.Elements.GetDictionary("/XObject");
if (xObjects != null)
{
ICollection<PdfItem> items = xObjects.Elements.Values;
// Iterate references to external objects
foreach (PdfItem item in items)
{
PdfReference reference = item as PdfReference;
if (reference != null)
{
PdfDictionary xObject = reference.Value as PdfDictionary;
// Is external object an image?
if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Image")
{
ExportImage(xObject, ref imageCount);
}
}
}
}
}
}
What I have done so far is:
VB Code
Dim filename As String = "C:\Sample.pdf"
Dim document As PdfDocument = PdfReader.Open(filename)
Dim imageCount As Integer = 0
For Each page In document.Pages
Dim resources As PdfDictionary
resources = page.Elements.GetDictionary("/Resources")
If resources IsNot Nothing Then
Dim xObjects As PdfDictionary
xObjects = resources.Elements.GetDictionary("/XObject")
If xObjects IsNot Nothing Then
Dim items As ICollection(Of PdfItem) = xObjects.Elements.Values
For Each item In items
'Dim item As PdfReference
Dim reference As PdfReference
reference = item as PdfReference
' ^^I dont know how to do this on VB
Next
End If
End If
Next
So to summarize, this is the line of code that gives me problem converting:
C#
PdfReference reference = item as PdfReference;
VB.Net
Dim reference As PdfReference
reference = item as PdfReference // this gives me an error.