Imports iTextSharp.text.pdf
Imports iTextSharp.text
''' iTextSharp ver = 5.5.6.0
'''-------------------------------------
Public Sub PdfCopyAddFooterAndMergePdf()
Imports iTextSharp.text
''' iTextSharp ver = 5.5.6.0
'''-------------------------------------
Public Sub PdfCopyAddFooterAndMergePdf()
Dim
PdfFilePath As String = "C:\temp\test.pdf"
Dim
outputPath As String = "C:\temp\merge.pdf"
Dim reader As PdfReader
= Nothing
Dim pdfDoc As Document
= Nothing
Dim pdfCpy As PdfCopy
= Nothing
Dim page As PdfImportedPage
= Nothing
Try
'Read input pdf file
reader = New PdfReader(PdfFilePath)
'Create new document and set the size as input pdf
size
pdfDoc = New Document(reader.GetPageSizeWithRotation(1))
'Create output new pdf
pdfCpy = New PdfCopy(pdfDoc,
New
FileStream(outputPath, FileMode.Create))
pdfDoc.Open()
Dim
pageNumbers As New List(Of Integer)(New Integer() {2, 4, 6, 8})
For Each pageNum As Integer
In
pageNumbers
page =
pdfCpy.GetImportedPage(reader, pageNum)
Dim stamp As PdfCopy.PageStamp
= pdfCpy.CreatePageStamp(page)
Dim uc As PdfContentByte
= stamp.GetUnderContent
Dim
footerTable As New PdfPTable(1)
footerTable.TotalWidth = pdfDoc.PageSize.Width
'Create new Base Font for japanese text display
Dim fontPath As String
= System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), "fonts", "msgothic.ttc")
Dim
footerFont As New Font(BaseFont.CreateFont(fontPath & ",0",
BaseFont.IDENTITY_H, True), 10)
Dim myFooter As New Chunk("Your footer", footerFont)
Dim footer As New PdfPCell(New Phrase(myFooter))
footer.Border = Rectangle.NO_BORDER
footer.HorizontalAlignment = Element.ALIGN_RIGHT
footerTable.AddCell(footer)
footerTable.WriteSelectedRows(0, -1, -120, (pdfDoc.BottomMargin + 51),
uc)
'Accept changes
stamp.AlterContents()
pdfCpy.AddPage(page)
Next
pdfDoc.Close()
reader.Close()
pdfCpy.Close()
Catch ex As Exception
Throw ex
Finally
reader = Nothing
pdfDoc = Nothing
pdfCpy = Nothing
page = Nothing
End Try
End
Sub