通过n和循环拆分输出结果

时间:2018-05-14 19:55:56

标签: vb.net pdf itext pdf-generation oledb

我正在解决我需要创建一个PDF表单的问题。

该PDF表格有8个部分,我需要放置信息,看起来如图所示(仅显示4个)。

重点是我的查询将返回0 - n不同的结果。所以我需要拆分8并在不同页面上发布。

我尝试如下所示,但由于我总是加载一个新文档,因此似乎无效。有没有人有一些建议怎么做?

enter image description here

Try
    Dim sCommand As OleDb.OleDbCommand
    sCommand = New OleDb.OleDbCommand("SELECT a,b,c Query to fetch n results ", _dbCon)
    sCommand.CommandTimeout = 0
    Dim _dbREADER As OleDb.OleDbDataReader
    _dbREADER = sCommand.ExecuteReader
    Dim dt As DataTable = New DataTable()
    dt.Load(_dbREADER)

    Dim totalPages As Integer = dt.Rows.Count / 8
    Dim currentPage As Integer = 1
    Dim rowCounter As Long = 0

    If dt.Rows.Count > 0 Then
        For Each row In dt.Rows
            rowCounter += 1

            If rowCounter = 8 Then
                currentPage += 1
                rowCounter = 0
            End If

            _pdfDocumentOutput = System.IO.Path.GetTempPath() & "MailingForm_" & currentPage & ".pdf"
            SaveFromResources(_pdfDocument, My.Resources.template)

            Using reader As New PdfReader(_pdfDocument)
                Using stamper As New PdfStamper(reader, New IO.FileStream(_pdfDocumentOutput, IO.FileMode.Create))
                    Dim fontName As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "SCRIPTIN.ttf")
                    Dim bf As BaseFont = BaseFont.CreateFont(fontName, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)

                    Dim pdfForm As AcroFields = stamper.AcroFields
                    pdfForm.AddSubstitutionFont(bf)
                    pdfForm.SetField(rowCounter - 1 & "0", row("Customer")) 'Checks the top radiobutton of the VrPr4 field
                    pdfForm.SetField(rowCounter - 1 & "1", row("Address"))
                    pdfForm.SetField(rowCounter - 1 & "2", row("Location"))
                    stamper.FormFlattening = True
                End Using
            End Using
        Next
    End If

    Status.Text = "Store info loaded ! "
Catch ex As Exception
    Status.Text = ex.Message
End Try

1 个答案:

答案 0 :(得分:0)

我通过拆分表找到了解决方案

e

然后循环通过每个表。并将所有结果放在一个文件

 Private Shared Function SplitTable(ByVal originalTable As DataTable, ByVal batchSize As Integer) As List(Of DataTable)
        Dim tables As List(Of DataTable) = New List(Of DataTable)()
        Dim i As Integer = 0
        Dim j As Integer = 1
        Dim newDt As DataTable = originalTable.Clone()
        newDt.TableName = "Table_" & j
        newDt.Clear()
        For Each row As DataRow In originalTable.Rows
            Dim newRow As DataRow = newDt.NewRow()
            newRow.ItemArray = row.ItemArray
            newDt.Rows.Add(newRow)
            i += 1
            If i = batchSize Then
                tables.Add(newDt)
                j += 1
                newDt = originalTable.Clone()
                newDt.TableName = "Table_" & j
                newDt.Clear()
                i = 0
            End If

            If row.Equals(originalTable.Rows(originalTable.Rows.Count - 1)) Then
                tables.Add(newDt)
                j += 1
                newDt = originalTable.Clone()
                newDt.TableName = "Table_" & j
                newDt.Clear()
                i = 0
            End If
        Next

        Return tables
    End Function