1

in iText i only need to get one number: Count of all pages in document.

I have this class what i get as a pattern somewhere from iText:

class PageCounter extends PdfPageEventHelper {

    PdfTemplate t;
    Image total;

    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        t = writer.getDirectContent().createTemplate(30, 16);

        try {
            total = Image.getInstance(t);
            total.setRole(PdfName.ARTIFACT);
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfPTable table = new PdfPTable(3);
        try {
            table.setWidths(new int[] { 24, 24, 2 });
            table.setTotalWidth(tableWidth);

            Font font = returnCustomFont(fontTypeRegular, fontSizeRegular,
                    BaseColor.BLACK);

            DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy / HH:mm");
            PdfPCell cell1 = new PdfPCell(
                    new Phrase(dateFormat.format(new Date()), font));
            cell1.setVerticalAlignment(Element.ALIGN_TOP);
            cell1.setBorder(Rectangle.NO_BORDER);
            cell1.setFixedHeight(30);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell(new Phrase(
                    String.format("Page %d of", writer.getPageNumber()), font));
            cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell2.setBorder(Rectangle.NO_BORDER);
            cell2.setFixedHeight(30);
            cell2.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell(total);
            cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell3.setBorder(Rectangle.NO_BORDER);
            cell3.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell3);

            PdfContentByte canvas = writer.getDirectContent();
            canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
            table.writeSelectedRows(0, -1, 30, 40, canvas);
            canvas.endMarkedContentSequence();
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {
        ColumnText.showTextAligned(t, Element.ALIGN_LEFT,
                new Phrase(String.valueOf(writer.getPageNumber())), 2, 2, 0);
    }
}

The trouble is, that it derives count of all pages only as Image format, and i need it as anything formattable as text (Integer, String etc.).

The only one thing I have found is this question in stackoverflow: How to add total page number on every page with iText?

And the very first answer is interesting, but unfortunatelly, there is no example, only this description (inbetween lines):


1) Process the output from a PdfWriter to a bytestream first with a dummy page count.

2) Create a PdfReader from that bytestream, calling PdfReader.getNumberOfPages to get the actual page count.

3) Recreate the PDF output, knowing what the page count will be, changing the footer accordingly.


If nobody had any better plan how to achieve that page count, I would be interested, how specifically can i handle POINT NUMBER 1. Do you have any idea?

NOTE: When i start to type number of current page, i already need to know count of all pages and type it there too.

My WHOLE code:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class Pdf1formiText {

public static final String DEST = "c:/radek-folder/pdf1iText.pdf";
protected int horizontalAlignmentLeft = Element.ALIGN_LEFT;
protected int horizontalAlignmentCenter = Element.ALIGN_CENTER;
protected int verticalAlignmentMiddle = Element.ALIGN_MIDDLE;
protected int verticalAlignmentTop = Element.ALIGN_TOP;
protected String fontTypeRegular = "c:/radek-folder/font_sitebook.ttf";
protected String fontTypeBold = "c:/radek-folder/font_sitebook_bold.ttf";
protected float fontSizeRegular = 10f;
protected float fontSizeLarger = 14f;
protected float fontSizeLarge = 16f;
float tableWidth;

public static void main(String[] args) throws IOException, DocumentException {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new Pdf1formiText().createPdf(DEST);
    System.out.println("done");
}

public void createPdf(String dest) throws IOException, DocumentException {
    float[] columns = { 100, 50, 100, 50, 50, 50, 50, 50, 75, 50, 50, 50 };
    Document document = new Document(PageSize.A4.rotate(), 36, 36, 36, 36);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    writer.setPageEvent(new PageCounter());

    document.open();
    PdfPCell cell;
    // Header part
    PdfPTable mainTable = new PdfPTable(1);
    mainTable.getDefaultCell().setPadding(0);
    mainTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    mainTable.setTotalWidth(columns);
    mainTable.setLockedWidth(true);

    PdfPTable subTable1 = new PdfPTable(columns.length);
    subTable1.setTotalWidth(columns);
    subTable1.setLockedWidth(true);
    cell = createAndReturnTextCell(horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Přenosové trasy GTS", columns.length, 1, fontTypeBold, fontSizeLarge,
            BaseColor.BLACK);
    cell.setPaddingTop(10f);
    cell.setPaddingBottom(15f);
    subTable1.addCell(cell);

    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Trasa [-]", 1, 1, fontTypeBold, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Pásmo [GHz]", 1, 1, fontTypeBold, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Kap a konf [Mbit/s]", 1, 1, fontTypeBold, fontSizeRegular,
            PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Typ zařízení Index [-]", 1, 1, fontTypeBold, fontSizeRegular,
            PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Zeměpis.délka WGS84[°-'-\"]", 1, 1, fontTypeBold, fontSizeRegular,
            PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Zeměpis.šířka WGS84[°-'-\"]", 1, 1, fontTypeBold, fontSizeRegular,
            PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Nadm.výška [m]", 1, 1, fontTypeBold, fontSizeRegular,
            PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Výška ant.[m]", 1, 1, fontTypeBold, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Průměr ant./zisk [m]/[dB]", 1, 1, fontTypeBold, fontSizeRegular,
            PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Útlum vlnovod [dB]", 1, 1, fontTypeBold, fontSizeRegular,
            PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Výkon vysílač dBm", 1, 1, fontTypeBold, fontSizeRegular,
            PdfPCell.NO_BORDER);
    addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Délka trasy [m]", 1, 1, fontTypeBold, fontSizeRegular,
            PdfPCell.NO_BORDER);
    mainTable.addCell(subTable1);

    PdfPTable subTable2 = new PdfPTable(columns.length);
    subTable2.setTotalWidth(columns);
    subTable2.setLockedWidth(true);
    addTableCellToTable(subTable2, innerTableInCell1(), 10, 1);
    addTextCellToTable(subTable2, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "2959.32", 1, 2, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(subTable2, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "2009-03-30", 1, 2, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    mainTable.addCell(subTable2);

    PdfPTable subTable3 = new PdfPTable(columns.length);
    subTable3.setTotalWidth(columns);
    subTable3.setLockedWidth(true);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "CZ5142", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "38", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "100M", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "QAM_32", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "28", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "FibeAir 1500p 38GHz", 1, 1, fontTypeRegular, fontSizeRegular,
            PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "N/A", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Praha 4 Milevská 7", 1, 1, fontTypeRegular, fontSizeRegular,
            PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "14°26'18''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "14°26'18''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "265", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "70", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
    mainTable.addCell(subTable3);

    for (int i = 0; i < 200; i++) {
        PdfPTable subTable4 = new PdfPTable(columns.length);
        subTable4.setTotalWidth(columns);
        subTable4.setLockedWidth(true);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "CZ5142", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "38", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "100M", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "QAM_32", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "28", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "FibeAir 1500p 38GHz", 1, 1, fontTypeRegular, fontSizeRegular,
                PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "N/A", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "Praha 4 Milevská 7", 1, 1, fontTypeRegular, fontSizeRegular,
                PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "14°26'18''", 1, 1, fontTypeRegular, fontSizeRegular,
                PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "14°26'18''", 1, 1, fontTypeRegular, fontSizeRegular,
                PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "265", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
        addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "70", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
        mainTable.addCell(subTable4);
    }

    document.add(mainTable);
    document.close();
}

private PdfPTable innerTableInCell1() throws DocumentException {
    int[] columnWidths = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    PdfPTable innertable = new PdfPTable(columnWidths.length);
    innertable.setWidths(columnWidths);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Brno Špitálka 8 Brno Hájecká 1068/14 CZ5159", 1, 2, fontTypeRegular,
            fontSizeRegular, PdfPCell.NO_BORDER);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "38510.5", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "37250.5", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "MINI-LINK/E 38GHz", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "V / 4x2M / 1+0", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "16?37'21.4''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "16?38'16.94''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "49?11'47.3''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "49?10'18.75''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "217", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "201", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "92", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "10", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "0.30/40.4", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "0.60/44.3", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "UKY21008/SC11", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "UKY21009/SC11", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "0", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "0", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "15", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "15", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
    return innertable;
}

private static void addTableCellToTable(PdfPTable mainTable, PdfPTable insertedTable, int colspan,
        int rowspan) {
    PdfPCell cell = new PdfPCell(insertedTable);
    cell.setColspan(colspan);
    cell.setRowspan(rowspan);
    mainTable.addCell(cell);
}

private static void addTextCellToTable(PdfPTable table, int horizontalAlignment,
        int verticalAlignment, String value, int colspan, int rowspan,
        String fontType, float fontSize, int borderStyle) {
    BaseFont base = null;
    try {
        base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Font font = new Font(base, fontSize);
    PdfPCell cell = new PdfPCell(new Phrase(value, font));
    cell.setColspan(colspan);
    cell.setRowspan(rowspan);
    cell.setHorizontalAlignment(horizontalAlignment);
    cell.setVerticalAlignment(verticalAlignment);
    cell.setBorder(borderStyle);
    table.addCell(cell);
}

private static PdfPCell createAndReturnTextCell(int horizontalAlignment,
        int verticalAlignment, String value, int colspan, int rowspan,
        String fontType, float fontSize, BaseColor color) {
    BaseFont base = null;
    try {
        base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Font font = new Font(base, fontSize);
    font.setColor(color);
    PdfPCell cell = new PdfPCell(new Phrase(value, font));
    cell.setColspan(colspan);
    cell.setRowspan(rowspan);
    cell.setHorizontalAlignment(horizontalAlignment);
    cell.setVerticalAlignment(verticalAlignment);
    cell.setBorder(PdfPCell.NO_BORDER);
    return cell;
}

private static void addImageCellToTable(PdfPTable table, int horizontalAlignment,
        int verticalAlignment, Image value, int colspan, int rowspan, String fontType,
        float fontSize) {
    PdfPCell cell = new PdfPCell(value);
    cell.setColspan(colspan);
    cell.setRowspan(rowspan);
    cell.setHorizontalAlignment(horizontalAlignment);
    cell.setVerticalAlignment(verticalAlignment);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);
}

class PageCounter extends PdfPageEventHelper {

    PdfTemplate t;
    Image total;

    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        t = writer.getDirectContent().createTemplate(30, 16);
        t.setColorFill(BaseColor.LIGHT_GRAY);
        try {
            total = Image.getInstance(t);
            total.setRole(PdfName.ARTIFACT);
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfPTable table = new PdfPTable(2);
        try {
            table.setWidths(new int[] { 48, 2 });
            table.setTotalWidth(tableWidth);
            table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);

            PdfPCell cell = createAndReturnTextCell(Element.ALIGN_RIGHT,
                    verticalAlignmentTop,
                    String.format("%d /", writer.getPageNumber()), 1, 1,
                    fontTypeRegular, 13f, BaseColor.LIGHT_GRAY);
            cell.setFixedHeight(30);
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);

            addImageCellToTable(table, Element.ALIGN_RIGHT, verticalAlignmentTop,
                    total, 1, 1, fontTypeRegular, fontSizeRegular);

            PdfContentByte canvas = writer.getDirectContent();
            canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
            table.writeSelectedRows(0, -1, 40, 40, canvas);
            canvas.endMarkedContentSequence();
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {
        ColumnText.showTextAligned(t, Element.ALIGN_LEFT,
                new Phrase(String.valueOf(writer.getPageNumber())), 1, 1, 0);
    }
}
}
Community
  • 1
  • 1
user7968180
  • 145
  • 1
  • 15
  • Do you need the number of pages of document that's been created already, or are you creating a document and do you need to know the current number of pages? Also: http://developers.itextpdf.com/examples-itext5 http://developers.itextpdf.com/examples-itext7 – Samuel Huylebroeck May 18 '17 at 12:57
  • that document does not exist as a file in the moment when i need to use this code. I have added my whole code for sure. – user7968180 May 18 '17 at 13:04
  • When exactly do you need the number? While you are still creating the document? When you've just finished adding the last piece of content? – mkl May 18 '17 at 13:12
  • Whats the problem on asking the PdfWriter instance about the .getPageNumber() at any time in the creation process? – COeDev May 18 '17 at 13:22
  • Have a look into class PageCounter / onOpenDocument method. That "Image total" brings the picture with the number of page. When it starts creating document, it is already capable to find out that count. And i only need it in different format than Image. – user7968180 May 18 '17 at 13:38
  • COeDev : Problem is that i need the count after opening document (class PageCounter / onOpenDocument method). When i start to type number of current page, i already need to know count of all pages and type it there too. – user7968180 May 18 '17 at 13:46
  • @user7968180 *"Have a look into class PageCounter / onOpenDocument method. That "Image total" brings the picture with the number of page. When it starts creating document, it is already capable to find out that count."* - That is wrong: The "image total" start out as an empty canvas a reference to which is added to each page. Just shortly before closing the document, in `onCloseDocument`, the actual number of pages is written to that empty canvas. – mkl May 18 '17 at 14:03
  • Ok, but i am waching it and still dont know how to get it from that PdfTemplate variable. Or derive it from anything else. – user7968180 May 18 '17 at 15:05

0 Answers0