dcsimg
Results 1 to 13 of 13

Thread: PDFSharp - Using a variable instead of the color name?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2015
    Location
    Hastings, UK
    Posts
    133

    PDFSharp - Using a variable instead of the color name?

    I am trying to use PDFSharp to change the Font color dynamically, from a combo box.
    The XBrushes class has the named colors as its property, however I want to use the combo box to allow the user to choose from the 141 colors available. I does not allow me to subsititute a variable instead of the color, at least the method I have tried, which may be incorrect.
    This is the code for the line which gives me the error: 'TxtColorValue is not a member of XBrushes'
    Code:
    tf.DrawString(Ftext, CFont, XBrushes.TxtColorValue, rect, XStringFormats.TopLeft)
    If this is impossible does anyone know how I can get the RGB values for all the colors that PDFSharp XBrushes lists?
    Any help will be appreciated!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,567

    Re: PDFSharp - Using a variable instead of the color name?

    Of course it will let you use a variable, but you have to use a variable of the correct type, as is always the case. Why do you think the XBrushes type has a member with that name in the first place? You say that you're trying to use a value from a ComboBox but I don't see any reference to a ComboBox there. Presumably you need to populate that ComboBox with Color values (or whatever specific type that method expects) and then get the user's selection from the SelectedValue of the ComboBox.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,270

    Re: PDFSharp - Using a variable instead of the color name?

    I guess you could look at the code for PDFSharp.
    The values for the colors would be in XKnownColorTable.cs
    Its the same colors that GDI+ uses, so you could probably use the ColorDialog to select the color and then create a brush and cast it to an XBrush.

    I don't know for a fact though, since I haven't dealt with it.
    Last edited by passel; Jun 8th, 2019 at 01:15 PM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2015
    Location
    Hastings, UK
    Posts
    133

    Re: PDFSharp - Using a variable instead of the color name?

    I did not show the whole code as all I was asking is HOW you write a variable as the property of XBrushes, as it only accepts the name of the color property. i.e. Black
    The color combo is bound into the combobox with the following set:
    Display member: Color
    Value Member: XID

    The following code is run to select the color in the combo box:
    Code:
     Private Sub cboColor_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboColor.SelectedIndexChanged
            Me.TxtColorTableAdapter.Fill(Me.ColorDataSet.TxtColor)
            Dim TxtColorValue As Integer
            Dim cboColor As Object = Nothing
            TxtColorValue = cboColor.selectedvalue
        End Sub
    So the question I am asking is how do I represent the color for the XBrushes property instead of a color?

    I have another problem at the moment in the Application Designer.vb which I will post separately.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,567

    Re: PDFSharp - Using a variable instead of the color name?

    Quote Originally Posted by Rocky48 View Post
    I did not show the whole code as all I was asking is HOW you write a variable as the property of XBrushes
    There is no answer to that question because it is nonsensical as asked. You need to think the whole problem through. Firstly, the DrawString method you are calling in post #1 has five parameters and the third parameter is the one of interest. What is the type of that parameter? What you are passing to it now is irrelevant. All that matters is the type. Once you know the type of the parameter, you know what type the value has to be that you pass as a parameter. If you expect that value to come from a ComboBox then obviously you need values of that type in the ComboBox in the first place and then you can get a value of that type from the ComboBox when the user makes a selection.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,270

    Re: PDFSharp - Using a variable instead of the color name?

    The third parameter is an XBrush.
    So you could create a combobox with a bunch of XBrush objects, set to the brush color you want.
    As for a variable, you should just be able to declare a variable of that type as well.

    I don't have PDFSharp, but an example like this might work.
    Code:
    Imports PdfSharp
    Imports PdfSharp.Drawing
    Imports PdfSharp.Pdf
    
    Public Class Form1
        Private myBrushes() As XBrush = {XBrushes.Red, XBrushes.Green, XBrushes.Blue, XBrushes.Black, XBrushes.White, XBrushes.Yellow}
    
        Private SelectedBrushByColor As XBrush = XBrushes.AliceBlue
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.Items.AddRange(myBrushes)
            ComboBox1.DisplayMember = "Color"
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            SelectedBrushByColor = ComboBox1.SelectedItem
        End Sub
    Then you would use SelectedBrushByColor in your drawing call, e.g. DrawString, for the third parameter.
    Code:
    tf.DrawString(Ftext, CFont, SelectedBrushByColor, rect, XStringFormats.TopLeft)
    The DisplayMember = "Color" should display the color property of the various brushes in the Combobox, so the list should be "Red", "Green", "Blue", etc...

    As mentioned, I don't have PDFSharp so couldn't test the example code above.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2015
    Location
    Hastings, UK
    Posts
    133

    Re: PDFSharp - Using a variable instead of the color name?

    Passel.
    Thanks for that! I was hoping that I could use the 141 XBrushes colors, but as a last resort I may use the method you have suggested for a cut down version. On my web version I did use a color picker to give users infinate choice of colours. Don't know whether you can do that with VB?

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,270

    Re: PDFSharp - Using a variable instead of the color name?

    XBrushes is just a wrapper for the GDI+ Brushes class if you're using winforms, or whatever the class is that wpf uses for the same thing if you're writing a wpf application.
    So, the colors that XBrushes supports should be the same colors by the same name that GDI+ supports.
    If you want to create any user defined color, you should be able to do that in a similar manner as the SolidBrush class does, being that the various X.... classes wraps all that same functionality.
    Example to create a custom shade of red
    Code:
    Dim redBrush As XBrush = new XSolidBrush(XColor.FromArgb(224, 0, 64))
    Last edited by passel; Jun 10th, 2019 at 05:38 PM.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Dec 2015
    Location
    Hastings, UK
    Posts
    133

    Re: PDFSharp - Using a variable instead of the color name?

    Passel
    I have just tried the code that you wrote in #6, but I get a cast error:
    System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'PdfSharp.Drawing.XBrush'.'
    It errored on this line:
    Code:
            SelectedBrushByColor = CType(cboColor.SelectedItem, XBrush)
    I know that is different, but it says 'Option strict on disallows implicit conversions from Object to XBrush' when I entered the code as you suggested.
    Is there a way around this?

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Dec 2015
    Location
    Hastings, UK
    Posts
    133

    Re: PDFSharp - Using a variable instead of the color name?

    Found why I got the cast error.
    I had added the colors using the combobox control as well as programmatically.
    However, instead of the color being displayed it just says: 'PDFSharp.Drawing.XColor'
    Any suggestions why as it seems to be correct?

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,567

    Re: PDFSharp - Using a variable instead of the color name?

    Quote Originally Posted by Rocky48 View Post
    Finstead of the color being displayed it just says: 'PDFSharp.Drawing.XColor'
    If you mean that that's what you see in the ComboBox, that's because a ComboBox will simply call ToString on each item to get the text to display by default. The default behaviour of ToString is to return the name of the type and that's what you're seeing, so default behaviour all round. If what you actually want displayed is the value of some property of each item then you do that by setting the DisplayMember of the ComboBox to the name of that property. If there is no such property then you have to manufacture your own items that do have such a property.

  12. #12
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,243

    Re: PDFSharp - Using a variable instead of the color name?

    It would be simpler if you continued to populate the ComboBox with the names of the colours as Strings (no need to set the DisplayMember in that case).

    Then create the brush 'on the fly' from the name when you need to draw your pdf text in the Print method:
    Code:
    '   create the brush
    Dim xClrName As String = CStr(cboColor.SelectedItem)
    Dim xclr As XColor = XColor.FromName(xClrName)
    Dim brush As XSolidBrush = New XSolidBrush(xclr)
    
    '   Draw the text
    tf.DrawString(Ftext, font, brush, bounds, XStringFormats.TopLeft)

    Even simpler would be to fetch a collection of the XKnownColor enum values and bind that collection to the ComboBox when setting up the Form. That would mean you wouldn't have to worry about storing and retrieving the values from a database.
    Code:
    Private Sub PrintForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        'This line populates the font combo with the system installed fonts
        For Each oFont As FontFamily In FontFamily.Families
            cboFont.Items.Add(oFont.Name)
        Next
        cboFont.SelectedIndex = 0
    
        'fetch the XKnownColor values into cboColor
        Dim knownColours() As XKnownColor = XColorResourceManager.GetKnownColors(includeTransparent:=False)
        cboColor.DataSource = knownColours
        cboColor.SelectedIndex = 0
    
    End Sub
    and in your Print method you would have something like:
    Code:
    '''Private Sub printIt()
    
        '   create the brush
        Dim xClr As XColor = XColor.FromKnownColor(CType(cboColor.SelectedItem, XKnownColor))
        Dim brush As XSolidBrush = New XSolidBrush(xClr)
    
        '   Draw the text
        tf.DrawString(Ftext, font, brush, bounds, XStringFormats.TopLeft)


    Having said that, do you know that PDFSharp itself provides a ColorComboBox Control class that looks like this:
    Name:  ColorComboBox.png
Views: 66
Size:  4.1 KB
    It can be found in the PdfSharp-gdi.dll assembly under the PdfSharp.Forms namespace, and you can add it to your IDE Designer toolbox.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Dec 2015
    Location
    Hastings, UK
    Posts
    133

    Re: PDFSharp - Using a variable instead of the color name?

    I now have a working code for printing using PDFSharp:
    Code:
     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
    
            '   create the brush
            Dim xClr As XColor = XColor.FromKnownColor(CType(cboColor.SelectedItem, XKnownColor))
            Dim brush As XSolidBrush = New XSolidBrush(xClr)
    
            ' Draw the text
            Y = boxY * 2.834665
    
            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.Regular)
            Dim rect As XRect
            rect = New XRect(X, Y, W, H)
            gfx.DrawRectangle(XBrushes.SeaShell, rect)
            tf.Alignment = XParagraphAlignment.Center
            tf.DrawString(Verse, font, brush, rect, XStringFormat.TopLeft)
    
            ' Save the document
            Dim filename As String = "verse.pdf"
            document.Save(filename)
    
            ' ...and start a viewer.
            Process.Start(filename)
    
        End Sub
    Thanks for everyones help!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width