5

I'm trying to create a PDF document with more than 2 pages in portrait and others in landscape, I found that both page and text rotates to landscape I need to prevent page content rotation. am using following code

 Document document = new Document(PageSize.A4, 36, 36, 36, 72);
    PdfWriter writer = PdfWriter.getInstance(document, new 
    FileOutputStream(outPutDirectory + indexID + ".pdf"));
    writer.setPageEvent(new Orientation(orientation));
    document.open();
    XMLWorkerHelper.getInstance().parseXHtml(writer,document, new ByteArrayInputStream(parserXHtml(page.getPageContent()).getBytes()))
    document.close();

my expected result should be like this

expected result.pdf

mkl
  • 90,588
  • 15
  • 125
  • 265
arj
  • 887
  • 1
  • 15
  • 37

1 Answers1

6

Instead of using a page event, you have to change the page size.

For instance:

Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new 
FileOutputStream(outPutDirectory + indexID + ".pdf"));
document.open();
// Add some content in portrait
document.setPageSize(PageSize.A4.rotate());
document.newPage();
// Add some content in landscape
document.close();

Be aware of the fact that the page size only changes on the next page. The order of setPageSize() and newPage() is important.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • The OP does not say so but his code seems to imply that he wants to fill his document using the `XMLWorker`. Probably he isn't sure when new pages start and, therefore, attempts to use page events... (all pure guesswork, I admit) – mkl Aug 04 '17 at 07:20
  • OK, in that case, he might want to introduce some custom tag and extend the `TagWorkerFactory` to introduce the change in page size. – Bruno Lowagie Aug 04 '17 at 07:27