如何对齐itext columntext并使宽度为100%

如何对齐itext columntext并使宽度为100%,itext,hebrew,Itext,Hebrew,我需要将最后一页底部的希伯来文文本添加到现有的PDF中。我设法做到了,但是,我需要文本在中心对齐。pdftable只有一列。我还希望列的宽度为100% 这是我的代码: Dim LastPage As Integer = pdfRd.NumberOfPages 'The page number Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing Dim ct = New ColumnText(cb) Dim rows As Single()

我需要将最后一页底部的希伯来文文本添加到现有的PDF中。我设法做到了,但是,我需要文本在中心对齐。pdftable只有一列。我还希望列的宽度为100%

这是我的代码:

Dim LastPage As Integer = pdfRd.NumberOfPages 'The page number
Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
Dim ct = New ColumnText(cb)
Dim rows As Single() = {500} 'create column sizes 
Dim nTbl As PdfPTable = New PdfPTable(1) 'number of columns in our case 1
Dim cellFont As iTextSharp.text.Font = New iTextSharp.text.Font(BaseFont, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.RED)

nTbl.RunDirection = PdfWriter.RUN_DIRECTION_RTL

nTbl.WidthPercentage = 100 'Table size is set to 100% of the page
nTbl.LockedWidth = True   
nTbl.SetTotalWidth(rows) ' Set the column widths on table  

nTbl.DefaultCell.Border = 0
nTbl.AddCell(New iTextSharp.text.Phrase(MsgOnClosing, cellFont))
nTbl.SetExtendLastRow(1, 1)
' nTbl.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT 'align table
ct.Alignment = iTextSharp.text.Element.ALIGN_CENTER
'ct.SetSimpleColumn(70, 36, iTextSharp.text.PageSize.A4.Width - 36, iTextSharp.text.PageSize.A4.Height - 300)

nTbl.WriteSelectedRows(0, -1, 0, 20, stamp.GetOverContent(LastPage)) 'coords x=300,y=300                    
ct.Go()

您的代码中出现了一些奇怪的情况

您创建了一个
ColumnText
对象,但没有对它做任何操作。删除对
ct
的所有引用,您的代码将生成与以前完全相同的PDF:

Dim LastPage As Integer = pdfRd.NumberOfPages 'The page number
Dim rows As Single() = {500} 'create column sizes 
Dim nTbl As PdfPTable = New PdfPTable(1) 'number of columns in our case 1
Dim cellFont As iTextSharp.text.Font = New iTextSharp.text.Font(BaseFont, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.RED)
nTbl.RunDirection = PdfWriter.RUN_DIRECTION_RTL
nTbl.WidthPercentage = 100 'Table size is set to 100% of the page
nTbl.LockedWidth = True   
nTbl.SetTotalWidth(rows) ' Set the column widths on table  
nTbl.DefaultCell.Border = 0
nTbl.AddCell(New iTextSharp.text.Phrase(MsgOnClosing, cellFont))
nTbl.SetExtendLastRow(1, 1)
nTbl.WriteSelectedRows(0, -1, 0, 20, stamp.GetOverContent(LastPage)) 'coords x=300,y=300
如果我看一下列出的代码,我会看到一些更奇怪的事情:

nTbl.WidthPercentage = 100 'Table size is set to 100% of the page
nTbl.LockedWidth = True   
nTbl.SetTotalWidth(rows) ' Set the column widths on table 
这没有道理。
PdfPTable
的宽度可以表示为百分比(使用
WidthPercentage
),也可以表示为绝对宽度(使用
setTotalWidth
)。
LockedWidth
的值将决定是使用百分比(
false
),还是使用绝对宽度(
true
)。在您的情况下,您选择使用绝对宽度,因此设置宽度百分比没有意义

这也没有意义,因为您正在使用
WriteSelectedRows()
方法添加表。使用
WriteSelectedRows
时,必须使用绝对宽度。在你的情况下,我会这样定义宽度:

nTbl.SetTotalWidth(iTextSharp.text.PageSize.A4.Width - 72)
Dim LastPage As Integer = pdfRd.NumberOfPages 'The page number
Dim ct = New ColumnText(stamp.GetOverContent(LastPage))
ct.SetSimpleColumn(36, 0, iTextSharp.text.PageSize.A4.Width - 72, 36)
ct.RunDirection = PdfWriter.RUN_DIRECTION_RTL
Dim myFont As iTextSharp.text.Font = New iTextSharp.text.Font(BaseFont, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.RED)
ct.AddElement(New iTextSharp.text.Paragraph(MsgOnClosing, cellFont))
ct.Go()
但要确保坐标正确:

nTbl.WriteSelectedRows(0, -1, 36, 20, stamp.GetOverContent(LastPage))
您还应该扔掉定义
变量的行。为什么要添加
nTbl.setextendedlastrow(1,1)
?这与
WriteSelectedRows()
方法结合使用应该不会产生任何影响

备选方案#1:

还有另一种方法可以实现您想要的,您确实可以使用
ColumnText
,但在这种情况下,您必须将
PdfPTable
添加到列中。请注意,您对变量
cb
所做的操作也没有意义,我已经扔掉了这一行:

Dim LastPage As Integer = pdfRd.NumberOfPages 'The page number
Dim ct = New ColumnText(stamp.GetOverContent(LastPage))
Dim nTbl As PdfPTable = New PdfPTable(1) 'number of columns in our case 1
Dim cellFont As iTextSharp.text.Font = New iTextSharp.text.Font(BaseFont, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.RED)
nTbl.RunDirection = PdfWriter.RUN_DIRECTION_RTL
nTbl.WidthPercentage = 100 'Table size is set to 100% of the page
nTbl.DefaultCell.Border = 0
nTbl.AddCell(New iTextSharp.text.Phrase(MsgOnClosing, cellFont))
ct.SetSimpleColumn(36, 0, iTextSharp.text.PageSize.A4.Width - 72, 36)
ct.Go()
再一次,我不得不扔掉一些台词。例如:如果将表的宽度设置为100%,为什么需要设置列的对齐方式

备选方案2:

代码的主要问题是它很复杂。如果只想添加一些希伯来文,为什么要使用
PdfPtable

如果您使用iText 7和pdfCalligraph附加组件而不是旧的iText 5,则正确的方向会出现。但是,如果您坚持使用iText 5,您可以简单地将希伯来文添加到
ColumnText
中,如下所示:

nTbl.SetTotalWidth(iTextSharp.text.PageSize.A4.Width - 72)
Dim LastPage As Integer = pdfRd.NumberOfPages 'The page number
Dim ct = New ColumnText(stamp.GetOverContent(LastPage))
ct.SetSimpleColumn(36, 0, iTextSharp.text.PageSize.A4.Width - 72, 36)
ct.RunDirection = PdfWriter.RUN_DIRECTION_RTL
Dim myFont As iTextSharp.text.Font = New iTextSharp.text.Font(BaseFont, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.RED)
ct.AddElement(New iTextSharp.text.Paragraph(MsgOnClosing, cellFont))
ct.Go()

如果您可以在7行非常清晰的代码中获得相同的结果,为什么还要花费17行晦涩难懂的代码?

非常感谢Bruno。使用SetSimpleColumn硬编码值-由于pdf大小是动态的,结果是否始终相同?新内容的位置不会受到影响吗?显然,您必须调整硬编码的值。目前,代码假定
(0,0)
()是左下角的坐标,并且每页的宽度是A4页的宽度。如果页面大小不可预测,则需要更改。请看,我正在尝试固定位置,以便新文本将位于页面底部,但我仍然不明白在SetSimpleColumn中,在何处设置值,以便文本将显示在页面底部。我看了你的链接。