Merge Multiple pdf file into a single pdf
- First create a class with name Pdfmarge.vb and write the following code :
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Collections
Imports System.IO
Imports iTextSharp.text.Rectangle
''' <summary>
''' New Class created by Soumendu for Page Index
''' </summary>
''' <remarks></remarks>
Public Class FileDesc
Private fileName As String
Private startPage As String
Private pageTo As String
Public Property NewFileName() As String
Get
Return fileName
End Get
Set(ByVal value As String)
fileName = value
End Set
End Property
Public Property NumOfPage() As String
Get
Return startPage
End Get
Set(ByVal value As String)
startPage = value
End Set
End Property
Public Property Strt_Page() As String
Get
Return pageTo
End Get
Set(ByVal value As String)
pageTo = value
End Set
End Property
End Class
Public Class MergePDF
#Region "Fields"
Private sourcefolder As String
Private m_destinationfile As String
Private fileList As IList = New ArrayList()
Private _footer As String
Public Property footer() As String
Get
Return _footer
End Get
Set(value As String)
_footer = value
End Set
End Property
Dim fileDetails As New List(Of FileDesc)
Private fl_Details As List(Of FileDesc)
Public Property file_Details() As List(Of FileDesc)
Get
Return fl_Details
End Get
Set(value As List(Of FileDesc))
fl_Details = value
End Set
End Property
#End Region
Public Sub New(_fileList As IList)
Me.fileList = _fileList
End Sub
#Region "Public Methods"
'''
''' Add a new file, together with a given docname to the fileList and namelist collection
'''
Public Sub AddFile(pathnname As String)
fileList.Add(pathnname)
End Sub
'''
''' Generate the merged PDF
'''
Public Sub Execute(ByVal calingFrom As String)
MergeDocs(calingFrom)
End Sub
#End Region
#Region "Private Methods"
'''
''' Merges the Docs and renders the destinationFile
'''
Private Sub MergeDocs(ByVal calingFrom As String)
'Step 1: Create a Docuement-Object
Dim document As New Document(PageSize.A4, 50, 50, 50, 40)
Try
'Step 2: we create a writer that listens to the document
Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(m_destinationfile, FileMode.Create))
Dim obj_event As New ITextEvents
If calingFrom = "C" Then
writer.PageEvent = obj_event
obj_event.FooterText = footer
End If
'Step 3: Open the document
document.Open()
Dim cb As PdfContentByte = writer.DirectContent
Dim page As PdfImportedPage
Dim n As Integer = 0
Dim m As Integer = 0
Dim rotation As Integer = 0
' writer.PageEvent = eventHandler
'Dim footer As Header = New Header(New Phrase(Footer_pdf("A", System.DateTime.Now), FontFactory.GetFont(FontFactory.TIMES_ROMAN, 8, iTextSharp.text.Font.BOLD)), False)
'footer.Border = iTextSharp.text.Rectangle.NO_BORDER
'document.header = footer
'Loops for each file that has been listed
For Each filename As String In fileList
'The current file path
' string filePath = sourcefolder + filename;
' we create a reader for the document
Dim reader As New PdfReader(filename)
'Gets the number of pages to process
n = reader.NumberOfPages
'''''''''''''''''''''''''''''''''''''''''''''Soumendu 21052014 for page index''
Dim fdOb As New FileDesc()
fdOb.NewFileName = filename
fdOb.NumOfPage = n
fdOb.Strt_Page = m
fileDetails.Add(fdOb)
m = m + n
'''''''''''''''''''''''''''''''''''''''''''''
Dim i As Integer = 0
While i < n
i += 1
document.SetPageSize(reader.GetPageSizeWithRotation(1))
document.NewPage()
'Insert to Destination on the first page
If i = 1 Then
Dim fileRef As New Chunk(" ")
fileRef.SetLocalDestination(filename)
document.Add(fileRef)
End If
page = writer.GetImportedPage(reader, i)
rotation = reader.GetPageRotation(i)
If rotation = 90 OrElse rotation = 270 Then
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, _
reader.GetPageSizeWithRotation(i).Height)
Else
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, _
0)
End If
End While
Next
file_Details = fileDetails
Catch e As Exception
Throw e
Finally
document.Close()
End Try
End Sub
#End Region
'''
''' Gets or Sets the DestinationFile
'''
Public Property DestinationFile() As String
Get
Return m_destinationfile
End Get
Set(value As String)
m_destinationfile = value
End Set
End Property
End Class
Public Class ITextEvents
Inherits PdfPageEventHelper
' This is the contentbyte object of the writer
Private cb As PdfContentByte
' we will put the final number of pages in a template
Private headerTemplate As PdfTemplate, footerTemplate As PdfTemplate
' this is the BaseFont we are going to use for the header / footer
Private bf As BaseFont = Nothing
' This keeps track of the creation time
Private PrintTime As DateTime = DateTime.Now
#Region "Fields"
Private _header As String
Private _FooterText As String = ""
#End Region
#Region "Properties"
Public Property Header() As String
Get
Return _header
End Get
Set(value As String)
_header = value
End Set
End Property
Public Property FooterText() As String
Get
Return _FooterText
End Get
Set(value As String)
_FooterText = value
End Set
End Property
#End Region
Public Overrides Sub OnOpenDocument(writer As PdfWriter, document As Document)
Try
PrintTime = DateTime.Now
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
cb = writer.DirectContent
headerTemplate = cb.CreateTemplate(100, 100)
footerTemplate = cb.CreateTemplate(50, 50)
Catch de As DocumentException
Catch ioe As System.IO.IOException
End Try
End Sub
Public Overrides Sub OnEndPage(writer As iTextSharp.text.pdf.PdfWriter, document As iTextSharp.text.Document)
MyBase.OnEndPage(writer, document)
Dim baseFontNormal As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12.0F, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK)
Dim baseFontBig As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12.0F, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK)
'Create PdfTable object
Dim pdfTab As New PdfPTable(3)
Dim text As [String] = "Page " & Convert.ToString(writer.PageNumber) & " of "
'Add paging to footer
If True Then
cb.BeginText()
cb.SetFontAndSize(bf, 9)
cb.SetTextMatrix(document.PageSize.GetLeft(40), document.PageSize.GetBottom(30))
cb.ShowText(FooterText)
'cb.SetTextMatrix(document.PageSize.GetLeft(40), document.PageSize.GetBottom(20))
'cb.NewlineShowText("Deed No I -1901000009 /2014, ARA-I Kolkata")
cb.EndText()
text = "Page " & Convert.ToString(writer.PageNumber) & " of "
cb.BeginText()
cb.SetFontAndSize(bf, 11)
cb.SetTextMatrix(document.PageSize.GetRight(150), document.PageSize.GetBottom(10))
cb.ShowText(text)
cb.EndText()
Dim len As Single = bf.GetWidthPoint(text, 11)
cb.AddTemplate(footerTemplate, document.PageSize.GetRight(150) + len, document.PageSize.GetBottom(10))
End If
' Move the pointer and draw line to separate footer section from rest of page
cb.MoveTo(40, document.PageSize.GetBottom(40))
cb.LineTo(document.PageSize.Width - 30, document.PageSize.GetBottom(40))
cb.Stroke()
End Sub
Public Overrides Sub OnCloseDocument(writer As PdfWriter, document As Document)
MyBase.OnCloseDocument(writer, document)
headerTemplate.BeginText()
headerTemplate.SetFontAndSize(bf, 12)
headerTemplate.SetTextMatrix(0, 0)
headerTemplate.ShowText((writer.PageNumber - 1).ToString())
headerTemplate.EndText()
footerTemplate.BeginText()
footerTemplate.SetFontAndSize(bf, 12)
footerTemplate.SetTextMatrix(0, 0)
footerTemplate.ShowText((writer.PageNumber - 1).ToString())
footerTemplate.EndText()
End Sub
End Class
- Now on calling page write the following code
All_PDF_SAVING_PATH is the local path where all the pdf documents save
ppath is the path where marge pdf will save
Dim fileList As IList = New ArrayList()
Dim fileEntries As String() = Directory.GetFileSystemEntries(All_PDF_SAVING_PATH)
Dim fileNm As String
For Each fileNm In fileEntries
fileList.Add(fileNm)
Next
Dim ppath As String = Marge_pdf & "/" & Scan_id & ".pdf"
Dim PDF As New MergePDF(fileList)
PDF.footer = DateTime.Now.ToShortDateString & " Query No:-" & obj_public_variables.distcode & _
obj_public_variables.rocode & obj_public_variables.qno & " / " & obj_public_variables.qyear & " Deed No :" & _
Query_Details_for_Scan.deed_no.Text & ", "
PDF.DestinationFile = ppath
PDF.Execute("C")