我有同樣的問題,但由下面的代碼解決,問題是由PDF兼容性。
PdfSharp.Pdf.IO.PdfReader.Open(ReturnCompatiblePdf("PDF FILE PATH"), PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import)
Private Function ReturnCompatiblePdf(ByVal sFilename As String) As MemoryStream
Dim reader As New iTextSharp.text.pdf.PdfReader(sFilename)
Dim output_stream As New MemoryStream
' we retrieve the total number of pages
Dim n As Integer = reader.NumberOfPages
' step 1: creation of a document-object
Dim document As New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
' step 2: we create a writer that listens to the document
Dim writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, output_stream)
'write pdf that pdfsharp can understand
writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4)
' step 3: we open the document
document.Open()
Dim cb As iTextSharp.text.pdf.PdfContentByte = writer.DirectContent
Dim page As iTextSharp.text.pdf.PdfImportedPage
Dim rotation As Integer
Dim i As Integer = 0
While i < n
i += 1
document.SetPageSize(reader.GetPageSizeWithRotation(i))
document.NewPage()
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
'---- Keep the stream open!
writer.CloseStream = False
' step 5: we close the document
document.Close()
Return output_stream
End Function
我沒有看到任何* *代碼中的錯誤檢查或錯誤處理。這就是發生的事情。 –
可能是因爲輸入PDF已損壞,即頁面字典缺少相應的'NumKids'條目。您可以嘗試轉儲元數據並檢查,如果您的庫支持。 – dirkgently
錯誤在用戶看到之前被捕獲。 – Anderson