PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 1:23 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Fri Jun 07, 2019 10:30 pm 
Offline

Joined: Fri May 03, 2019 10:10 pm
Posts: 21
I am trying to use the PDFSharp to print text in the color of the users choice.
I have created a variable call 'TxtColorValue' to represent the color chosen, however will not accept the variable name and only the color name. Am I doing something wrong?
I am not an expert programmer and am still learning!
Can anyone point out where I am going wrong?
Here is the sub with the code that is not working:
Code:
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
        MessageBox.Show(Replace(txbVerse.Text, Chr(13) & Chr(10), " VBCrLf "))
        Dim boxX As Integer
        Dim boxY As Integer
        Dim cellw As Integer
        Dim cellh As Integer
        Dim ort As String = Nothing
        Dim FSize As Integer
        Dim CFont As Object = Nothing
        Dim TxtColorValue As String = Nothing

        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 ort = "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



        Dim gfx As XGraphics
        gfx = XGraphics.FromPdfPage(page)
        Dim font As XFont = New XFont(FSize, 20, XFontStyle.Regular)
        Dim tf As XTextFormatter
        tf = New XTextFormatter(gfx)

        Dim rect As XRect
        rect = New XRect(boxX, boxY, cellw, cellh)
        gfx.DrawRectangle(XBrushes.SeaShell, rect)
        tf.Alignment = XParagraphAlignment.Center
        tf.DrawString(Ftext, CFont, XBrushes.TxtColorValue, rect, XStringFormats.TopLeft)

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

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


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 11, 2019 10:33 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!

I am not a VB.NET expert.
I would try to set the colour Enum values to the drop-down listbox so you do not get a string, but the correct enum value.

To convert from string to enum you can use the Parse method of enum. That's plan B if you add strings to the listbox.

Rocky48 wrote:
Here is the sub with the code that is not working

I assume the code does not even compile. It would have been nice to indicate the line that does not compile along with the compiler error message.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Sat Jun 15, 2019 4:12 pm 
Offline

Joined: Fri May 03, 2019 10:10 pm
Posts: 21
The code has changed since I first posted and the error is in the same line as before on line 66.
The error says: 'Object reference not set to an instance of an object.' The line is shown below:
Code:
tf.DrawString(Ftext, New XFont(CFont, FSize, XFontStyle.Regular), XBrushes.Black, rect, XStringFormats.TopLeft)

Its not with reference to the color as I had other errors which needed eliminating before I tackled that problem.
I still need to know how to declare a variable to represent one of the 141 colors. The variable name for the color I am using is 'TxtColorValue'
Here is the complete form code:
Code:
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 connectionString As String = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
    Public Property dt As Object
    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
        MessageBox.Show(Replace(txbVerse.Text, Chr(13) & Chr(10), " VBCrLf "))
        Dim boxX As Integer
        Dim boxY As Integer
        Dim cellw As Integer
        Dim cellh As Integer
        Dim ort As String = Nothing
        Dim FSize As Integer
        Dim CFont As String = Nothing
        Dim TxtColorValue As String

        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 ort = "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



        Dim gfx As XGraphics
        gfx = XGraphics.FromPdfPage(page)
        Dim tf As XTextFormatter
        tf = New XTextFormatter(gfx)

        Dim rect As XRect
        rect = New XRect(boxX, boxY, cellw, cellh)
        gfx.DrawRectangle(XBrushes.SeaShell, rect)
        tf.Alignment = XParagraphAlignment.Center
        tf.DrawString(Ftext, New XFont(CFont, FSize, XFontStyle.Regular), XBrushes.Black, rect, XStringFormats.TopLeft)

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

        ' ...and start a viewer.
        Process.Start(filename)
    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 PrnForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ColorDataSet.TxtColor' table. You can move, or remove it, as needed.
        Me.TxtColorTableAdapter.Fill(Me.ColorDataSet.TxtColor)
        'TODO: This line of code loads data into the 'Verses_Find_Color_DataSet.TxtColor' table. You can move, or remove it, as needed.
        Me.CSizeTableAdapter.Fill(Me.Verses_FindDataSet.CSize)
        Dim Print As New PrintFrm
        Me.TopMost = True
        Me.WindowState = FormWindowState.Normal

        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 cboCSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCSize.SelectedIndexChanged
        Me.CSizeTableAdapter.Fill(Me.Verses_FindDataSet.CSize)
        Dim cboCSize As Integer
        Dim CSizeValue As Integer = cboCSize

    End Sub

    Private Sub cboColor_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboColor.SelectedIndexChanged
        ' Me.TxtColorTableAdapter.Fill(Me.ColorDataSet.TxtColor)
        Dim cboColor As Integer
        Dim TxtColorValue As Integer = cboColor
    End Sub

    Private Sub nudTop_ValueChanged(sender As Object, e As EventArgs)
        Dim nudTop As Integer
        Dim CTop As Integer = nudTop

    End Sub

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

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


Top
 Profile  
Reply with quote  
PostPosted: Sun Jun 16, 2019 10:55 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Rocky48 wrote:
The code has changed since I first posted and the error is in the same line as before on line 66.
The error says: 'Object reference not set to an instance of an object.'
So set a breakpoint on that line, run the code in the debugger and check which parameter is "null" (whatever "null" is in VB.NET).
Difficult for others to figure this out without debugger.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 19, 2019 8:54 pm 
Offline

Joined: Fri May 03, 2019 10:10 pm
Posts: 21
I eventually sorted this out.
In the combobox you can set the data binding mode. I originally set the Display Member as the 'Color' and the Value Member as XID (its ID number). I simply changed the Value Member to @Color' as well.


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 24, 2019 7:49 pm 
Offline

Joined: Fri May 03, 2019 10:10 pm
Posts: 21
Somehow I seemed to have deviated from the original question!
The XBrushes class requires a color name as its property. I wish to let the user choose the color from a combobox, but the property of the XBrushes expects the property to be one of the 141 color names. I want to replace the property with a variable name. Is there any way that I can do this?
This is the up to date code:
Code:
 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


        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, XBrushes.TxtColor, rect, XStringFormats.TopLeft)

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

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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 161 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group