问题
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.
回答1:
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();
来源:https://stackoverflow.com/questions/43579992/how-to-rotate-pages-but-not-the-text-in-itext