如何导出与pdfCell计数不相等或可整除的pdfTable? [英] How to export pdfTable that is not equal or divisible with the pdfCell count?

查看:126
本文介绍了如何导出与pdfCell计数不相等或可整除的pdfTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从 DataGridView 进行循环,该循环将读取每个选定的项目并创建一个带有该项目条形码的单元格(要导出的单元格取决于用户输入的数量),但是,如果我尝试导出1个条形码标签(1个pdfCell),它将不会导出并说 pdfTable为空,因为我有一个pdfTable.Columns(5),这意味着我的表的列数为5。所以只要我尝试导出无法被5整除的单元格数量,就不会导出。

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.

示例1:项目-香蕉(要打印10个条形码)-已导出

Example 1: Item - Banana (10 Barcodes to print) - Exported

示例2:项目-苹果(1条要打印的条形码)-未导出(PDFTable为空),因为1(one)不能覆盖列数为5(5)的pdfTable的一行。

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


推荐答案

主要问题是


只要我尝试导出不可被5整除的单元格数量,就不会导出。

So as long as I try to export cell quantities that are not divisible by 5 it wouldn't export.

如果该表行中的其余单元格为空单元格,则可以通过简单地添加其他空单元格直到您有一定数量的被5整除的单元格来解决此问题。 p>

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?

自本世纪初以来,我还没有进行过任何VB编程,因此语法可能需要进行一些更正,但基本上您只需这样做

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

将新的空单元格添加到 pdfTable

to add a new empty cell to pdfTable.


我该如何添加空单元格直到将其除以5? / p>

and how can I do that adding empty cells until it is divisible by 5?

计算已添加的单元格数ed并添加,直到该计数可以除以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

(我不确定VB详细信息,尤其是 j 在以前的 For之外是否仍然存在循环以及它具有哪个值。取决于您可能不需要单独的计数器...)

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

这篇关于如何导出与pdfCell计数不相等或可整除的pdfTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆