iTextSharp“文档没有页面。”错误

时间:2015-01-20 11:59:34

标签: c# asp.net itextsharp

我正在创建带有图片的pdf文件。在使用它创建iTextSharp图像之后,我首先将图像保存到服务器中;

iTextSharp.text.Image backgroundImage = iTextSharp.text.Image.GetInstance(path);

在这一行,我收到错误"文档没有页面。"

这是StackTrace:

   location: iTextSharp.text.pdf.PdfPages.WritePageTree()
   location: iTextSharp.text.pdf.PdfWriter.Close()
   location: iTextSharp.text.pdf.PdfDocument.Close()
   location: iTextSharp.text.pdf.PdfWriter.Close()
   location: iTextSharp.text.DocWriter.Dispose()
   location: MyProject.Helpers.FileUploadHelper.SaveMarathonCertificateTemplate(HttpRequestBase Request, String _fileName, CertificateOrientation orientation) c:\MyProject\Helpers\FileUploadHelper.cs : line 68
   location: MyProject.Controllers.CertificateController.Add(Int32 marathonId, MarathonCertificate marathonCertificate) c:\MyProject\Controllers\CertificateController.cs: line 74

昨天代码运行良好,但奇怪的是今天我收到了这个错误。这是我的代码:

using (var fs = new FileStream(pdfFileName, FileMode.Create))
{
    using (var pdfDoc = new iTextSharp.text.Document())
    {
        if (orientation == CertificateOrientation.HORIZONTAL)
            pdfDoc.SetPageSize(PageSize.A4.Rotate());
        using (var w = PdfWriter.GetInstance(pdfDoc, fs))
        {
            pdfDoc.Open();
            pdfDoc.NewPage(); // add Page here

            iTextSharp.text.Image backgroundImage = iTextSharp.text.Image.GetInstance(path);

            if (orientation == CertificateOrientation.HORIZONTAL)
            {
                backgroundImage.ScaleAbsoluteWidth(Config.PdfActualSizeHorizontal[0]);
                backgroundImage.ScaleAbsoluteHeight(Config.PdfActualSizeHorizontal[1]);
            }
            else if (orientation == CertificateOrientation.VERTICAL)
            {
                backgroundImage.ScaleAbsoluteWidth(Config.PdfActualSizeVertical[0]);
                backgroundImage.ScaleAbsoluteHeight(Config.PdfActualSizeVertical[1]);
            }
            backgroundImage.SetAbsolutePosition(0, 0);
            pdfDoc.Add(backgroundImage);

            pdfDoc.Close();
        }
    }
}

我无法解决问题。有没有解决方案?

  

编辑:

我在获取Image实例

之前添加了一行
pdfDoc.Add(new Paragraph(" "));

之后错误就变成了:

  

System.ObjectDisposedException被捕获Message =无法访问   关闭文件。

New StackTrace:

location: System.IO.__Error.FileNotOpen()
location: System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
location: iTextSharp.text.pdf.OutputStreamCounter.Write(Byte[] buffer, Int32 offset, Int32 count)
location: iTextSharp.text.pdf.PdfIndirectObject.WriteTo(Stream os)
location: iTextSharp.text.pdf.PdfWriter.PdfBody.Write(PdfIndirectObject indirect, Int32 refNumber, Int32 generation)
location: iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta, Int32 refNumber, Int32 generation, Boolean inObjStm)
location: iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta, PdfIndirectReference refa, Boolean inObjStm)
location: iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta, PdfIndirectReference refa)
location: iTextSharp.text.pdf.PdfWriter.AddToBody(PdfObject objecta, PdfIndirectReference refa)
location: iTextSharp.text.pdf.Type1Font.WriteFont(PdfWriter writer, PdfIndirectReference piref, Object[] parms)
location: iTextSharp.text.pdf.FontDetails.WriteFont(PdfWriter writer)
location: iTextSharp.text.pdf.PdfWriter.AddSharedObjectsToBody()
location: iTextSharp.text.pdf.PdfWriter.Close()
location: iTextSharp.text.DocWriter.Dispose()
location: MyProject.Helpers.FileUploadHelper.SaveMarathonCertificateTemplate(HttpRequestBase Request, String _fileName, CertificateOrientation orientation) c:\MyProject\Helpers\FileUploadHelper.cs: line 70
location: MyProject.Controllers.CertificateController.Add(Int32 marathonId, MarathonCertificate marathonCertificate) c:\MyProject\Controllers\CertificateController.cs: line 74

2 个答案:

答案 0 :(得分:0)

试试这个:

[-name "*.avi"] & [&& find . -name '*.avi' -exec rm -r     {} \;]

多数人。

答案 1 :(得分:0)

  

请验证 HTML 是否包含图片或资源    URL 内容不存在的文件?如果网址路径丢失   内容(未打开)将导致此类问题。