3

I am trying to change the font in a PDF file while using VB.net and itextsharp. I think I am allmost there but I am failing at declaring the right things. The code I got looks ok but I get an error.

Dim FontColour As VariantType = New BaseColor(35, 31, 32)
Dim Calibri8 As VariantType = FontFactory.GetFont("Calibri", 8, FontColour)

The errors I get are the following.

Severity Code Description Project File Line Suppression State Error BC30311 Value of type 'BaseColor' cannot be converted to 'VariantType'. J C:\Users\Jeffrey\Documents\Visual Studio 2013\Projects\J\J\UniFormulieren\Offerte\OfferteUniverseel.vb 61 Active

Severity Code Description Project File Line Suppression State Error BC30311 Value of type 'Font' cannot be converted to 'VariantType'. J C:\Users\Jeffrey\Documents\Visual Studio 2013\Projects\J\J\UniFormulieren\Offerte\OfferteUniverseel.vb 62 Active

I also tried using another method as below.

Dim bftimes As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, False)
Dim times As New Font(bftimes, 10)

This way I could introduce "Times new roman" but not Calibri. The code above would only let met add Times new roman, Courier and Helvetica.

I am using Microsoft Visual studio 2017 with VB.NET and iTextSharp. My C# is not that great, but it helped me get this far in VB.net :)

Edit:

The first solution is something I tried using this answer from Nalan M. https://stackoverflow.com/a/29641544/9056174

Edit2:

SOLUTION

Thank you for your help ! I was so hammering on that varianttype that I was blind for another option.

Made the follow adjustments and indeed it worked like a charm!

    Dim FontColour As BaseColor = New BaseColor(35, 31, 32)
    Dim Calibri8 As Font = FontFactory.GetFont("Calibri", 8, FontColour)

Thank you guys for your help. ( Still don't get why I didn't thought of it myself, using the C# to VB.net converter sometimes makes you crazy)

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • *"Value of type 'BaseColor' cannot be converted to 'VariantType'"* and *"Value of type 'Font' cannot be converted to 'VariantType'"* already say it all, don't they? Declare the `FontColour` variable `As BaseColor` and the `Calibri8` variable `As Font`... – mkl Dec 05 '17 at 12:39
  • As an aside: the `var` in c# is completely different from a `VariantType` in vb. – mkl Dec 05 '17 at 12:41
  • I am trying to mark your reply as the answer but I can't find out how. – Jeffrey Lionel van Houdt Dec 05 '17 at 13:13
  • It is not a reply yet, merely a comment. If that comment helped you, I'd be happy to make the comment an answer. – mkl Dec 05 '17 at 14:02
  • It helped me, you sir are my hero. Please make it an answer so I can mark it as the solution that helped me. – Jeffrey Lionel van Houdt Dec 05 '17 at 14:36
  • Ok, I generated an answer. You can mark it as accepted by clicking the tick at its upper left. – mkl Dec 05 '17 at 15:03

1 Answers1

4

The error messages

Value of type 'BaseColor' cannot be converted to 'VariantType'

and

Value of type 'Font' cannot be converted to 'VariantType'

indicate what the issue is: The BaseColor and Font instances cannot be converted to a VariantType.

Thus, one has to assign them to variables of a different type. Fortunately there are natural alternatives: Let's take the known actual types BaseColor and Font!

Dim FontColour As BaseColor = New BaseColor(35, 31, 32)
Dim Calibri8 As Font = FontFactory.GetFont("Calibri", 8, FontColour)
mkl
  • 90,588
  • 15
  • 125
  • 265