I'm Trying to to rename file image after export with datatable in VB.NET
I use library control LabelControl for .NET from BaiqiSoft
I have also asked Baiqisoft support and then he replied "this is the design behavior of our product." so the results of the export filename from the library are sequential .is there a solution I can rename after export image and also without mistakes I rename the filename based on expression datatable ? or another method?
Please Guide me
for more detailed documentation refer below link.
https://www.mysofttool.com/help/labeldesign/
Imports System.IO
Imports BaiqiSoft.LabelControl
Public Class Form1
Private m_DataTable As DataTable
Private Sub CreateDataTable()
If m_DataTable IsNot Nothing Then Return
m_DataTable = New DataTable
'Columns
m_DataTable.Columns.Add("ProductName", GetType(String))
m_DataTable.Columns.Add("Barcode", GetType(String))
m_DataTable.Columns.Add("Price", GetType(Single))
m_DataTable.Columns.Add("LabelNumber", GetType(Integer))
m_DataTable.Columns.Add("QTY", GetType(Integer))
m_DataTable.Columns.Add("Filename", GetType(String), "ProductName +','+ Barcode")
'Rows
m_DataTable.Rows.Add("Mishi Kobe Niku", "845723054943", 96.0, 2, 1)
m_DataTable.Rows.Add("Carnarvon Tigers", "246321456231", 61.5, 1, 1)
m_DataTable.Rows.Add("Ipoh Coffee", "589412354786", 46.0, 3, 1)
m_DataTable.Rows.Add("Aniseed Syrup", "457125463254", 10.0, 1, 1)
m_DataTable.Rows.Add("Teatime Chocolate Biscuits", "232145674321", 9.2, 5, 1)
End Sub
Private Sub Btnexport_Click(sender As Object, e As EventArgs) Handles Btnexport.Click
Dim theLabel As New LabelPrinting()
theLabel.LicenseKey = ""
theLabel.OpenLabel(Application.StartupPath & "\test.blf")
Dim selectedRows As DataTable = m_DataTable.Clone
selectedRows.Columns("Filename").Expression = Nothing : selectedRows.Columns("Filename").ReadOnly = False
For Each row2 As DataGridViewRow In DataGridView1.Rows
Dim isselect As Boolean = Convert.ToBoolean(row2.Cells("checkboxcolumn").Value)
If isselect Then
Dim newRow As DataRow = selectedRows.NewRow()
newRow("ProductName") = m_DataTable.Rows(row2.Index)("ProductName")
newRow("Barcode") = m_DataTable.Rows(row2.Index)("Barcode")
newRow("Price") = m_DataTable.Rows(row2.Index)("Price")
newRow("LabelNumber") = m_DataTable.Rows(row2.Index)("LabelNumber")
newRow("QTY") = m_DataTable.Rows(row2.Index)("QTY")
newRow("Filename") = m_DataTable.Rows(row2.Index)("ProductName") + "," + m_DataTable.Rows(row2.Index)("Barcode") + ".png"
selectedRows.Rows.Add(newRow)
theLabel.Label.QuantityColumn = "QTY"
End If
Next row2
theLabel.DataSource = selectedRows
theLabel.ExportOptions.FileName = "test"
theLabel.ExportOptions.Path = Application.StartupPath
theLabel.ExportOptions.Quantity = QuantityOptions.AllRecords
theLabel.ExportOptions.Format = ImageFormats.Png
theLabel.ExportOptions.Resolution = 300
theLabel.ExportImage()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateDataTable()
DataGridView1.DataSource = m_DataTable
Dim CheckedBoxColumn As New DataGridViewCheckBoxColumn
CheckedBoxColumn.Width = 40
CheckedBoxColumn.Name = "checkboxcolumn"
CheckedBoxColumn.HeaderText = "Check"
DataGridView1.Columns.Insert(0, CheckedBoxColumn)
End Sub
End Class
Result from Code :
test1.png
test2.png
test3.png
test4.png
test5.png
It should be like this (Desired result):
Mishi Kobe Niku,845723054943.png
Carnarvon Tigers,246321456231.png
Ipoh Coffee,589412354786.png
Aniseed Syrup,457125463254.png
Teatime Chocolate Biscuits,232145674321.png
Is it possible that I can rename like this
test1.png >>>> Mishi Kobe Niku,845723054943.png
test2.png >>>> Carnarvon Tigers,246321456231.png
test3.png >>>> Ipoh Coffee,589412354786.png
test4.png >>>> Aniseed Syrup,457125463254.png
test5.png >>>> Teatime Chocolate Biscuits,232145674321.png
You should be able to rename the files afterwards, just map the original name with the desired new name, for example in a Dictionary(Of String, String)
:
Private Sub Btnexport_Click(sender As Object, e As EventArgs) Handles Btnexport.Click
Dim theLabel As New LabelPrinting()
theLabel.LicenseKey = ""
theLabel.OpenLabel(Application.StartupPath & "\test.blf")
Dim selectedRows As DataTable = m_DataTable.Clone
selectedRows.Columns("Filename").Expression = Nothing : selectedRows.Columns("Filename").ReadOnly = False
Dim fileNameMapper = New Dictionary(Of String, String)(StringComparer.OrdinalIgnoreCase)
Dim fileCounter As Int32 = 0
Dim fileName = "test"
For Each row2 As DataGridViewRow In DataGridView1.Rows
Dim isselect As Boolean = Convert.ToBoolean(row2.Cells("checkboxcolumn").Value)
If isselect Then
Dim newRow As DataRow = selectedRows.NewRow()
newRow("ProductName") = m_DataTable.Rows(row2.Index)("ProductName")
newRow("Barcode") = m_DataTable.Rows(row2.Index)("Barcode")
newRow("Price") = m_DataTable.Rows(row2.Index)("Price")
newRow("LabelNumber") = m_DataTable.Rows(row2.Index)("LabelNumber")
newRow("QTY") = m_DataTable.Rows(row2.Index)("QTY")
newRow("Filename") = m_DataTable.Rows(row2.Index)("ProductName") + "," + m_DataTable.Rows(row2.Index)("Barcode") + ".png"
selectedRows.Rows.Add(newRow)
theLabel.Label.QuantityColumn = "QTY"
fileCounter += 1
Dim originalName = $"{fileName}{fileCounter}.png"
fileNameMapper.Add(originalName, newRow.Field(Of String)("Filename"))
End If
Next row2
theLabel.DataSource = selectedRows
theLabel.ExportOptions.FileName = fileName
theLabel.ExportOptions.Path = Application.StartupPath
theLabel.ExportOptions.Quantity = QuantityOptions.AllRecords
theLabel.ExportOptions.Format = ImageFormats.Png
theLabel.ExportOptions.Resolution = 300
theLabel.ExportImage()
RenameFiles(fileNameMapper, theLabel.ExportOptions.Path)
End Sub
The RenameFiles
method is straightforward with little help of LINQ:
Private Sub RenameFiles(fileNameMapper As Dictionary(Of String,String), path As String)
Dim dir As New DirectoryInfo(path)
Dim files = dir.EnumerateFiles().
Where(Function(fi) fileNameMapper.ContainsKey(fi.Name))
For Each file In files
Dim destination = IO.Path.Combine(file.Directory.FullName, fileNameMapper(file.Name))
file.MoveTo(destination, true)
Next
End Sub