我需要将最后一页底部的希伯来语文本添加到现有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()
答案 0 :(得分:1)
您的代码中发生了一些奇怪的事情。
您创建了一个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)
但请确保您的坐标正确:
nTbl.WriteSelectedRows(0, -1, 36, 20, stamp.GetOverContent(LastPage))
您还应该丢弃定义rows
变量的行。为什么要添加nTbl.SetExtendLastRow(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,你可以简单地将希伯来文添加到{{{ 1}}像这样:
ColumnText
为什么你会在7条清晰的线条中获得相同的结果时,会花费17条不起眼的代码?