【问题标题】:PDFSharp seems to be failing to open certain PDF documentsPDFSharp 似乎无法打开某些 PDF 文档
【发布时间】:2012-05-26 09:37:18
【问题描述】:

我有一个程序可以为提交到网站的 PDF 添加第二页。我使用 C# 和 PDFSharp。大多数文档工作正常,但少数用户收到“对象引用未设置为对象实例”。

using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing.Layout;

PdfDocument rosterInput = PdfReader.Open(FilePath, PdfDocumentOpenMode.Import);

PdfPage rpage = rosterInput.Pages[0];

错误发生在第二行。当我调试时,它显示 PageCount = 0,这很奇怪,因为它是一个 1 页文档。

【问题讨论】:

  • 我没有在您的代码中看到 任何 错误检查或错误处理。就是这样。
  • 可能是输入 PDF 已损坏,即页面字典缺少适当的 NumKids 条目。您可以尝试转储元数据并检查您的库是否支持。
  • 错误在用户看到之前就被捕获了。
  • 也许您无法加载的文件是 PDF 格式,但尚不支持。看看这个链接:pdfsharp.net/wiki/….
  • PDF 1.3 应该没有问题 - 如果文件严格遵循 Adob​​e PDF 参考。但许多第三方 PDF 生成器不遵循 PDF 参考。 PDFsharp 团队正在尝试使 PDFsharp 与不完全符合参考的文件兼容,但他们需要示例文件。

标签: asp.net .net pdf pdfsharp


【解决方案1】:

非常感谢你拯救了我的一天!我对改进解决方案的唯一建议是在这样的 using 块上使用内存流:

  Using memoryStream As MemoryStream = ReturnCompatiblePdf(File.FullName)
      Dim DocPdf As PdfDocument = PdfReader.Open(memoryStream, PdfDocumentOpenMode.Import)
      //Your code here.....
  End Using

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,但通过以下代码解决了,问题在于 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-30
      • 2013-02-18
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多