2016-11-11 54 views
0

我寫了下面的代碼來生成PDF:如何突破PDF頁面中iTextSharp的PDF

path = Server.MapPath("PDF-Files") 
filename = path + "/mydata.pdf" 

document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 5.0F, 5.0F, 5.0F, 5.0F) 

Dim bfTimes As BaseFont 
Dim times As iTextSharp.text.Font 

bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, False) 
times = New iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL) 

Dim writer As PdfWriter 
writer = PdfWriter.GetInstance(document, New FileStream(filename, FileMode.Create)) 
Dim ev As New itsEvents 
writer.PageEvent = ev 

If document.IsOpen Then 

    document.Close() 

End If 

document.Open() 

Dim spacing As Integer 

spacing = 0 

Dim curY, lineHeight As Double 

curY = document.Top 
lineHeight = 0 

Const maxPerLine As Integer = 3 

For k As Integer = 0 To ds.Tables(0).Rows.Count - 1 

    Dim table As PdfPTable 
    table = New PdfPTable(3) 

    table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER 
    table.TotalWidth = 200.0F 
    table.LockedWidth = True 

    Dim cell As PdfPCell 
    cell = New PdfPCell() 
    cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("id").ToString(), times)) 
    cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("name").ToString() + " " + dsLabelTemp.Tables(0).Rows(k)("city").ToString() + "(" + dsLabelTemp.Tables(0).Rows(k)("post").ToString() + ")", times)) 
cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("userpersonal").ToString(), times)) 
    cell.Colspan = 3 
    cell.HorizontalAlignment = 0 
    cell.Border = iTextSharp.text.Rectangle.NO_BORDER 
    cell.Padding = 20.0F 
    table.AddCell(cell) 


    table.WriteSelectedRows(0, -1, document.Left + spacing, curY, writer.DirectContent) 

    spacing = spacing + 200 

    lineHeight = Math.Max(lineHeight, table.TotalHeight) 

    If 0 = (k + 1) Mod maxPerLine Then 

     curY = curY - lineHeight 
     spacing = 0 
     lineHeight = 0 

    End If 

    Next 

    document.Close() 
    ShowPdf(filename) 

當上面的代碼執行完美的方式,並給出了輸出,但如果第1頁完成它沒有顯示的。

enter image description here

在上圖中可以檢查它沒有顯示完整記錄。

我想打破頁面,如果頁面已滿或增加頁面高度。

如何將數據傳輸到第二頁如果我的第一頁已滿?

+0

如果你想iTextSharp的,以便在不同的頁面分配表,那麼你爲什麼要使用'WriteSelectedRows()'?你爲什麼不把「table」加到'document'中?你在哪裏學習如何編寫iTextSharp代碼?你可以解僱你的老師或扔掉你一直在讀的書嗎? –

+0

因爲我需要在1行中寫入3個表格,然後中斷到新行,並且在接近第1頁之後需要重定向到第2頁。 – deepak

+0

該評論不是我的問題的答案。你所說的沒有道理,你用你的答案證明:「document.Add(table)」正是你所需要的。 –

回答

0

我得到了解決方案。

下面是我的新代碼:

Dim table As PdfPTable 
     table = New PdfPTable(4) 

     table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER 
     table.TotalWidth = 400.0F 
     table.LockedWidth = True 

    For k As Integer = 0 To ds.Tables(0).Rows.Count - 1 

     Dim cell As PdfPCell 
     cell = New PdfPCell() 
     cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("id").ToString(), times)) 
     cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("name").ToString() + " " + dsLabelTemp.Tables(0).Rows(k)("city").ToString() + "(" + dsLabelTemp.Tables(0).Rows(k)("post").ToString() + ")", times)) 
     cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("userpersonal").ToString(), times)) 
     cell.Colspan = 1 
     cell.HorizontalAlignment = 0 
     cell.Border = iTextSharp.text.Rectangle.NO_BORDER 
     cell.Padding = 20.0F 
     table.AddCell(cell) 
    Next 

document.Add(table) 
document.Close() 
ShowPdf(filename) 
+0

這正是我評論的意思。 –