Reputation: 8912
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.
Upvotes: 1
Views: 9643
Reputation: 77528
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.
Upvotes: 4