我正在使用 iTextSharp 将 PDF 文档合并在一起。我的问题是我正在尝试合并包含书签的大型 PDF。我当前的功能是使用 PdfWriter 合并文档。我知道 PdfStamper 可以工作,但我不知道如何更改该功能以使其正常工作。
当我在下面的示例中将 PdfWriter 更改为 PdfStamper 时,出现错误。
代码示例:
writer = PdfStamper.GetInstance(pdfDoc, New FileStream(outputPath, FileMode.OpenOrCreate))
错误信息:
“GetInstance”不是“iTextSharp.text.pdf.PdfStamper”的成员
这是整个函数:
Public Shared Function MergePdfFiles(ByVal pdfFiles() As String, ByVal outputPath As String) As Boolean
Dim result As Boolean = False
Dim pdfCount As Integer = 0
Dim f As Integer = 0
Dim fName As String
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim pageCount As Integer = 0
Dim pdfDoc As iTextSharp.text.Document = Nothing
Dim writer As PdfWriter = Nothing
Dim cb As PdfContentByte = Nothing
Dim page As PdfImportedPage = Nothing
Dim rotation As Integer = 0
Try
pdfCount = pdfFiles.Length
If pdfCount > 1 Then
fName = pdfFiles(f)
reader = New iTextSharp.text.pdf.PdfReader(fName)
pageCount = reader.NumberOfPages
pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)
writer = PdfWriter.GetInstance(pdfDoc, New FileStream(outputPath, FileMode.OpenOrCreate))
With pdfDoc
.Open()
End With
cb = writer.DirectContent
While f < pdfCount
Dim i As Integer = 0
While i < pageCount
i += 1
pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i))
pdfDoc.NewPage()
page = writer.GetImportedPage(reader, i)
rotation = reader.GetPageRotation(i)
If rotation = 90 Then
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height)
ElseIf rotation = 270 Then
cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30)
Else
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
End If
End While
f += 1
If f < pdfCount Then
fName = pdfFiles(f)
reader = New iTextSharp.text.pdf.PdfReader(fName)
pageCount = reader.NumberOfPages
End If
End While
pdfDoc.Close()
result = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
End Try
Return result
End Function