6

I'm trying to create a PDF document with some pages in portrait and others in landscape, but seeing this example (iText7 - Page orientation and rotation) I found that the page rotates to landscape but the text also does it (PDF generated from iText7 samples), then, I need that the pages to rotate but the text continues from left to right, how in the next image.

Note: I tried to use document.getPdfDocument().addNewPage(new PageSize(PageSize.A4.rotate()));but it works for one page, not for the next x pages.

enter image description here

Sergey
  • 176
  • 2
  • 12
Raul
  • 465
  • 5
  • 16

1 Answers1

9

You can do it with setting page size

For itextpdf 5.5.x

Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("D://qwqw12.pdf"));
doc.open();
doc.add(new Paragraph("Hi"));
doc.setPageSize(PageSize.A4.rotate());
doc.newPage();
doc.add(new Paragraph("Hi2"));
doc.newPage();
doc.add(new Paragraph("Hi3"));
doc.close();

this will create an A4-page with Hi, then an landscape-oriented page with Hi2, and last page will also be landscape-oriented. All new pages will be landscape-oriented until you don't set new page style via setPageSize().


For itextpdf 7.x

PdfDocument pdfDoc = new PdfDocument(new PdfWriter("D://qwqw12.pdf"));
Document doc = new Document(pdfDoc, PageSize.A4);
doc.add(new Paragraph("Hi"));
doc.getPdfDocument().setDefaultPageSize(PageSize.A4.rotate());
doc.add(new AreaBreak());
doc.add(new Paragraph("Hi2"));
doc.add(new AreaBreak());
doc.add(new Paragraph("Hi3"));
doc.close();
Sergey
  • 176
  • 2
  • 12
  • Hi, thanks for your response. But setPageSize method not works in iText7. – Raul Apr 24 '17 at 13:15
  • @Raul, sorry, totally missed you're using version 7 in your question. Update solution for 7th version. – Sergey Apr 25 '17 at 04:39