|
-
Jun 15th, 2019, 12:28 PM
#1
Thread Starter
Addicted Member
Using data from an SQL query in my code
I am using PDFSharp and want to use a database to set some of the properties in PDFSharp class code.
I have a table which I call CSize which holds all of the numeric values a some string values.
I have a combobox that lists all of the variations and the user will choose the row in the combobox and the ID, which in have named CID is the Value member and the Selected value in the Combobox control.
I need to use all of the fields in the query, but am not sure how to write the code to use these values in the following PDFSharp class.
I have in some places not tried to use the query table output but the boxX, boxY, cellX, cellY I have, and they are not being read as the document produced is all in the top left corner.
Can someone illustrate to me the syntax for extrating the data from the datatable.
I apologise that I am relatively inexperienced and am a senior citzen, so I am slow to understand.
Here is the 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 sqlAdaptor As SqlDataAdapter
Dim dt As New DataTable
Dim CSizeSql As String = Nothing
Dim cmd As Object = Nothing
Dim CSizeValue As Integer
' the SQL code starts here:
Using connection As New SqlConnection(connectionString)
connection.Open()
CSizeSql = $"SELECT CID, boxX, ort, Narrative FROM CSize WHERE CID = {CSizeValue}"
sqlAdaptor = New SqlDataAdapter(CSizeSql, connection)
dt = New DataTable()
sqlAdaptor.Fill(dt)
End Using
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 font As XFont = New XFont("Verdana", 20, XFontStyle.Bold)
Dim rect As XRect
rect = New XRect(boxX, boxY, cellw, cellh)
gfx.DrawRectangle(XBrushes.SeaShell, rect)
tf.Alignment = XParagraphAlignment.Center
tf.DrawString("This is some text", font, 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
Last edited by Rocky48; Jun 15th, 2019 at 12:29 PM.
Reason: typo
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|