PDF单元格的循环总是关闭一

时间:2017-11-30 19:13:03

标签: vb.net loops for-loop pdf itext

我正在尝试通过单元格制作条形码标签打印机。我有一个有3列的pdfTable。但每当我尝试导出pdfTable时,其单元格数不等于或可被3整除(即列数),则单元格数量总是增加或减少1.

例如:

  • 尝试导出30个可被pdfTable列整除的单元格,这是3(工作正常)

  • 尝试导出1或10个不能被pdfTable列整除的单元格(单元格数量总是偏离1)。结果就像1个细胞变成2个细胞,10个细胞变成11个细胞。

我总是检查我的循环是否有问题导致细胞增加1但我找不到任何东西。

以下是我导出PDF的代码:

Public Function print_itembarcodes(lbl169 As Label)
    Dim pdfTable As New PdfPTable(3) 'pdfTable Column Count'
    pdfTable.DefaultCell.Padding = 3
    pdfTable.WidthPercentage = 100
    pdfTable.HorizontalAlignment = Element.ALIGN_CENTER
    pdfTable.DefaultCell.Border = Rectangle.NO_BORDER
    Dim emptyCell As New PdfPCell
    emptyCell.Border = 0
    Dim count As Integer
    count = 0

    For i As Integer = 0 To Admin_Menu.BarcodePrintListGrid.Rows.Count - 1 'Read item one by one'
        Admin_Menu.Label169.Text = Admin_Menu.BarcodePrintListGrid.Rows(i).Cells(1).Value
        Admin_Menu.Label170.Text = Admin_Menu.BarcodePrintListGrid.Rows(i).Cells(0).Value
        Admin_Menu.Label171.Text = Admin_Menu.BarcodePrintListGrid.Rows(i).Cells(4).Value
        Barcode.process_printbarcode(Admin_Menu.Label169)
        save_printbarcode()


        For j As Integer = 0 To Admin_Menu.BarcodePrintListGrid.Rows(i).Cells(5).Value 'Cell quantity to be exported for one item'
            pdfTable.AddCell(create_barcodecell) 'Create a cell with barcode'
            count = count + 1
        Next

    Next

    For k As Integer = 0 To count Mod 3
        pdfTable.AddCell(emptyCell)
    Next
    count = 0

    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)
            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

以下是我使用条形码创建单元格的功能代码:

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 + ".png")
    Barcode2.ScaleAbsolute(170.0F, 50.0F)
    img.ScalePercent(20.0F)
    img.Alignment = iTextSharp.text.Image.ALIGN_CENTER
    Dim itemname, itemprice, itemcode As New Paragraph
    itemname.Alignment = Element.ALIGN_CENTER
    itemprice.Alignment = Element.ALIGN_CENTER
    itemcode.Alignment = Element.ALIGN_CENTER


    Dim codeFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)
    Dim tagFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8)
    Dim priceFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11)
    codeFont.Color = BaseColor.WHITE
    tagFont.Color = BaseColor.WHITE
    priceFont.Color = BaseColor.WHITE


    itemname.Add(New Chunk(Admin_Menu.Label170.Text, tagFont))
    itemprice.Add(New Chunk("P " + Admin_Menu.Label171.Text + ".00", priceFont))
    itemcode.Add(New Chunk(Admin_Menu.Label169.Text, codeFont))


    Dim pdfCell As New PdfPCell
    pdfCell.UseVariableBorders = True
    pdfCell.BackgroundColor = BaseColor.RED
    pdfCell.BorderColorLeft = BaseColor.BLACK
    pdfCell.BorderColorRight = BaseColor.BLACK
    pdfCell.BorderColorTop = BaseColor.BLACK
    pdfCell.BorderColorBottom = BaseColor.BLACK
    pdfCell.PaddingTop = 10
    pdfCell.PaddingBottom = 10
    pdfCell.PaddingLeft = 8
    pdfCell.PaddingRight = 10

    pdfCell.AddElement(img)
    pdfCell.AddElement(itemname)
    pdfCell.AddElement(Barcode2)
    pdfCell.AddElement(itemcode)
    pdfCell.AddElement(itemprice)


    Return pdfCell
End Function

1 个答案:

答案 0 :(得分:1)

循环由

启动
For j As Integer = 0 To Admin_Menu.BarcodePrintListGrid.Rows(i).Cells(5).Value 'Cell quantity to be exported for one item'

执行Admin_Menu.BarcodePrintListGrid.Rows(i).Cells(5).Value + 1次。您可能希望以值1而不是0开头,以摆脱 off-by-one

循环由

启动
For k As Integer = 0 To count Mod 3

执行(count Mod 3) + 1次。

我的第一个想法是,您可能还希望从值1而不是0开始,毕竟如果Count已经是3的倍数,那么您就不会这样做。我希望它能够运行。

当我设置你的程序的简化版本时,我开始在1循环,我也得到了一个例外。因此,我重新考虑了额外循环的实际目的(我自己在my answer to your previous question中推荐)并且发现迭代次数完全错误:

对于Count = 1,需要2次额外迭代,对于Count = 2,需要1次额外迭代,对于Count = 3,不需要任何迭代,对于Count = 4,需要2次额外迭代,。 ..

因此,根本不需要Count Mod 3迭代第二个循环,而是(3 - (Count Mod 3)) Mod 3次迭代,或更简单

While count Mod 3 <> 0
    pdfTable.AddCell(emptyCell)
    count = count + 1
End While

这也是直觉应该指示的:继续添加空单元格直到单元格数是3的倍数...

测试代码

我简化了你的代码以便能够运行它,毕竟我没有你使用的那么多变量。最终版本(包括上面提到的修复,第一个循环从1开始,第二个循环现在使用While)是这样的:

Public Sub CreateTableLuciferRodstark(filledCells As Integer, fileName As String)
    Dim pdfTable As New PdfPTable(3) 'pdfTable Column Count'
    pdfTable.DefaultCell.Padding = 3
    pdfTable.WidthPercentage = 100
    pdfTable.HorizontalAlignment = Element.ALIGN_CENTER
    pdfTable.DefaultCell.Border = Rectangle.NO_BORDER

    Dim emptyCell As New PdfPCell
    emptyCell.Border = 0

    Dim count As Integer = 0

    For j As Integer = 1 To filledCells
        pdfTable.AddCell(create_barcodecell) 'Create a cell with barcode'
        count = count + 1
    Next

    While count Mod 3 <> 0
        pdfTable.AddCell(emptyCell)
        count = count + 1
    End While

    Using stream As New FileStream(fileName, 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)
        pdfdoc.Close()
    End Using
End Sub

Public Function create_barcodecell()
    Dim pdfCell As New PdfPCell
    pdfCell.UseVariableBorders = True
    pdfCell.BackgroundColor = BaseColor.RED
    pdfCell.BorderColorLeft = BaseColor.BLACK
    pdfCell.BorderColorRight = BaseColor.BLACK
    pdfCell.BorderColorTop = BaseColor.BLACK
    pdfCell.BorderColorBottom = BaseColor.BLACK
    pdfCell.PaddingTop = 10
    pdfCell.PaddingBottom = 10
    pdfCell.PaddingLeft = 8
    pdfCell.PaddingRight = 10

    pdfCell.AddElement(New Paragraph("an item"))
    pdfCell.AddElement(New Paragraph("a code"))
    pdfCell.AddElement(New Paragraph("a price"))

    Return pdfCell
End Function

我运行了1到6个填充单元格的代码:

CreateTableLuciferRodstark(1, "Table1of3.pdf")
CreateTableLuciferRodstark(2, "Table2of3.pdf")
CreateTableLuciferRodstark(3, "Table3of3.pdf")
CreateTableLuciferRodstark(4, "Table4of3.pdf")
CreateTableLuciferRodstark(5, "Table5of3.pdf")
CreateTableLuciferRodstark(6, "Table6of3.pdf")

结果是:

Table1of3.pdf:

Table1of3.pdf

Table2of3.pdf:

Table2of3.pdf

Table3of3.pdf:

enter image description here

Table4of3.pdf:

enter image description here

Table5of3.pdf:

enter image description here

Table6of3.pdf:

enter image description here