How to rotate pages but not text in iText?

I'm trying to create a PDF document with some pages in portrait and others in landscape, but after seeing this example ( iText7 - Page Orientation and Rotation ) I found that the page rotates to landscape, but the text does it too ( PDF created with iText7 samples ), then I want the pages to rotate, but the text continues from left to right, as in the following image.

Note. I tried to use document.getPdfDocument().addNewPage(new PageSize(PageSize.A4.rotate()));

but it works for one page and not for the next x pages.

enter image description here

+3


source to share


1 answer


You can do it with a page size setting

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 a landscape oriented page with Hi2, and the last page will be landscape oriented . All new pages will be landscape oriented until you set the new page style with 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();

      

+5


source







All Articles