Adding a new page in PDF using itext 7
Asked Answered
R

2

25

I'm trying to create a PDF Document using iText 7 with below code and my PDF documents contents are overlapping in same page when generated.(i.e in Page 1).

I see the

document.newPage();

method is missing in iText 7. How can i add pages to my PDF document without using pdfDocumet.copyPages(...) or PDFmerger in itext 7.

        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));      
        pdfDoc.addNewPage();
        Document PageOnedocument = new Document(pdfDoc,PageSize.A4);            
        addPageOneContents(PageOnedocument);  


        pdfDoc.addNewPage();
        Document PageTwodocument = new Document(pdfDoc,PageSize.A4);            
        addPageTwoContents(PageTwodocument);  

        pdfDoc.close();
        PageOnedocument.close();
        PageTwodocument.close();
Rhine answered 29/11, 2016 at 6:48 Comment(0)
U
56

In iText 7 the newPage method has become a special case of an area break:

Document document = ...;
[....add some content...]
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
[...add some content on next page...]
Utopia answered 29/11, 2016 at 7:47 Comment(5)
Is this documented somewhere?Vend
@Vend In itextpdf.com/en/resources/books/itext-7-building-blocks/… - but it is a bit hidden among more complex AreaBreak usages.Utopia
Thanks! It's very useful, I was getting mad trying to add a simple paragraph to a new page.Vend
@Utopia getting "Access denied" on trying to access that link. Is that only for commercial access?Mclain
@AlexP. "Is that only for commercial access?" - No, the book merely moved to a different URL, see kb.itextpdf.com/home/it7kb/ebooks/itext-7-building-blocks/… - marketing likes to re-structure the site...Utopia
M
0

If you're using C# and refactoring code that referenced iText 5 (like I was) try putting the iText 7 method in an override.

public static class Itext7DocumentExtensions
{
    public static void NewPage(this Document document)
    {
        document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
    }
}

Now you can call document.NewPage();

Mutt answered 5/4, 2021 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.