有一些奇怪的事情在你的代碼怎麼回事。
您創建了一個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,你可以簡單的增加一個希伯來文的ColumnText
這樣的:
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()
爲什麼會多花的代碼17個晦澀的線條時可以達到7個系相同的結果是清澈的?
非常感謝布魯諾。使用SetSimpleColumn硬編碼值 - 由於pdf大小是動態的,結果總是相同的?新內容的位置是否不受影響? – nkotb
顯然你必須修改硬編碼的值。目前,代碼假設'(0,0)'([座標系的原點](http://developers.itextpdf.com/question/where-origin-xy-pdf-page))是座標左下角,並且每個頁面的寬度是A4頁面的寬度。如果頁面大小不可預知,您將需要[獲取每個頁面的頁面尺寸](http://developers.itextpdf.com/question/how-get-pdf-page-width-and-height)。請參閱[如何相對於頁面定位文本?](http://developers.itextpdf.com/question/how-position-text-relative-page) –
我正在嘗試修復此位置,以便新文本將位於頁面的底部,但我仍然不明白在SetSimpleColumn中設置值的位置,以便文本顯示在頁面的底部。我看着你的鏈接。 – nkotb