Search code examples
itext7

Itext 7 - Need solution for (pdfdocument.GetPage(4).GetContentBytes())


I am trying pdfDocument.GetPage(4).GetContentBytes() but getting an error object reference not set to instance of an object. itext kernel at kernel extension.get[tkey, tvalue] But If I try pdfDocument.GetPage(5).GetContentBytes() it is working.


Solution

  • In comments you say:

    I am creating a pdf from Json Data , after particular point need to read a particular content in pdf

    so i am using this method pdfDocument.GetPage(4).GetContentBytes() to read it but not able to read the content in page 4

    According to this you attempt to retrieve the content bytes of a page you have recently created in the same PdfDocument instance.

    I assume you are creating the content in your PDF using a Document instance which you have created using either the single argument constructor:

    /// <summary>
    /// Creates a document from a <see cref="iText.Kernel.Pdf.PdfDocument"/>.
    /// </summary>
    /// <remarks>
    /// Creates a document from a <see cref="iText.Kernel.Pdf.PdfDocument"/>.
    /// Initializes the first page with the <see cref="iText.Kernel.Pdf.PdfDocument"/>'s
    /// current default <see cref="iText.Kernel.Geom.PageSize"/>.
    /// </remarks>
    /// <param name="pdfDoc">the in-memory representation of the PDF document</param>
    public Document(PdfDocument pdfDoc)
    

    or the double argument constructor:

    /// <summary>
    /// Creates a document from a <see cref="iText.Kernel.Pdf.PdfDocument"/>
    /// with a manually set <see cref="iText.Kernel.Geom.PageSize"/>.
    /// </summary>
    /// <param name="pdfDoc">the in-memory representation of the PDF document</param>
    /// <param name="pageSize">the page size</param>
    public Document(PdfDocument pdfDoc, PageSize pageSize)
    

    Unfortunately for your use case both these constructors activate an optimization in the Document code which flushes the page data from memory to the target output stream as soon as possible after filling the next page has been started. (This optimization allows creating large PDFs with iText 7 while keeping the memory requirements small.)

    In your case iText appears to already have flushed page 4 (resulting in your error) but not yet page 5 (so its content still could be retrieved).

    If you need to access pages long after having started putting content onto the following page, you should instead use the triple argument Document constructor

    /// <summary>
    /// Creates a document from a <see cref="iText.Kernel.Pdf.PdfDocument"/>
    /// with a manually set <see cref="iText.Kernel.Geom.PageSize"/>.
    /// </summary>
    /// <param name="pdfDoc">the in-memory representation of the PDF document</param>
    /// <param name="pageSize">the page size</param>
    /// <param name="immediateFlush">
    /// if true, write pages and page-related instructions to the
    /// <see cref="iText.Kernel.Pdf.PdfDocument"/> as soon as possible.
    /// </param>
    public Document(PdfDocument pdfDoc, PageSize pageSize, bool immediateFlush)
    

    with an immediateFlush value of false.