itextsharp: pdfptables are running into each other

谁说我不能喝 提交于 2019-12-11 18:39:12

问题


Does anyone know why my tables are lapping over each other?

Dim datatable As PdfPTable = New PdfPTable(4)
Dim page As Rectangle = document.PageSize
datatable.TotalWidth = page.Width - document.LeftMargin - document.RightMargin
datatable.DefaultCell.Border = 0

Dim datatable1 As PdfPTable = New PdfPTable(4)
datatable1.TotalWidth = page.Width - document.LeftMargin - document.RightMargin
datatable1.DefaultCell.Border = 0

Dim datatable2 As PdfPTable = New PdfPTable(4)
datatable2.TotalWidth = page.Width - document.LeftMargin - document.RightMargin
datatable2.DefaultCell.Border = 0

Dim last_pos As Integer = 580
Const xpos As Integer = 70

For i = 0 To extreme_foods.Count - 1
    datatable.AddCell(extreme_foods(i))
Next

datatable.WriteSelectedRows(0, -1, xpos, last_pos, writer.DirectContent)

For i = 0 To moderate_foods.Count - 1
    datatable1.AddCell(moderate_foods(i))
Next

last_pos = last_pos - datatable1.TotalHeight
datatable1.WriteSelectedRows(0, -1, xpos, last_pos, writer.DirectContent)

For i = 0 To light_foods.Count - 1
    datatable2.AddCell(light_foods(i))
Next

last_pos = last_pos - datatable2.TotalHeight
datatable2.WriteSelectedRows(0, -1, xpos, last_pos, writer.DirectContent)

'document.Add(datatable)

Catch de As DocumentException
    Console.Error.WriteLine(de.Message)
    MessageBox.Show(de.Message)
Catch ioe As IOException
    Console.Error.WriteLine(ioe.Message)
    MessageBox.Show(ioe.Message)
Catch e As Exception
    Console.Error.WriteLine(e.Message)
    MessageBox.Show(e.Message)

End Try
document.Close()

Even though I'm decrementing last_pos by the height of the previous table, they still lap over in a weird way like this:

screenshot http://img3.imageshack.us/img3/8168/38741459.jpg


回答1:


your subtraction is just a smidge off. you're subtracting your last_pos by the current table instead of the previous table. you should be doing this:

Dim last_pos As Integer = 580
Const xpos As Integer = 70
For i = 0 To extreme_foods.Count - 1
    datatable.AddCell(extreme_foods(i))
Next

datatable.WriteSelectedRows(0, -1, xpos, last_pos, writer.DirectContent)
For i = 0 To moderate_foods.Count - 1
    datatable1.AddCell(moderate_foods(i))
Next

last_pos = last_pos - datatable.TotalHeight '<--- NOT datatable1'
datatable1.WriteSelectedRows(0, -1, xpos, last_pos, writer.DirectContent)


来源:https://stackoverflow.com/questions/1375677/itextsharp-pdfptables-are-running-into-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!