简体   繁体   English

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

[英]iTextSharp "The document has no pages." error

I'm creating a pdf file with an image.我正在创建一个带有图像的 pdf 文件。 I'm getting the image, first saving it into the server, after creating an iTextSharp image with it;我正在获取图像,首先将其保存到服务器中,然后使用它创建 iTextSharp 图像;

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

On this line I'm getting an error "The document has no pages."在这一行我收到一个错误“文档没有页面”。

Here is the 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

Yesterday code was working well, but weirdly today I'm getting this error.昨天的代码运行良好,但奇怪的是今天我收到了这个错误。 Here is my code:这是我的代码:

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();
        }
    }
}

I cant't get the problem.我无法解决问题。 Is there any solution?有什么解决办法吗?

EDIT:编辑:

I added a line before getting Image instance我在获取 Image 实例之前添加了一行

pdfDoc.Add(new Paragraph(" "));

After that the error becomes to this:之后,错误变为:

System.ObjectDisposedException was caught Message=Cannot access a closed file. System.ObjectDisposedException 被捕获 Message=Cannot access a closed file。

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

Try this one:试试这个:

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();

Thats all.就这样。

Please verify the HTML does it contains the Images or Resource files whose URL content does't exist ?请验证HTML是否包含URL内容不存在的图像或资源文件? If Url path is missing content (not opening) will cause such issue.如果 URL 路径缺少内容(未打开)将导致此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM