How to solve the Document Has No Pages IOException in iText in java


In this core java tutorial we will learn How to solve the Document Has No Pages IOException in java using iText library - core java tutorial with program and examples.

ExceptionConverter: java.io.IOException: The document has no pages ,
CAUSES > Exception occurs when you have added nothing in the document.
SOLUTION > you simply need to add something into the document, in below program just uncomment the commented line (//document.add(paragraph1);) to avoid this exception.


How to create Bar Chart In Pdf in java - iText java tutorial

Create Pie Chart In Pdf in java using itext - iText java tutorial

How to Modify - Add Text To Existing PDF in java - iText java tutorial example



Download jars required to execute program >

Or, you may download jars from here.

Program/Example How to solve the Document Has No Pages IOException in java - Using iText library - core java tutorial

package i8_exception;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* How to solve the Document Has No Pages IOException in
*  iText library - core java tutorial
*
*/
public class DocumentHasNoPagesExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "e:/DocumentHasNoPages Example.pdf";
                
                 OutputStream fos = new FileOutputStream(new File(pdfFilePath));
                 Document document = new Document();
                 PdfWriter.getInstance(document, fos);
                 document.open();
                
                 Paragraph paragraph1 = new Paragraph("This is SettingPdfAttributesExample.pdf");
                
                 //Uncomment it to avoid/remove > The document has no pages : java.io.IOException
                 //document.add(paragraph1);
                
                
                 document.close();             
                 fos.close();
                
                 System.out.println("PDF created in >> "+ pdfFilePath);
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/* OUTPUT of above program/Example -
ExceptionConverter: java.io.IOException: The document has no pages.
   at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:113)
   at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1269)
   at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:889)
   at com.itextpdf.text.Document.close(Document.java:416)
   at i8_exception.DocumentHasNoPagesExample.main(DocumentHasNoPagesExample.java:34)
*/




Summary -
So in this core java tutorial we solved the Document Has No Pages IOException in java.

how to Create Paragraph In Pdf - iText java tutorial program

Create Left, center and right aligned Paragraph In Pdf - iText java tutorial

Create Phrase In Pdf (with lead) Using Itext - iText java tutorial example




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java

Creating IMAGES in pdf in java - iText tutorial >

Adding the Images In Pdf in java example using iText - iText java tutorial

How to Add And Rotate Images In Pdf in java - iText java tutorial example

How can you Add the Images from URL In Pdf in java program





Adding WATERMARK in pdf in java - iText tutorial >

How to add TEXT as Watermark In Pdf in java - iText java tutorial

How to add IMAGE as Watermark In Pdf in iText in java example



Creating CHAPTER, SECTION, CHUNK, PARAGRAPH AND PHRASE in pdf in java - iText tutorial >

Create Chapter And Section In Pdf in java - iText java tutorial

Create Chunk In Pdf in java - iText java tutorial program

how to Create Paragraph In Pdf - iText java tutorial program

Create Left, center and right aligned Paragraph In Pdf - iText java tutorial

Create Phrase In Pdf (with lead) Using Itext - iText java tutorial example



Set FONT NAME, SIZE, STYLE, COLOUR in pdf in java - iText tutorial >

How to Set the Font Name, Size, Style and Colour In Pdf using itext in java


Create ANCHOR (HYPERLINK), SUPERSCRIPT, SUBSCRIPT, UNDERLINE AND STRIKETHROUGH in pdf in java - iText tutorial >

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

How To Set SuperScript And SubScript In Pdf in java - iText java tutorial

UnderLine Text In Pdf in java - iText java tutorial example

Strikethrough Text In Pdf in java - iText java tutorial program


HTML to PDF in java - iText tutorial >

Convert Html To Pdf in java using iText - iText java tutorial


Create new page, set page height and width in java - iText tutorial >

How To Create New Pages In Pdf Using Itext - iText java tutorial example

How to Find Pdf Page Height And Width in java - iText java tutorial program



HEADER and FOOTER in pdf in java - iText tutorial >

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial



BAR AND PIE CHARTS in pdf in java - iText tutorial >

How to create Bar Chart In Pdf in java - iText java tutorial

Create Pie Chart In Pdf in java using itext - iText java tutorial



MODIFY/ EDIT pdf in java - iText tutorial >

How to Modify - Add Text To Existing PDF in java - iText java tutorial example

How To Read And Add Image To Existing Pdf in java Example - iText java tutorial



Solve common Exceptions in itext in java - iText tutorial >

How to solve the Document Has No Pages IOException in iText in java


Alternates to itext in java - iText tutorial >

What are alternatives to iText library for creating Pdf in java?




eEdit
Must read for you :