iTextSharp insert a page at the beginning

Matias Bello

Using iTextSharp, how can I insert a new page at the beginning of the page, when the PdfWriter has been writing pages already? Suppose the case of an index page which should be the first page of the document, but you wouldn't know its contents until you write the whole document. Particularly, on which page is each section/chapter written.

Bruno Lowagie

You can't go back to the first page while you're creating a document, but there are different ways to solve your problem.

If you don't expect to have many pages, you could consider the solution that is explained in chapter 5 of iText in Action - Second Edition, more specifically in the MovieHistory1.java example.

In this example, we reorder the pages right before we close the document:

// step 1
Document document = new Document();
// step 2
PdfWriter writer
    = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// IMPORTANT: set linear page mode!
writer.setLinearPageMode();
// step 3
document.open();
// step 4
// Add all your content
// Create a new order for the pages
int total = writer.reorderPages(null);
// change the order
int[] order = new int[total];
for (int i = 0; i < total; i++) {
    order[i] = i + toc;
    if (order[i] > total)
        order[i] -= total;
}
// apply the new order
writer.reorderPages(order);
// step 5
document.close();

Why do I only recommend this for documents with a limited number of pages? For this functionality to work we need to create a linear page tree:

writer.setLinearPageMode();

A linear page tree is not really a tree (it's a tree without any branches) and that is not optimal in PDF.

It is better to reorder the pages in a second go. This is explained in two questions that are bundled in The Best iText Questions on StackOverflow (a free ebook).

The questions were:

I know that having redundant info on SO is not ideal, but this is the code you'd need:

PdfReader reader = new PdfReader(baos.toByteArray());
int n = reader.getNumberOfPages();
reader.selectPages(String.format("%d, 1-%d", n, n-1));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
stamper.close();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy document by page to insert blank page in between using iTextsharp

From Dev

iTextSharp vertical page fill

From Dev

Header of Page with iTextSharp

From Dev

Insert sections at the beginning of UICollectionView

From Dev

Insert a word at beginning of line

From Dev

iTextSharp - PDF Bookmark not pointing to a page

From Dev

iTextSharp: Header on only First Page

From Dev

Insert element at the beginning of Ruby Hash?

From Java

Insert element at the beginning of the list in dart

From Dev

How to insert element at beginning of vector

From Dev

Insert character at the beginning of specific lines

From Dev

how to insert something in the beginning of an element?

From Dev

iTextSharp PdfWriter Rotating page layout when it shouldnt be

From Dev

Is it possible to convert PDF page to Image using itextSharp?

From Dev

Error in converting aspx page to html (itextsharp 5.0.6)

From Dev

Bookmark to specific page using iTextSharp 4.1.6

From Dev

ITextSharp add page numbers to merged pdf

From Dev

put page number when create PDF with iTextSharp

From Dev

ITextSharp - include footer on every page but the first and the last

From Dev

iTextSharp table pushing another table to a new page

From Dev

iTextSharp Resize each page to fit the pagesize

From Dev

Allow page extraction in a password security pdf with itextsharp

From Dev

ITextSharp open pdf at page index with zoom

From Dev

iTextSharp PdfWriter Rotating page layout when it shouldnt be

From Dev

iTextSharp adding new instances of an existing page

From Dev

Get PDF be written in the center of the page itextsharp

From Dev

iTextsharp - Get rotation of pdf page in C#

From Dev

iTextSharp Resize each page to fit the pagesize

From Dev

iTextsharp - Repeat a html table on top of every page

Related Related

  1. 1

    Copy document by page to insert blank page in between using iTextsharp

  2. 2

    iTextSharp vertical page fill

  3. 3

    Header of Page with iTextSharp

  4. 4

    Insert sections at the beginning of UICollectionView

  5. 5

    Insert a word at beginning of line

  6. 6

    iTextSharp - PDF Bookmark not pointing to a page

  7. 7

    iTextSharp: Header on only First Page

  8. 8

    Insert element at the beginning of Ruby Hash?

  9. 9

    Insert element at the beginning of the list in dart

  10. 10

    How to insert element at beginning of vector

  11. 11

    Insert character at the beginning of specific lines

  12. 12

    how to insert something in the beginning of an element?

  13. 13

    iTextSharp PdfWriter Rotating page layout when it shouldnt be

  14. 14

    Is it possible to convert PDF page to Image using itextSharp?

  15. 15

    Error in converting aspx page to html (itextsharp 5.0.6)

  16. 16

    Bookmark to specific page using iTextSharp 4.1.6

  17. 17

    ITextSharp add page numbers to merged pdf

  18. 18

    put page number when create PDF with iTextSharp

  19. 19

    ITextSharp - include footer on every page but the first and the last

  20. 20

    iTextSharp table pushing another table to a new page

  21. 21

    iTextSharp Resize each page to fit the pagesize

  22. 22

    Allow page extraction in a password security pdf with itextsharp

  23. 23

    ITextSharp open pdf at page index with zoom

  24. 24

    iTextSharp PdfWriter Rotating page layout when it shouldnt be

  25. 25

    iTextSharp adding new instances of an existing page

  26. 26

    Get PDF be written in the center of the page itextsharp

  27. 27

    iTextsharp - Get rotation of pdf page in C#

  28. 28

    iTextSharp Resize each page to fit the pagesize

  29. 29

    iTextsharp - Repeat a html table on top of every page

HotTag

Archive