The page 1 was requested but the document has only 0 pages.

When using iText to generate pdf files, encountering this error, I haven't found a solution to it for a long time. There is no excuse for putting our solution here.

First paste the code:

Document document = new Document(PageSize.A4,50,50,50,50);// Create a Document object
        try {
            PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        document.open();//Open Write Stream
        Paragraph title=new Paragraph(departmentEntity.getName()+"Learning Record Report——"+CalendarTool.formatYYYYMMdd(new Date()),titlefont );
        title.setAlignment(Element.ALIGN_CENTER);
        title.setSpacingAfter(20);
        Chapter chapter=new Chapter(title,1);
        chapter.setNumberDepth(0);
        Section section=chapter.addSection(new Paragraph("Learning Records of Unit Personnel",titlefont));
        Paragraph tableDesc=new Paragraph("Unit ownership"+studyRecordList.size()+"people");
        PdfPTable table=new PdfPTable(7);
        table.setSpacingBefore(25);
        table.setSpacingAfter(25);
        int order=0;
        //Create a table header first
        table.addCell(createCell("Serial number",keyfont));
        table.addCell(createCell("User number",keyfont));
        table.addCell(createCell("User name",keyfont));
        table.addCell(createCell("Weekly learning times",keyfont));
        table.addCell(createCell("Weekly completion rate",keyfont));
        table.addCell(createCell("Monthly learning times",keyfont));
        table.addCell(createCell("Monthly completion rate",keyfont));
        for (DepartmentUserStudyRecord studyRecord:studyRecordList){
            order++;//Add serial number
            table.addCell(createCell(order+"",textfont));
            table.addCell(createCell(studyRecord.getAdmId(),textfont));
            table.addCell(createCell(studyRecord.getUserName(),textfont));
            table.addCell(createCell(studyRecord.getWeeklyStudyNum()+"",textfont));
            table.addCell(createCell(studyRecord.getWeeklyStudyRate()*100+"%",textfont));
            table.addCell(createCell(studyRecord.getMonthlyStudyNum()+"",textfont));
            table.addCell(createCell(studyRecord.getMonthlyStudyRate()*100+"%",textfont));
        }
        section.add(table);


        Section section1=chapter.addSection(new Paragraph("Unit Learning Completion Rate",titlefont));
        Paragraph deptResult=new Paragraph("Unit Weekly Completion Rate:"+DweeklyStudyRate*100+"%,Monthly completion rate:"+DmonthlyStudyRate*100+"%. ",textfont);
        deptResult.setSpacingBefore(20);
        deptResult.setAlignment(Element.ALIGN_CENTER);
        section1.add(deptResult);
        try {
            document.newPage();
            document.add(chapter);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        document.close();//Close the write stream
        return file;

At this step in document.close(), an exception is thrown and saved as The page 1 was requested but the document has only 0 pages. The corresponding pdf file already exists, and the size of the file is 0 bytes. Later, the document.add(chapter) was deleted and the error of document has no pages was reported. But on the computer of the classmates, it can be successfully executed.
Compared with the environment of two computers, there is no big difference. Coincidentally, I checked iText's bag again:

Besides:
There is also an itext-asian.jar package.
Comparing with classmates, it is found that iText-asian.jar package has forgotten to be introduced. So there's a mistake.

I'm glad to help you.

Posted by garcon on Wed, 13 Feb 2019 00:45:18 -0800