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

问题描述 投票:1回答:2

我正在创建一个带有图像的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消息=无法访问已关闭的文件。

新的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
c# asp.net itextsharp
2个回答
0
投票

试试这个:

var path ="path of final pdf to save";
var imagePath="path of image that should be paste in final pdf file";
iTextSharp.text.Document document = new iTextSharp.text.Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance(imagePath);
myImage.ScaleAbsoluteHeight(document.PageSize.Height);
myImage.ScaleAbsoluteWidth(document.PageSize.Width);
myImage.Alignment = Element.ALIGN_CENTER;
document.Add(myImage);
document.Close();

就这样。


0
投票

请验证HTML是否包含其URL内容不存在的图像或资源文件?如果Url路径缺少内容(未打开)将导致此类问题。

© www.soinside.com 2019 - 2024. All rights reserved.