As is stated in this SO answer:
It is TryCast:
Dim x As String = TryCast(y, String)
Or in your case:
Dim reference As PdfReference
reference = TryCast(item, PdfReference)
Pregunta
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.
Solución 3
As is stated in this SO answer:
It is TryCast:
Dim x As String = TryCast(y, String)
Or in your case:
Dim reference As PdfReference
reference = TryCast(item, PdfReference)
Otros consejos
Do you have the correct imports statements and project references?
You might also just try a conversion tool like:
you can use online language conversion tools language convertor
For Each page As PdfPage In document.Pages
' Get resources dictionary
Dim resources As PdfDictionary = page.Elements.GetDictionary("/Resources")
If resources IsNot Nothing Then
' Get external objects dictionary
Dim xObjects As PdfDictionary = resources.Elements.GetDictionary("/XObject")
If xObjects IsNot Nothing Then
Dim items As ICollection(Of PdfItem) = xObjects.Elements.Values
' Iterate references to external objects
For Each item As PdfItem In items
Dim reference As PdfReference = TryCast(item, PdfReference)
If reference IsNot Nothing Then
Dim xObject As PdfDictionary = TryCast(reference.Value, PdfDictionary)
' Is external object an image?
If xObject IsNot Nothing AndAlso xObject.Elements.GetString("/Subtype") = "/Image" Then
ExportImage(xObject, imageCount)
End If
End If
Next
End If
End If
Next
You can use http://converter.telerik.com/ to convert your code from c# to vb.net and back
This should work:
Dim reference as PdfReference = TryCast(item, PdfReference)