我在将特定代码从C#转换为VB时遇到问题。
C#代码
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);
}
}
}
}
}
}
到目前为止,我所做的是:
VB代码
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
总而言之,这是给我转换问题的代码行:
C#
PdfReference reference = item as PdfReference;
VB.Net
Dim reference As PdfReference
reference = item as PdfReference // this gives me an error.
答案 0 :(得分:1)
答案 1 :(得分:1)
您可以使用在线语言转换工具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
答案 2 :(得分:0)
您可以使用http://converter.telerik.com/将代码从c#转换为vb.net并返回
答案 3 :(得分:0)
这应该有效:
Dim reference as PdfReference = TryCast(item, PdfReference)
答案 4 :(得分:0)
如this SO answer中所述:
是TryCast:
Dim x As String = TryCast(y,String)
或者在你的情况下:
Dim reference As PdfReference
reference = TryCast(item, PdfReference)