简体   繁体   中英

iTextSharp "The document has no pages." error

I'm creating a pdf file with an image. I'm getting the image, first saving it into the server, after creating an iTextSharp image with it;

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

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

After that the error becomes to this:

System.ObjectDisposedException was caught 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 ? If Url path is missing content (not opening) will cause such issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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