Here is a 2008 VB.Net example of using ITextSharp PDFCopy to copy multiple PDF files into 1 multi-page PDF file. This will copy everything except underlying links. It appears to copy all annotations perfectly, at least I could not find any it did not copy.
Note: You must have ITextSharp referenced in your project.
Input Parameters:
fileArray – an array of pdf files.
outPutPDF – full path and name to output multipage PDF document.
Private Sub BuildMultiPagePDF(ByVal fileArray As String(), ByVal outPutPDF As String)
Try
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim pageCount As Integer = 0
Dim currentPage As Integer = 0
Dim pdfDoc As iTextSharp.text.Document = Nothing
Dim writer As iTextSharp.text.pdf.PdfCopy = Nothing
Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing
Dim currentPDF As Integer = 0
If fileArray.Length > 0 Then
reader = New iTextSharp.text.pdf.PdfReader(fileArray(currentPDF))
pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
writer = New iTextSharp.text.pdf.PdfCopy(pdfDoc, _
New IO.FileStream(outPutPDF, _
IO.FileMode.OpenOrCreate, _
IO.FileAccess.Write))
pageCount = reader.NumberOfPages
While currentPDF < fileArray.Length
pdfDoc.Open()
While currentPage < pageCount
currentPage += 1
pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(currentPage))
pdfDoc.NewPage()
page = writer.GetImportedPage(reader, currentPage)
writer.AddPage(page)
End While
currentPDF += 1
If currentPDF < fileArray.Length Then
reader = New iTextSharp.text.pdf.PdfReader(fileArray(currentPDF))
pageCount = reader.NumberOfPages
currentPage = 0
End If
End While
pdfDoc.Close()
Else
MessageBox.Show("The input file array is empty. Processing terminated.", _
"INVALID FILE LIST", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
MessageBox.Show(ex.message)
End Try
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…