iTextSharp中的新页面 [英] New page in iTextSharp

查看:125
本文介绍了iTextSharp中的新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我尝试使用iTextSharp创建一个PDF,该PDF从表单中读取值,然后从DATAGRIDVIEW中插入其中的值.
因此,在DATAGRIDVIEW的特定行数之后,我想创建一个新页面(PdfFile.NewPage).


这段代码将DATAGRIDVIEW写入Pdf:


Hello,

I try to create a PDF with iTextSharp which reads the values out of a form and wich are inserted from a DATAGRIDVIEW.
So, after a specific amount of lines of the DATAGRIDVIEW I want to create a new page(PdfFile.NewPage).


This code writes the DATAGRIDVIEW into a Pdf:


            For i As Integer = 0 To DataGridView1.Rows.Count - 2
                For j As Integer = 0 To DataGridView1.Columns.Count - 1
                    pdfcell = New PdfPCell(New Phrase(DataGridView1(j, i).Value.ToString(), pTable))
                    PdfTable.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                    PdfTable.AddCell(pdfcell)
                Next
            Next
            PdfFile.Add(PdfTable)







这样做的原因是因为DATAGRIDVIEW覆盖了页脚.

在20 DATAGRIDVIEW行之后,应跟随PdfFile.NewPage().在第二页上,应继续21 DATAGRIDVIEW行.
例如,在50行之后,出现一个新页面...

那是页脚的代码:









The reason for doing that is because the DATAGRIDVIEW overwrites the footer.

After 20 DATAGRIDVIEW lines a PdfFile.NewPage() should follow. On the second page it should continue with the 21 DATAGRIDVIEW line.
For example after 50 lines a new page...

That`s the code of the footer:



    Inherits PdfPageEventHelper
    Public Overrides Sub OnendPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)
        Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
        Dim cb As PdfContentByte = writer.DirectContent
        cb.BeginText()
        cb.SetFontAndSize(bf, 12)
        cb.SetTextMatrix(50, 30)
        cb.ShowText("TEXTTEXT " & writer.PageNumber)
        cb.EndText()





我已经尝试过了,它只是创建了下一页,但仍然覆盖了页脚:





I already tried this it just creating the next page but still overwrites the footer:

          For i As Integer = 0 To DataGridView1.Rows.Count - 2
             For j As Integer = 0 To DataGridView1.Columns.Count - 1
                pdfcell = New PdfPCell(New Phrase(DataGridView1(j, i).Value.ToString(), pTable))
                PdfTable.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                PdfTable.AddCell(pdfcell)
             Next
             Select Case i
                Case 20, 30, 60, 99
                   PdfFile.NewPage()
             End Select
          Next
          PdfFile.Add(PdfTable)






预先感谢您的帮助.

问候

GerryGras






Thanks in advance for your help.

Greetings

GerryGras

推荐答案

 对于每页上想要的每50行或多行,您需要将前50行添加到表中,将表格添加到文档中,调用NewPage方法,然后为接下来的50个(或更多)创建一个新表. 不断重复直到找到最后一行.下面是我汇总的一个示例,您可以在新表单项目中尝试.向其添加1个DataGridView,并向其添加5列.

 For each 50 or however many rows you want on each page,  you need to add the first 50 rows to the table,  add the table to the document,  call the NewPage method,  and then create a new table for the next 50 or however many.  Keep repeating that until the last row is hit.  Below is an example that i threw together that you can try in a new form project.  Add 1 DataGridView to it and add 5 columns to it.

 您可以使用任意多的列,在一定程度上但在我的示例中,我将Load事件中的代码设置为为每行添加5列的值.另外,我使用了一个特殊的子函数,将每个单元格添加到文档中. 进行设置时,您可以在创建每个单元格时控制其字体和文本的对齐方式.如果您希望页面上的不同单元格以不同的方式对齐,则非常方便.例如,标头文本居中,数据单元格 保持对齐,如下面的示例图片所示.

 You can use however many columns you want,  to an extent,  but in my example i have the code in the Load event set up to add values for 5 columns for each row.  Also,  i used a special sub that i call to add each Cell to the document.  It is set up so that you can control the font and alignment of the text in each cell as it is created.  Kind of handy if you wanted different cells on the pages aligned differently.  For example,  the header text centered and the data cells left aligned as seen in my example image below.

 页面上适合的列和行的数量将取决于A3之类的文档的大小, A4, A5,等等...这也将取决于您用于所有内容的字体大小.您可以 想要根据您的需求进行调整.

 The amount of columns and rows that will fit on the page is of coarse going to depend on the size of the document like A3,  A4,  A5,  and so on...  It will also depend on the Font sizes you use for everything too.  You may want to adjust all that to your needs.

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i As Integer = 1 To 200
            DataGridView1.Rows.Add(New Object() {"C1-R" & i.ToString, "C2-R" & i.ToString, "C3-R" & i.ToString, "C4-R" & i.ToString, "C5-R" & i.ToString})
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim MyPdfFilePath As String = "C:\TestFolder\MyPdf.pdf"
        Dim pSize As New Rectangle(PageSize.A4)
        Dim PdfDoc As Document = New Document(pSize, 10, 10, 10, 10)

        Using fs As New FileStream(MyPdfFilePath, FileMode.Create, FileAccess.Write, FileShare.None)
            Using pdfWrite As PdfWriter = PdfWriter.GetInstance(PdfDoc, fs)
                PdfDoc.Open()

                Dim pTable As New PdfPTable(1)
                pTable.AddCell(getCell("Page Title For This Document", Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, FontFactory.GetFont("Arial", 20, FontStyle.Bold Or FontStyle.Underline, BaseColor.BLACK), False, BaseColor.WHITE, 15, 35, 3, 3))
                PdfDoc.Add(pTable)

                Dim cTable As New PdfPTable(DataGridView1.ColumnCount)
                For Each col As DataGridViewColumn In DataGridView1.Columns
                    cTable.AddCell(getCell(col.HeaderText, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, FontFactory.GetFont("Arial", 12, BaseColor.WHITE), True, BaseColor.BLUE, 3, 3, 3, 3))
                Next

                Dim tblfont As Font = FontFactory.GetFont("Arial", 9, BaseColor.BLACK)
                For i As Integer = 0 To DataGridView1.RowCount - 1
                    For Each c As DataGridViewCell In DataGridView1.Rows(i).Cells
                        cTable.AddCell(getCell(c.Value.ToString, Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, tblfont, True, BaseColor.WHITE, 3, 3, 3, 3))
                    Next

                    Dim rcnt As Integer = i + 1
                    'We want to start a new page and put a page number at the bottom of the page if the row count meats one of the following conditions.
                    '(1) If we are at row 40 then it is the end of the first page. We only want 40 on the first page because it has a header that takes some page space.
                    '(2) If the row count is greater than 50 and is at an even increment of 50 (after the first 40 rows).
                    '(3) If it is the last row of the data but, does not meat the first two conditions.
                    If rcnt = 40 OrElse (rcnt > 50 AndAlso (rcnt - 40) Mod 50 = 0) OrElse rcnt = DataGridView1.RowCount Then

                        PdfDoc.Add(cTable) 'add the table to the pdf document

                        'put the page number at the bottom of the page
                        Dim PgSize As Rectangle = pdfWrite.PageSize
                        Dim ColText As New ColumnText(pdfWrite.DirectContent)
                        Dim PgFont As Font = FontFactory.GetFont("Arial", 9, FontStyle.Bold, BaseColor.BLACK)
                        Dim PgPhrase As New Phrase("Page " & pdfWrite.PageNumber.ToString, PgFont)
                        ColText.SetSimpleColumn(PgPhrase, PgSize.Bottom - 20, PgSize.Left + 20, PgSize.Right - 20, PgSize.Bottom - 50, 15, Element.ALIGN_CENTER)
                        ColText.Go()

                        PdfDoc.NewPage() 'start a new page
                        cTable = New PdfPTable(DataGridView1.ColumnCount) 'create a new table for the next 50
                    End If
                Next

                PdfDoc.Close()
            End Using
        End Using

        PdfDoc.Dispose()
    End Sub

    ''' <summary>Creates a new cell for the table.</summary>
    ''' <param name="Text">The text string for the cell.</param>
    ''' <param name="TextHorzAlign">Horizontal alighnment for the text.</param>
    ''' <param name="TextVertAlign">Vertical alighnment for the text.</param>
    ''' <param name="TextFont">The font for the text.</param>
    ''' <param name="ShowBorder">True to show the cell border. False to hide the cell border.</param>
    ''' <param name="CellBackClr">The background color of the cell.</param>
    ''' <param name="PadTop">The amount of padding on the top of the text string.</param>
    ''' <param name="PadBottom">The amount of padding on the bottom of the text string.</param>
    ''' <param name="PadLeft">The amount of padding on the left of the text string.</param>
    ''' <param name="PadRight">The amount of padding on the right of the text string.</param>
    Public Function getCell(Text As String, TextHorzAlign As Integer, TextVertAlign As Integer, TextFont As Font, ShowBorder As Boolean, CellBackClr As BaseColor, PadTop As Single, PadBottom As Single, PadLeft As Single, PadRight As Single) As PdfPCell
        Dim cell As New PdfPCell(New Phrase(Text, TextFont))
        cell.BackgroundColor = CellBackClr
        cell.PaddingLeft = PadLeft
        cell.PaddingRight = PadRight
        cell.PaddingTop = PadTop
        cell.PaddingBottom = PadBottom
        cell.HorizontalAlignment = TextHorzAlign
        cell.VerticalAlignment = TextVertAlign
        If Not ShowBorder Then cell.Border = PdfPCell.NO_BORDER
        Return cell
    End Function
End Class

 

 

 

 


这篇关于iTextSharp中的新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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