问题
I am currently doing a loop from a DataGridView that will read each selected item and create a cell with the item's barcode in it (cells to be exported depends on the quantity the user had input), but If I tried to export 1 barcode label (1 pdfCell) it wouldn't export and says 'the pdfTable is empty' because I have a pdfTable.Columns(5) which is means that my table has a column count of 5. So as long as I try to export cell quantities that are not divisible by 5 it wouldn't export.
Example 1: Item - Banana (10 Barcodes to print) - Exported
Example 2: Item - Apple (1 Barcode to print) - Not Exported (PDFTable is Empty) because 1(one) does not cover a single row of the pdfTable with a column count which is 5(five).
Here is my code for the 'Print Barcode Labels Button' :
Public Function print_itembarcodes(lbl169 As Label)
Dim pdfTable As New PdfPTable(5)
pdfTable.DefaultCell.Padding = 3
pdfTable.WidthPercentage = 100
pdfTable.HorizontalAlignment = Element.ALIGN_CENTER
pdfTable.DefaultCell.Border = Rectangle.NO_BORDER
For i As Integer = 0 To Admin_Menu.BarcodePrintListGrid.Rows.Count - 1
Admin_Menu.Label169.Text = Admin_Menu.BarcodePrintListGrid.Rows(i).Cells(1).Value 'Item Barcode'
Barcode.process_printbarcode(Admin_Menu.Label169) 'Make barcode image function'
save_printbarcode() 'Save barcode to desktop function'
For j As Integer = 0 To Admin_Menu.BarcodePrintListGrid.Rows(i).Cells(5).Value 'Quantity of barcodes to be printed per item'
pdfTable.AddCell(create_barcodecell) 'Add cell with barcode function'
Next
Next
Try
'Exporting to PDF
Dim folderPath As String = "C:\Temp\"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Using stream As New FileStream(folderPath & "temp2.pdf", FileMode.Create)
Dim pdfdoc As New Document(PageSize.A4, 15.0F, 15.0F, 10.0F, 20.0F)
PdfWriter.GetInstance(pdfdoc, stream)
pdfdoc.Open()
pdfdoc.Add(pdfTable) 'Table Declaration'
pdfdoc.Close()
stream.Close()
System.Diagnostics.Process.Start("C:\\Temp\\temp2.pdf")
End Using
Catch ex As MySqlException
MsgBox(ex.Message)
Finally
MysqlConn.Dispose()
End Try
Return True
End Function
Here is my code for creating a cell with barcode :
Public Function create_barcodecell()
Dim SaveFileDialog1 = "D:\School\Capstone\Sta. Lucia East Bowling and Billiard Hall Management System\Item Barcodes\"
Dim Barcode2 As Image = Image.GetInstance(SaveFileDialog1 + Admin_Menu.Label169.Text + ".jpg") 'Barcode Image'
Barcode2.ScaleAbsolute(80.0F, 25.0F)
img.ScalePercent(15.0F) 'Company Logo Image'
img.Alignment = iTextSharp.text.Image.ALIGN_RIGHT
Dim titleFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8)
Dim paragraph As New Paragraph()
paragraph.Add(New Chunk(img, 0, 0))
paragraph.Add(New Chunk(" Item Tag", titleFont))
Dim pdfCell As New PdfPCell
pdfCell.UseVariableBorders = True
pdfCell.BackgroundColor = BaseColor.GRAY
pdfCell.BorderColorLeft = BaseColor.GREEN
pdfCell.BorderColorRight = BaseColor.GREEN
pdfCell.BorderColorTop = BaseColor.GREEN
pdfCell.BorderColorBottom = BaseColor.GREEN
pdfCell.AddElement(paragraph)
pdfCell.AddElement(Barcode2)
pdfCell.AddElement(New Paragraph(" " + Admin_Menu.Label169.Text, titleFont))
Return pdfCell
End Function
回答1:
The main issue is
So as long as I try to export cell quantities that are not divisible by 5 it wouldn't export.
If the remaining cells in that table row are to be empty ones, you can solve this by simply adding additional empty cells until you do have a number of cells divisible by 5.
how can I add empty cells?
I have not done any VB programming since the start of this century, so the syntax might need some correction, but basically you simply do
pdfTable.AddCell(New PdfPCell())
to add a new empty cell to pdfTable
.
and how can I do that adding empty cells until it is divisible by 5?
Count the number of cells you have added already and add until the count can be divided by 5 without remainder.
Dim Count as Integer = 0
For j As Integer = 1 To Admin_Menu.BarcodePrintListGrid.Rows(i).Cells(5).Value 'Quantity of barcodes to be printed per item'
pdfTable.AddCell(create_barcodecell) 'Add cell with barcode function'
Count = Count + 1
Next
While count Mod 5 <> 0
pdfTable.AddCell(New PdfPCell())
Count = Count + 1
End While
(I'm not sure about VB details anymore, in particular whether j
still exists outside the former For
loop and which value it has. Depending on that you may not need a separate counter...)
来源:https://stackoverflow.com/questions/47550078/how-to-export-pdftable-that-is-not-equal-or-divisible-with-the-pdfcell-count