Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rotate pages but not the text in iText?

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

like image 565
Raul Avatar asked Apr 24 '17 04:04

Raul


People also ask

What is iText format?

iText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them. The following tutorial will show how to create PDF files with iText. This tutorial assumes that you have basis Java and Eclipse knowledge. iText has a hierarchical structure.

Is iText free for commercial use?

This license is a commercial license. You have to pay for it. To answer your question: iText can be used for free in situations where you also distribute your software for free. As soon as you want to use iText in a closed source, proprietary environment, you have to pay for your use of iText.

Is iText PDF open source?

iText is rooted in the Open-Source community Over 20 years ago, the code that formed the basis for the iText PDF library as we know it today was written in an open-source environment. Even today, its code remains open-source.

What is iText tool?

iText is a set of PDF tools for Java and . NET developers. This tool allows software developers to create PDF documents, perform conversions, and easily produce interactive forms that are optimized to the customer's needs.


1 Answers

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();
like image 119
Sergey Avatar answered Sep 22 '22 03:09

Sergey