I'm trying to make a PDF table like the one in the top image below. The second image is what I'm getting. This is my first time using iText. I have the needed NuGet packages and I am using VB in Visual Studio 2022. I'm using the iText v9 suite.
What I'm getting is
What I need is
The code I have for this table is.
Dim mytable7 As Table = New Table(8)
Dim commodityStr As String = ("Commodities requiring special Or additional care Or attention in handling Or stowing must be so marked And packaged as to ensure safe transportation with ordinary care. " &
"See Section 2(e) of NMFC Item 360")
mytable7.AddCell(New Cell(1, 9).SetWidth(765).Add(New Paragraph("CARRIER INFORMATION")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(8).SetFontColor(ColorConstants.WHITE).SetBackgroundColor((ColorConstants.BLACK)))
mytable7.AddCell(New Cell(2, 1).SetWidth(100).Add(New Paragraph("HANDELING UNIT").SetFontSize(FontSize.Small).SetTextAlignment(TextAlignment.CENTER)))
mytable7.AddCell(New Cell(2, 1).SetWidth(100).Add(New Paragraph("PACKAGE").SetFontSize(FontSize.Small).SetTextAlignment(TextAlignment.CENTER)))
mytable7.AddCell(New Cell(2, 1).SetWidth(113).Add(New Paragraph("WEIGHT").SetFontSize(FontSize.Small).SetTextAlignment(TextAlignment.CENTER)))
mytable7.AddCell(New Cell(2, 1).SetWidth(65).Add(New Paragraph("HM").SetFontSize(FontSize.Small).SetTextAlignment(TextAlignment.CENTER)))
mytable7.AddCell(New Cell(3, 1).SetWidth(275).Add(New Paragraph("COMMODITY DESCRIPTION").SetTextAlignment(TextAlignment.CENTER).SetFontSize(FontSize.Small).Add(New Paragraph(commodityStr).SetFontSize(FontSize.XSmall).SetTextAlignment(TextAlignment.CENTER))))
mytable7.AddCell(New Cell(1, 1).SetWidth(90).Add(New Paragraph("LTLONLY").SetFontSize(FontSize.Small).SetTextAlignment(TextAlignment.CENTER)))
'new Text("\n")
document.Add(mytable7)
Dim columnwidth As Single() = {38.0F, 38.0F, 37.0F, 37.0F, 50.0F, 50.0F, 60.0F, 50.0F}
Dim table8 As Table = New Table(columnwidth)
table8.AddCell(New Cell(rowspan:=2, colspan:=1).Add(New Paragraph(text:="22").SetFontSize(FontSize.Medium).SetTextAlignment(TextAlignment.CENTER)))
table8.AddCell(New Cell(rowspan:=2, colspan:=1).Add(New Paragraph(text:="SKD").SetFontSize(FontSize.Medium).SetTextAlignment(TextAlignment.CENTER)))
table8.AddCell(New Cell(rowspan:=2, colspan:=1).Add(New Paragraph(text:="3").SetFontSize(FontSize.Medium).SetTextAlignment(TextAlignment.CENTER)))
table8.AddCell(New Cell(rowspan:=2, colspan:=1).Add(New Paragraph(text:="CTN").SetFontSize(FontSize.Medium).SetTextAlignment(TextAlignment.CENTER)))
table8.AddCell(New Cell(rowspan:=1, colspan:=1).Add(New Paragraph(text:="2800lbs")).SetFontSize(FontSize.Medium).SetTextAlignment(TextAlignment.CENTER))
table8.AddCell(New Cell(rowspan:=1, colspan:=1).Add(New Paragraph(text:="HM").SetFontSize(FontSize.Medium).SetTextAlignment(TextAlignment.CENTER)))
table8.AddCell(New Cell(rowspan:=1, colspan:=1).SetWidth(200).Add(New Paragraph(text:="Used Books 42x42x56").SetFontSize(FontSize.Medium).SetTextAlignment(TextAlignment.CENTER)))
table8.AddCell(New Cell(rowspan:=1, colspan:=1).SetWidth(35).Add(New Paragraph(text:="NMFC#").SetFontSize(FontSize.Medium).SetTextAlignment(TextAlignment.CENTER)))
table8.AddCell(New Cell(rowspan:=1, colspan:=1).SetWidth(35).Add(New Paragraph(text:="LTL").SetFontSize(FontSize.Medium).SetTextAlignment(TextAlignment.CENTER)))
document.Add(table8)
Imports iText.IO.Font.Constants
Imports iText.Kernel.Colors
Imports iText.Kernel.Font
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Imports iText.Layout.Properties
Module Program
Sub Main()
Using writer = New PdfWriter("example.pdf")
Dim boldFnt = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD)
Dim italicFnt = PdfFontFactory.CreateFont(StandardFonts.TIMES_ITALIC)
Dim pdf = New PdfDocument(writer)
Dim document = New Document(pdf)
Dim table = New Table({
6.5, 6.5, 6.5, 6.5, 11.5, 5.5, 38.0, 10.7, 8.3
}).
UseAllAvailableWidth().
SetFixedLayout()
table.AddCell(New Cell(1, 9).Add(
New Paragraph("CARRIER INFORMATION").
SetFont(boldFnt).
SetFontSize(7).
SetTextAlignment(TextAlignment.CENTER)).
SetPadding(0).
SetFontColor(ColorConstants.WHITE).
SetBackgroundColor(ColorConstants.BLACK))
table.AddCell(New Cell(1, 2).Add(
New Paragraph("HANDELING UNIT").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(7).
SetTextAlignment(TextAlignment.CENTER)))
table.AddCell(New Cell(1, 2).Add(
New Paragraph("PACKAGE").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)))
table.AddCell(New Cell(2, 1).Add(
New Paragraph("WEIGHT").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)).
SetVerticalAlignment(VerticalAlignment.MIDDLE))
table.AddCell(New Cell(2, 1).Add(
New Paragraph("H.M." & Environment.NewLine & "(X)").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)).
SetVerticalAlignment(VerticalAlignment.MIDDLE))
table.AddCell(New Cell(2, 1).Add(
New Paragraph("COMMODITY DESCRIPTION").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)).Add(
New Paragraph("Commodities requiring special " &
"or additional care " &
"or attention in handling " &
"or stowing must be so marked and packaged " &
"as to ensure safe transportation with ordinary care.").
SetPadding(0).
SetFontSize(5).
SetTextAlignment(TextAlignment.CENTER)).Add(
New Paragraph("See Section 2(e) of NMFC Item 360").
SetPadding(0).
SetFont(italicFnt).
SetFontSize(5).
SetTextAlignment(TextAlignment.CENTER)))
table.AddCell(New Cell(1, 2).Add(
New Paragraph("LTL ONLY").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)))
table.AddCell(New Cell(1, 1).Add(
New Paragraph("QTY").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)))
table.AddCell(New Cell(1, 1).Add(
New Paragraph("TYPE").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)))
table.AddCell(New Cell(1, 1).Add(
New Paragraph("QTY").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)))
table.AddCell(New Cell(1, 1).Add(
New Paragraph("TYPE").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)))
table.AddCell(New Cell(1, 1).Add(
New Paragraph("NMFC #").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)))
table.AddCell(New Cell(1, 1).Add(
New Paragraph("CLASS").
SetPadding(0).
SetFont(boldFnt).
SetFontSize(8).
SetTextAlignment(TextAlignment.CENTER)))
For Each txt In {
"", "SKD", "", "CTN", "", "", "", "", "",
"", "", "", "", "", "", "", "", "",
"", "", "", "", "", ""
}
table.AddCell(New Cell(1, 1).Add(
New Paragraph(txt).
SetPadding(0).SetPaddingLeft(4).
SetFont(italicFnt).
SetFontSize(8)).
SetPadding(0).
SetMinHeight(15))
Next txt
table.AddCell(New Cell(1, 3).Add(
New Paragraph("RECEIVING").
SetPadding(0).SetPaddingRight(4).
SetFont(boldFnt).
SetFontSize(10).
SetFontColor(ColorConstants.LIGHT_GRAY).
SetCharacterSpacing(2).
SetTextAlignment(TextAlignment.RIGHT)).
SetPadding(0))
For Each txt In {
"", "", "", "", "", ""
}
table.AddCell(New Cell(1, 1).Add(
New Paragraph(txt).
SetPadding(0).
SetFont(italicFnt).
SetFontSize(8)).
SetPadding(0).
SetMinHeight(15))
Next txt
table.AddCell(New Cell(1, 3).Add(
New Paragraph("STAMP SPACE").
SetPadding(0).SetPaddingRight(4).
SetFont(boldFnt).
SetFontSize(10).
SetFontColor(ColorConstants.LIGHT_GRAY).
SetCharacterSpacing(2).
SetTextAlignment(TextAlignment.RIGHT)).
SetPadding(0))
For Each prop As (txt As String, clr As Color) In {
("", ColorConstants.WHITE),
("", ColorConstants.WHITE),
("", ColorConstants.WHITE),
("", ColorConstants.WHITE),
("", ColorConstants.WHITE),
("", ColorConstants.WHITE),
("", ColorConstants.WHITE),
("", ColorConstants.WHITE),
("", ColorConstants.WHITE),
("", ColorConstants.WHITE),
("", ColorConstants.GRAY),
("", ColorConstants.WHITE),
("", ColorConstants.GRAY),
("", ColorConstants.WHITE),
("", ColorConstants.GRAY),
("GRAND TOTAL", ColorConstants.WHITE),
("", ColorConstants.GRAY),
("", ColorConstants.GRAY)
}
table.AddCell(New Cell(1, 1).Add(
New Paragraph(prop.txt).
SetPadding(0).
SetFont(boldFnt).
SetFontSize(10).
SetTextAlignment(TextAlignment.CENTER)).
SetPadding(0).
SetMinHeight(15).
SetBackgroundColor(prop.clr))
Next prop
document.Add(table)
document.Close()
End Using
End Sub
End Module