Unable to cast object of type 'System.String' to type 'PdfSharp.Drawing.XBrush'.

  • Thread starter Thread starter Rocky48
  • Start date Start date
R

Rocky48

Guest
I am trying to use PDFSharp to output a document to PDF.

I want the user to be able to choose the color of the font in the document.

I am setting up a list of colors in the combobox.

Private myBrushes() As XBrush = {XBrushes.Red, XBrushes.Green, XBrushes.Blue, XBrushes.Black, XBrushes.Yellow}
Private SelectedBrushByColor As XBrush = XBrushes.Black


The above 2 lines set up the colors and declare the 'SelectedBrushByColor' with a default color.

The following lines add the colors to the combobox and display the color.

cboColor.Items.AddRange(myBrushes)
cboColor.DisplayMember = "Color"


The following should collect the color chosen to the variable 'SelectedBrushByColor', but it causes an exception:

'Unable to cast object of type 'System.String' to type 'PdfSharp.Drawing.XBrush'.'

Private Sub cboColor_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboColor.SelectedIndexChanged
SelectedBrushByColor = CType(cboColor.SelectedItem, XBrush)
End Sub


The original line that I wrote was :

SelectedBrushByColor = cboColor.SelectedItem


but Intellisense suggested that I used Ctype as Option strict on disallows implicit conversions from Object to XBrush.

Can anyone suggest a way around this exception?

I have tried other conversion types but none seem to work.

Here is the complete form:

Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Fonts
Imports PdfSharp.PageOrientation
Imports PdfSharp.PageSize
Imports PdfSharp.Pdf
Imports PdfSharp.Internal
Imports PdfSharp.Drawing.Layout
Imports System.Data.SqlClient
Enum pageOrientation
Landscape
Portrait
End Enum
Enum Pagesize
A4
A5
End Enum
Public Class PrintFrm
Private myBrushes() As XBrush = {XBrushes.Red, XBrushes.Green, XBrushes.Blue, XBrushes.Black, XBrushes.Yellow}
Private SelectedBrushByColor As XBrush = XBrushes.Black
Private boxX As Double
Private boxY As Double
Private cellw As Double
Private cellh As Double
Private ort As String = Nothing
Private FSize As Integer
Private CFont As String = Nothing
Private TxtColor As String
Private sqlAdaptor As SqlDataAdapter
Private dt As New DataTable
Private CSizeSql As String = Nothing
Private cmd As Object = Nothing
Private CSizeValue As Double
Private Narrative As String = Nothing
Private CID As Integer
Private Y As Double
Private Verse As String
'Private myBrushes As XBrush
Private connectionString As String = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
Private Sub PrnForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TxtColorTableAdapter.Fill(Me.ColorDataSet.TxtColor)
Me.CSizeTableAdapter.Fill(Me.Verses_FindDataSet.CSize)
Dim Print As New PrintFrm
Me.TopMost = True
Me.WindowState = FormWindowState.Normal
cboColor.Items.AddRange(myBrushes)
cboColor.DisplayMember = "Color"

For Each oFont As FontFamily In FontFamily.Families 'This line populates the font combo with the system installed fonts
cboFont.Items.Add(oFont.Name)
Next

End Sub
Private Sub cboColor_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboColor.SelectedIndexChanged
SelectedBrushByColor = CType(cboColor.SelectedItem, XBrush)
End Sub
Private Sub btnPaste_Click(sender As Object, e As EventArgs) Handles btnPaste.Click

' Determine if there is any text in the Clipboard to paste into the text box.
If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) = True Then
' Determine if any text is selected in the text box.
If txbVerse.SelectionLength > 0 Then
' Ask user if they want to paste over currently selected text.
If MessageBox.Show("Do you want to paste over current selection?",
"Cut Example", MessageBoxButtons.YesNo) = DialogResult.No Then
' Move selection to the point after the current selection and paste.
txbVerse.SelectionStart = txbVerse.SelectionStart + txbVerse.SelectionLength
End If
End If
' Paste current text in Clipboard into text box.
txbVerse.Paste()
End If
End Sub
Private Sub cboCSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCSize.SelectedIndexChanged

CSizeValue = CInt(cboCSize.SelectedValue)
End Sub


Private Sub nudFSize_ValueChanged(sender As Object, e As EventArgs) Handles nudFSize.ValueChanged
FSize = CInt(nudFSize.Value)
End Sub

Private Sub cboFont_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboFont.SelectedIndexChanged
CFont = cboFont.Text
End Sub

Private Sub nudTop_ValueChanged_1(sender As Object, e As EventArgs) Handles nudTop.ValueChanged
boxY = CInt(nudTop.Value)
End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click

Using connection As New SqlConnection(connectionString), da As New SqlDataAdapter("SELECT CID, BoxX, Cellw, Cellh, ort, Narrative FROM CSize WHERE CID = @CID", connection), dt As New DataTable
connection.Open()

da.SelectCommand.Parameters.AddWithValue("@CID", CSizeValue)
da.Fill(dt)
If dt.Rows.Count > 0 Then
Dim row As DataRow = dt.Rows(0)
printIt(row)

End If
End Using
End Sub
Private Sub printIt(row As DataRow)
Dim ID As Double = CDbl(row("CID"))
Dim X As Double = CDbl(row("BoxX")) * 2.83465
Dim W As Double = CDbl(row("Cellw")) * 2.83465
Dim H As Double = CDbl(row("Cellh")) * 2.83465
Dim O As String = CStr(row("ort"))
Dim N As String = CStr(row("Narrative"))
Dim Y As Double
Dim NL As String = CStr(Chr(13) & Chr(10))
Verse = txbVerse.Text
Replace(Verse, NL, " VBCrLf ")
Dim document As PdfDocument
' Create a new PDF document
document = New PdfDocument()
document.Info.Title = "Created with PDFsharp"

' Create an empty page
Dim page As PdfPage = document.AddPage

If O = "L" Then
page.Orientation = CType(pageOrientation.Landscape, PdfSharp.PageOrientation)
page.Width = XUnit.FromMillimeter(297)
page.Height = XUnit.FromMillimeter(210)
Else
page.Orientation = CType(pageOrientation.Portrait, PdfSharp.PageOrientation)
page.Width = XUnit.FromMillimeter(210)
page.Height = XUnit.FromMillimeter(297)
End If



' Draw the text
' Dim Ftext As String = txbVerse.Text
Y = boxY * 2.834665
'
'myBrush = New XSolidBrush(XColor.FromArgb(207, 0, 44))

Dim gfx As XGraphics
gfx = XGraphics.FromPdfPage(page)
Dim tf As XTextFormatter
tf = New XTextFormatter(gfx)
Dim font As XFont = New XFont(CFont, FSize, XFontStyle.Bold)
Dim rect As XRect
rect = New XRect(X, Y, W, H)
gfx.DrawRectangle(XBrushes.SeaShell, rect)
tf.Alignment = XParagraphAlignment.Center
tf.DrawString(Verse, font, SelectedBrushByColor, rect, XStringFormats.TopLeft)

' Save the document
Dim filename As String = "verse.pdf"
document.Save(filename)

' ...and start a viewer.
Process.Start(filename)
End Sub
End Class









TEH

Continue reading...
 
Back
Top