我设法根据VB.net并使用iText v7 api在这里和那里放了一些代码。但是,我在尝试将拆分后的文档重新获取为字节数组时遇到问题。非常感谢您的帮助。谢谢。对不起,我承认我是菜鸟,对编码还不熟悉。
Private Sub SplitSub()
Dim pdfcontents As New List(Of Byte())
Dim reader As iText.Kernel.Pdf.PdfReader = New iText.Kernel.Pdf.PdfReader("C:\Users\XXX\Desktop\Test\Test.pdf")
Dim pdfDoc As iText.Kernel.Pdf.PdfDocument = New iText.Kernel.Pdf.PdfDocument(reader)
Dim splitter As ByteArrayPdfSplitter = New ByteArrayPdfSplitter(pdfDoc)
Dim splitDocuments As List(Of iText.Kernel.Pdf.PdfDocument) = splitter.SplitBySize(5000000)
For Each splitDoc In splitDocuments
Dim bytecontents as Byte()
'??? How do I retrieve the pdfDocuments as bytes()???
pdfcontents.Add(bytecontents)
Next
pdfDoc.Close()
reader.Close()
End Sub
Class ByteArrayPdfSplitter
Inherits iText.Kernel.Utils.PdfSplitter
Private currentOutputStream As MemoryStream
Public Sub New(ByVal pdfDocument As iText.Kernel.Pdf.PdfDocument)
MyBase.New(pdfDocument)
End Sub
Protected Overrides Function GetNextPdfWriter(ByVal documentPageRange As iText.Kernel.Utils.PageRange) As iText.Kernel.Pdf.PdfWriter
currentOutputStream = New MemoryStream()
Return New iText.Kernel.Pdf.PdfWriter(currentOutputStream)
End Function
Public ReadOnly Property CurrentMemoryStream As MemoryStream
Get
Return currentOutputStream
End Get
End Property
Public Class DocumentReadyListener
Implements iText.Kernel.Utils.PdfSplitter.IDocumentReadyListener
Private splitter As ByteArrayPdfSplitter
Public Sub New(ByVal splitter As ByteArrayPdfSplitter)
Me.splitter = splitter
End Sub
Public Sub DocumentReady(ByVal pdfDocument As iText.Kernel.Pdf.PdfDocument, ByVal pageRange As iText.Kernel.Utils.PageRange) Implements iText.Kernel.Utils.PdfSplitter.IDocumentReadyListener.DocumentReady
pdfDocument.Close()
Dim contents As Byte() = splitter.CurrentMemoryStream.ToArray()
End Sub
End Class