1

I am using iTextSharp to generate PDF document. Currently, the HTML contents are converted successfully into PDF document. The default page orientation is Portrait.

However, my requirement is is create PDF document with some page in Portrait and some in Landscape.

The following line generates PDF document with Portrait orientation

    document.SetPageSize(PageSize.A4);

And, if i change this line to than it creates whole document in Landscape.

        document.SetPageSize(PageSize.A4.Rotate());

How i can generate PDF with mixed portrait and landscape orientation?

Kindly advise.

TheKingPinMirza
  • 7,924
  • 6
  • 51
  • 81
  • For all things iText or iTextSharp, please download the free ebook [The Best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html). It contains the answers to questions of hundreds of developers, including the answer to your current question. You'll discover that your question is a duplicate of [iText create document with unequal page sizes](http://stackoverflow.com/questions/23117200/itext-create-document-with-unequal-page-sizes) – Bruno Lowagie May 15 '15 at 11:06

1 Answers1

4

You already have everything you need. The method you are using is the correct one. You can use it more than once, just be aware of the fact that you need to change the page size before a new page is created:

Document document = new Document();
PdfWriter.GetInstance(document, new System.IO.FileStream(filename, System.IO.FileMode.Create));
document.SetPageSize(PageSize.A4);
document.Open();
document.Add(new Paragraph("Hi in portrait"));
document.SetPageSize(PageSize.A4.Rotate());
document.NewPage();
document.Add(new Paragraph("Hi in landscape"));
document.Close();

As you can see, we set the page size to A4 in portrait before we Open() the document. We add some content to this page, and then we decided to set the page size for the next page to A4 in landscape. This will only take effect after a new page starts. This can be triggered automatically by iText when you add content that doesn't fit the current page. Or you can trigger this yourself by invoking NewPage(). In the example, the second paragraph is added to a page in landscape.

See also iText create document with unequal page sizes for a Java example.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • i tried like the way you mentioned above, So, it generates the whole PDF in landscape. Am i missing something here? document.SetPageSize(PageSize.A4); document.Open(); document.SetPageSize(PageSize.A4.Rotate()); document.NewPage(); @Bruno – TheKingPinMirza May 17 '15 at 12:26
  • You need to add content after `document.Open()` (or indicate that you want a blank page). – Bruno Lowagie May 18 '15 at 07:31